Python exercises 11-20

Source: Internet
Author: User
Tags for in range pow

11, there are a pair of rabbits, from the 3rd month after birth every month, a pair of rabbits, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask each month the total number of rabbits?

12, determine how many primes between 101-200, and the output of all prime numbers.

13, print out all the "Narcissus number", so-called "Narcissus number" refers to a three-digit number, the number of its members of the cubic and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.

14, the decomposition of a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.

15, the use of the nesting of conditional operators to complete the problem: Academic scores >=90 points of the students with a, 60-89 between the use of B, 60 points below the C is indicated.

16. Output the date in the specified format.

17, enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.

18, the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (a total of 5 numbers are added at this time), several numbers are added by keyboard control.

19, if a number is exactly equal to the sum of its factors, this number is called "The end of the number." For example, 6=1+2+3. Programming to find all the finished numbers within 1000.

20, a ball from 100 meters height of the free fall, each landing back to the original height of half, and then down, to ask it to fall on the 10th time, the total number of meters? How high is the 10th time rebound?

11, there are a pair of rabbits, from the 3rd month after birth every month, a pair of rabbits, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask each month the total number of rabbits?

" "can be divided into two parts, the December Bunny and more than March adult rabbits" "Rabbits= {'1st'70A'2nd'70A'Adult': 1}#Unit: Yes Total= []#number of nth months forNinchRange (1,22):    ifN < 3:        Pass    Else: rabbits['Adult'] + = rabbits['2nd']#every month, February rabbits grow into adult rabbits .rabbits['2nd'] = rabbits['1st']#January rabbits grow to February rabbitsrabbits['1st'] = rabbits['Adult']#Adult rabbit gives birth to January rabbittotal.append (SUM (rabbits.values ()))Print(total)
View Code

12, determine how many primes between 101-200, and the output of all prime numbers.

 L = []  for  x in  range (100,201 = True #   set status bit  #   for-N in range (2,int (math.sqrt (x)) +1):  for  n in  Range (2,int (x * *) +1 if  x% n = = 0: #   poll for approximate  Flag = False  if  flag == True:l.append (x)  print   (L)  print  (len (L)) 
View Code

13, print out all the "Narcissus number", so-called "Narcissus number" refers to a three-digit number, the number of its members of the cubic and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.

Import= range (100,1000) for in num:    = Math.pow (x//100,3)  # number of hundred    b = Math.pow (x%100//10,3)  #  10 digits    c = Math.pow (x%10,3)  #  single digit     If A + b + c = = x        :print(' number of daffodils:', X )
View Code

14, the decomposition of a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.

x = 100deff (x): L= []     whileTrue: forNinchRange (2, x):ifX% n = = 0:#Minimum approximatel.append (str (n)) x= Int (x/n)#In addition to the approximate, re-cycle                 Break        ifn = = x-1:#after all data polling, output resultsl.append (str (x))returnll=f (x)Print(L)Print('{}={}'. Format (str (x),'*'. Join (L))
View Code

15, the use of the nesting of conditional operators to complete the problem: Academic scores >=90 points of the students with a, 60-89 between the use of B, 60 points below the C is indicated.

A =print('a'ifelse ('C' ) if Else ' B '))
View Code

16. Output the date in the specified format.

ImportdatetimePrint(Datetime.date (1941, 1, 5). Timetuple ())Print(Datetime.date (1941, 1, 5). Timetuple () [0])Print(Datetime.datetime (2011,12,3). Strftime ('%Y%m%d'))Print(Datetime.date.today (). Strftime ('%d/%m/%y'))Import TimePrint(Time.asctime ())Print(Time.localtime (Time.time ())) a=Time.localtime (Time.time ())Print(List (a))Print(a[1])#Print (Time.strftime ('%y year%m month%d ', a))Print(Time.strftime ('%Y%m%d', a))Print(Time.strptime ('2017-4-3','%y-%m-%d%H'))
View Code

17, enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.

s ='123123FSDFSDFSD!*@#'Letters= [x forXinchSifX.isalpha ()] Number= [x forXinchSifx.isdigit ()] Space= [x forXinchSifX.isspace ()] Other= [x forXinchSifX not inchLetter + number +Space]Print(len (letter))Print(len (number))Print(len (space))Print(Len (Other))
View Code

18, the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (a total of 5 numbers are added at this time), several numbers are added by keyboard control.

#method One:n = 10s= 3L= [Int (str (s) *i) forIinchRange (1,n+1)]Print(SUM (l)) n= 10s= 3a= Map (LambdaI:int (str (s) *i), Range (1,n+1))Print(sum (a))#Method Twon = 10a= 3L=[]s1=0SUM=0 forXinchrange (N): S1+ = a*10**x SUM+=S1 l.append (S1)Print(L)Print(SUM (l))Print(SUM)
View Code

19, if a number is exactly equal to the sum of its factors, this number is called "The end of the number." For example, 6=1+2+3. Programming to find all the finished numbers within 1000.

def sum_test (n):     = 0    for in range (1, N):        if n%x = =  0            :+ = x     if s = = N:  #  If the number is completed, the value is output        return for inif  sum_test (i)]print(a)
View Code

20, a ball from 100 meters height of the free fall, each landing back to the original height of half, and then down, to ask it to fall on the 10th time, the total number of meters? How high is the 10th time rebound?

deff (x): H= []     forIinchRange (x): H.append (100 * (1/2) * *i) Distance= H[0] + 2 * SUM (h[1:]) H_= * (i+1) * * ()    Print('{} at the time of landing, the total of {} m'. Format (x,distance))Print('the height of the second {} bounce is {} m'. Format (x,h_)) forXinchRange (11): F (x+1)
View Code

Python exercises 11-20

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.