Python small algorithm and python Algorithm

Source: Internet
Author: User

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.

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.