Today, I practiced a bit of writing a function in Python, and you put it on a blog. Bubble sort and prime judgment

Source: Internet
Author: User
Tags shuffle

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.