10 lines of Python code solve Joseph's ring (simulation) and 10 lines of python
I wrote this article because I saw someone else's blog using a long article (more than 50 lines) to solve a Joseph's ring problem. It is also known for its simplicity. In addition, if you search for python Joseph in degrees X, the first few items are all wrong. This is a great tragedy.
In general, it is a mistaken person.
Although it is very inefficient to use simulation to solve this Joseph Ring problem, it is easier to understand.
Run the code first.
def josephus(n,k): link=range(1,n+1) ind=0 for loop_i in range(n-1): ind = (ind+k)% len(link) ind-=1 print 'Kill:',link[ind] del link[ind] if ind==-1: # the last element of link ind=0 print 'survice :',link[0] if __name__ == '__main__': josephus(100000,300) print '-'*30 josephus(10,5) print '-'*30 josephus(10,1)
We can see that the entire function is only 10 rows.
The idea is very simple. We use a modulo to find the location to be deleted. However, the subscript starts from 0 and the number starts from 1. In addition, after del of py, the subscript will increase by 1, so we need to subtract it back.
Which of the following statements is true?
Del link [ind-1]
Ind-= 1
However, because both of them need to return 1, it is OK to directly ind-= 1.
In addition, it is important to come to the end of the ring, that is,-1 of py (this is the best place, tuple and list support negative subscript in py). After deletion, it will change to 0 at the beginning.
If you think I have written a mistake, you must comment on it and point out that you do not want to miss your children.
How can a python script read data from the screen output sysstdin read data output every 10 rows?
Import sys
Class LineBuffer:
Def _ init _ (self, buffsize = 10 ):
Self. buffer = []
Self. buffsize = buffsize
Def flush (self ):
Output, self. buffer = self. buffer, []
Return output
Def append (self, line ):
Self. buffer. append (line)
If self. buffsize <= len (self. buffer ):
Return self. flush ()
Cache = LineBuffer (5)
While True:
Ln = sys. stdin. readline ()
If ln. strip () = 'eof ':
Break
X = cache. append (ln)
If x:
Print "-" * 32
Print ''. join (x)
Print "=" * 32
Print "-" * 32
Print ''. join (cache. flush ())
Print "=" * 32
$ Python x. py
1
2
3
4
5
--------------------------------
1
2
3
4
5
======================================
6
7
8
9
0
--------------------------------
6
7
8
9
0
======================================
A
B
C
D
EOF
--------------------------------
A
B
C
D
======================================
$
How does python have a random weight value? (Simple and efficient code)
In python, You have to transform the functions you don't want to solve directly. Now you use random. randint () is no problem, and it is not a stupid method, and the code efficiency is not low.
Another method is to use the choice function in random, random. the function of choice is to obtain a random element from the sequence. The programming idea is to add the elements to be randomly output to a list according to different numbers, and then use random. choice, from which one is randomly selected.
========================================================== ==================
The Code is as follows:
Def randomchice (a, B, c, d, fa, fb, fc, fd ):
Import random
Tl = []
For I in range (0, fa): tl. append ()
For I in range (0, fb): tl. append (B)
For I in range (0, fc): tl. append (c)
For I in range (0, fd): tl. append (d)
Return random. choice (tl)
You only need to enter the following information for use:
Print randomchice ('hangsan', 'lisi', 'hangzhou', 'zhouqi ,)
========================================================== ==================
What python pursues is not speed efficiency but code efficiency. The code written in python is concise and clear enough. If speed is required, it can be embedded in C language, this is at least 100 times more efficient than python.
Hope to help you.