The first question guesses the number and exits more than 3 times. Prompt for the corresponding result:
Main study inside for inside can use else, _ in this is not need to return the value
1Number = 352 3 for_inchRange (3):4cur = Int (input ('Enter your number:'))5 ifCur = =Number :6 Print("Your win!")7 Break8 elifCur <Number :9 Print(" Less")Ten Else: One Print("bigger") A Else: - Print("Your guess more than 3 time!") -~View Code
Second question: Find the list inside the collection (Do not know what topic)
1(test_3.4.2) [[Email protected] ~]#Vim li1.py2L = [1,3,2,4,3,3,2,5,7,7,2]3 #[1,3,2,4,5,7]4 5RET =list ()6 forIteminchL:7 ifItem not inchret:8 ret.append (item)9 Print(ret)
View Code
ten (test_3.4.2) [[email protected] ~]# One [1, 3, 2, 4, 5, 7]
Change 2:
1(test_3.4.2) [[Email protected] ~]#Vim li2.py2L = [1,3,2,4,3,3,2,5,7,7,2]3 #[1,3,2,4,5,7]4 5RET =list ()6TMP =set ()7 forIteminchL:8 ifItem not inchret:9 ret.append (item)Ten Tmp.add (item) One Print(ret) A -(test_3.4.2) [[Email protected] ~]#python li2.py -[1, 3, 2, 4, 5, 7]
View Code
This place, example 1 of the execution efficiency without example 2 high, space for Time, O (n) O (1), if the list of values too much, take up too much memory.
The third question: the number of primes in the statistics list
Prime number, also known as prime number, has an infinite number. There are no other factors other than 1 and it itself. According to the basic theorem of arithmetic, each integer greater than 1 is either a prime number or a product of a series of prime numbers, with a minimum of 2.
(test_3.4.2) [[email protected] ~]# vim ma0.py l = [2,3,5,7,10,12,6,8]count = 0for item in L: For i in range (2,item): if it Em% i = = 0: Break Else: count + = 1print (count) ~ (test_3.4.2) [[email protected] ~]# Python ma0.py 4
Change: Digital processing module knowledge (math),
1(test_3.4.2) [[Email protected] ~]#Vim ma1.py2 ImportMath3 4L = [2,3,5,7,10,12,6,8]5 6Count =07 8 forIteminchL:9 forIinchRange (2, Math.ceil (MATH.SQRT (item))):Ten ifItem% i = =0: One Break A Else: -Count + = 1 - the Print(count) - -~ -(test_3.4.2) [[Email protected] ~]#python ma1.py +4
Ceil (...)
Ceil (x)
Return the ceiling of x as an int.
This is the smallest integral value >= x.
sqrt (...)
sqrt (x)
Return the square root of X.
The first day of homework problem