The first one is to judge the prime number, first on the Code bar:
1 fromMathImportsqrt2 defIs_prime (i):3 ifi = = 2:4 returnTrue5 elifI<2:6 returnFalse7 elifi%2 = =0:8 returnFalse9 Else:Ten forNinchRange (3,int (sqrt (i) +1), 2): One ifi%n = =0: A returnFalse - returnTrue
Here is the math package sqrt, in order to maximize efficiency, less loops, there is a range function, there are three parameters, respectively, the starting point, end point, interval, interval is optional, like range (0,12,3 is [0,3,6,9].
The second one is the bubble sort, or the code:
Import randomlist = range (0,10) for I in list: print (i) random.shuffle (list) for I in list: print (i) for D in range ( Len (list) -1,0,-1): For e in range (0,d,1): if list[e] > list[e+1]: list[e],list[e+1] = list[e+1],list[ E]for i in list: print (i)
This is very interesting, contains a random package, because I want to put an ordered array of scrambled, with this random.shuffle function, bubble sort nothing to say, the only thing to note is this sentence
LIST[E],LIST[E+1] = List[e+1],list[e], is not very cool, do not use a third-party variable, but it seems that the compile time will produce a third-party variable. Here again to leave a problem for themselves to achieve random.shuffle ().
The code for the problem yesterday:
def volid (pwd): a = any (map (str.isupper,pwd)) B = No (map (str.islower,pwd)) C = Any (map (str.isdigit,pwd)) d = Not all (map (str.isalnum,pwd)) if sum ([A,b,c,d]) >1: return True else: return False
Today, I practiced a bit of writing a function in Python, and you put it on a blog. Bubble sort and prime judgment