Cainiao tutorial-exercise instance answer I, Example
Question:There are four numbers: 1, 2, 3, and 4. How many three numbers can be composed of different and non-repeated numbers? What is each?
1 #coding=utf-82 3 from itertools import permutations4 5 for i in permutations(range(1,5),3):6 k = ''7 for j in i:8 k = k + str(j)9 print int(k)
Question:The enterprise's bonus is based on the profit. If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%; if the profit is higher than 0.1 million yuan, if the profit is lower than 0.2 million yuan, the portion lower than 0.1 million yuan will be charged by 10%, and the portion higher than 0.1 million yuan, can be increased to 7.5%; 0.2 million to 0.4 million, higher than 0.2 million yuan, can be increased to 5%; 0.4 million to 0.6 million is higher than 0.4 million yuan, can be increased to 3%; between 0.6 million and 1 million, if the price is higher than 0.6 million yuan, the price can be increased to 1.5%. If the price is higher than 1 million yuan, the price of more than 1 million yuan is increased by 1%, and the current month's profit is input from the keyboard, how many bonuses should be paid?
1 # coding = UTF-8 2 3 I = int (raw_input (U' please input profit: ') 4 earn = [0.01, 0.015,] 5 rat =, 0.03, 0.05, 0.075, 0.1] 6 bonus = 0 7 for m in range (len (earn): 8 if I> earn [m]: 9 bonus + = (I-earn [m]) * rat [m] 10 I = earn [m] 11 print bonus
View Code
Question:An integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number?
1 # coding = UTF-8 2 ''' 3 x + 100 = n2, x + 100 + 168 = m2 4 m + n = I, m-n = j 5 m = (I + j)/2, positive integer 6 n = (I-j)/2, positive Integer 7''' 8 for I in range (168%): 9 if 168 I = 0: 10 j =/i11 if (I + j) % 2 = 0 and (I-j) % 2 = 0 and I> j: 12 n = (I-j)/213 x = n * n-10014 print x
View Code
Question:Enter a day of a year to determine the day of the year?
1 # coding = utf-82 3 import time4 5 time_input = raw_input (u 'enter the date, format XXXX-XX-XX:') 6 d = time. strptime (time_input, '% Y-% m-% D '). tm_yday7 print u 'is the % s day of the year' % d
View Code
Question:Output the 9*9 multiplication table.
1 #coding=utf-82 3 for i in range(1,10):4 for j in range(1,i+1):5 print '%s*%s=%s ' %(i,j,i*j),6 print ''
View Code
Question:Determine the number of prime numbers between-and output all prime numbers.
1 # coding = UTF-8 2 3 import math 4 l = [] 5 for I in range (101,201): 6 for j in range (2, int (math. sqrt (I) + 1): 7 if I % j = 0: 8 break 9 else: 10 l. append (I) # After the for loop ends, I without break is added to the List l 11 print l12 print len (l)
View Code
Question:Factorization a positive integer into a prime factor. For example, enter 90 and print 90 = 2*3*3*5.
1 # coding = UTF-8 2 3 dig_input = int (raw_input (U' input a number: ') 4 l = [] 5 while dig_input! = 1: 6 for I in range (2, dig_input + 1): 7 if dig_input % I = 0: 8 l. append (I) 9 dig_input = dig_input/i10 break11 print l
View Code
Question:Use the nested conditional operators to complete this question: Students with scores greater than or equal to 90 are represented by A, students with scores between 60 and 89 are represented by B, and students with scores below 60 are represented by C.
1 # coding = utf-82 3 score = int (raw_input (u'input score :')) 4 print 'A' if score> 89 else ('B' if score> 59 else 'C ')View Code
Question:Enter a line of characters to calculate the number of English letters, spaces, numbers, and other characters.
1 # coding = UTF-8 2 3 import re, string 4 5 str_input = raw_input (U' input a string: ') 6 r1 = re. compile ('[a-zA-Z]') 7 r2 = re. compile ('[0-9]') 8 total_letters = len (re. findall (r1, str_input) 9 total_digits = len (re. findall (r2, str_input) 10 total_whitespace = len (re. findall ('', str_input) 11 total_others = len (str_input)-total_letters-total_digits-total_whitespace12 print u: % d' % total_letters13 print U': % d' % total_digits14 print U': % d' % total_whitespace15 print U: % d' % total_othersView Code
Question:Evaluate 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.
1 # coding = utf-82 3 m = raw_input (U' the input needs to calculate the number: ') 4 n = int (raw_input (U' number of input items :')) 5 l = [] 6 for I in range (1, n + 1): 7 l. append (int (m * I) 8 print d9print sum (l)
View Code
Question:If a number is equal to the sum of its factors, this number is called "complete number ". For example, 6 = 1 + 2 + 3. Program to find all the completion numbers within 1000.
1 #coding=utf-82 3 for i in range(2,1001):4 total = 15 for j in range(2,i):6 if i % j == 0:7 total += j8 if i == total:9 print i
View Code
Question:A ball falls freely from the height of 100 meters. After each landing, the ball jumps back to half of the original height. Then, how many meters does it go through during the first landing? How high is the rebound of 10th times?
1 # coding = utf-82 3 length = 100.04 up_height = 100.05 for I in range ): 6 up_height/= 27 length + = up_height * 28 print '% d bounce height % s meters, % d landing after % s meters % (I-1, up_height, I, length)
View Code
Question:Monkey peach eating problem: the monkey picked up a few peaches on the first day, and immediately ate half of them. It was not enough, and then eat another one and eat the remaining peaches in half the next morning, I ate another one. In the future, I eat the remaining half of the previous day every morning. When you want to eat again in the morning of the seventh day, there is only one peach left. Calculate the total number of extracted items on the first day.
1 #coding=utf-82 3 def peach(n):4 if n == 1:5 return 16 else:7 return (peach(n-1) + 1) * 28 print peach(10)
View Code