Python small algorithm and python Algorithm
1. String a with a length of m, string B with a length of n, (m> n) judge whether all the letters in B are in? O (n) minimum. class Num (object): def getNum (self, m): numList = filter (lambda x: not [x % I for I in range (2, x) if x % I = 0], range (2,500) return numList [0: m] def getSizeDict (self, string): numList = self. getNum (len (string) strDict ={} for s in set (string): strDict [s] = numList [0] del numList [0] return strDict def getDiff (self, string, string2): j = 1 flag = 1 strDict = self. getSizeDict (string) numList = self. getNum (len (string) for I in numList: j * = I for s in string2: try: d = strDict [s] random t: flag = 0 break return flagif _ name _ = '_ main _': string = raw_input ('string :')
String2 = raw_input ('another: ') main = Num () num = main. getDiff (string, string2) print num = 1 and 'yes' or 'no'
Python algorithm problems: traversal and iteration
No. Nested algorithms 50 times are very slow.
Generally, a close loop can be nested by three times. You have to sort out your algorithms and think about how to reduce nesting.
The general method is,
1. simplify some operations through function calls
2. check whether there are repeated content in your nesting. Remove it if any. You can also simplify the structure.
3. Implement the execution in segments. Instead of nested implementations
I estimate that you use the attributes of the role (or object) and the role (object) to scan and judge, and calculate the distance.
For example, the attribute of a role is a parallel relationship and does not need to be implemented through loops.
Role attribute collection and calculation can also be divided into two sections. In this way, you do not need to concentrate on the loop body.
Python exercises (algorithms)
According to the value of x after each loop:, 27, 64 .....
We can see that the cubic I of X [I] = I is 1, 2, 3, 4... I is the number of cycles.
Therefore, the function of the program is to calculate the cube of 2 * n results.