Newbie Tutorial-Practice Example Answer I

Title: There are four numbers: 1, 2, 3, 4. How many different three digits can be composed without repeating numbers?What is each?

1 #coding=utf-8
2 
3 from itertools import permutations
4 
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)

 

Title: Bonuses paid by enterprises are based on profits.When profit (I) is less than or equal to 100,000 yuan, the bonus may be deducted by 10%; when profit is more than 100,000 yuan, when profit is less than 200,000 yuan, the part less than 100,000 yuan is deducted by 10%, and the part higher than 100,000 yuan is deducted by 75%; between 200,000 and 400,000 yuan, the part higher than 200,000 yuan is deducted by 5%; between 400,000 and 600,000 yuan is deducted by 3%; between 600,000 and 100,000 yuan is deducted by more than 600,000 yuan.Part, which can be deducted by 1.5%, is more than 1 million yuan, and part over 1 million yuan is deducted by 1%. Enter profit I for the month from the keyboard and ask for the total bonus amount?

 1 #coding=utf-8
 2 
 3 i = int(raw_input(u'Please enter profit:'))
 4 earn = [1000000,600000,400000,200000,100000,0]
 5 rat = [0.01,0.015,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

 

Title: An integer, which is a complete square number after adding 100, and 168 is a complete square number. What is the number, please?

 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(1,85):
 9     if 168 % i == 0:
10         j = 168 / i
11         if (i+j) % 2 == 0 and (i-j) % 2 == 0 and i > j:
12             n = (i-j) / 2
13             x = n * n -100
14             print x

 

Title: Enter the day of a month in a year to determine the day of the year?

1 #coding=utf-8
2 
3 import time
4 
5 time_input = raw_input(u'Please enter date, format XXXX-XX-XX:')
6 d = time.strptime(time_input,'%Y-%m-%d').tm_yday
7 print u'This is the day of the year%s day' %d

 

Title: Output 9*9 multiplication trick table.

1 #coding=utf-8
2 
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 ''

 

Title: Determine how many prime numbers are between 101-200 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)    #for Not after the end of the cycle break Of i Join the list l in
11 print l
12 print len(l)

 

Title: Decomposition a positive integer into prime factors.For example, enter 90 and print out 90=2*3*3*5.

 1 #coding=utf-8
 2 
 3 dig_input = int(raw_input(u'Enter 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 / i
10             break
11 print l

 

Title: Use nesting of conditional operators to complete this problem: students with academic achievement >=90 points are represented by A, between 60-89 points by B, and below 60 points by C.

1 #coding=utf-8
2 
3 score = int(raw_input(u'Enter a score:'))
4 print 'A' if score > 89 else ('B' if score > 59 else 'C')

 

Title: Enter a line of characters to count the number of letters, spaces, numbers and other characters in English.

 1 #coding=utf-8
 2 
 3 import re,string
 4 
 5 str_input = raw_input(u'Enter 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_whitespace
12 print u'The number of letters in English is: %d' %total_letters
13 print u'The number of numbers is: %d' %total_digits
14 print u'The number of spaces is: %d' %total_whitespace
15 print u'The number of other characters is: %d' %total_others

 

Title: Value s=a+a a+a a a+a a a a a a+a a...a, where a is a number.For example, 2+22+222+2222+22222 (there are five numbers added at this time), and the addition of several numbers is controlled by the keyboard.

1 #coding=utf-8
2 
3 m = raw_input(u'Input requires a calculated 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 l
9 print sum(l)

 

Title: If a number is exactly equal to the sum of its factors, this number is called the "perfect number".For example, 6=1+2+3.Programming finds all completions within 1000.

1 #coding=utf-8
2 
3 for i in range(2,1001):
4     total = 1
5     for j in range(2,i):
6         if i % j == 0:
7             total += j
8     if i == total:
9         print i

 

Title: A ball falls freely from a height of 100 meters and bounces back half of its original height after each fall. When it falls again, how many meters did it pass in the 10th fall?How high is the 10th bounce?

1 #coding=utf-8
2 
3 length = 100.0
4 up_height = 100.0
5 for i in range(2,12):
6     up_height /= 2
7     length += up_height * 2
8     print 'No.%d The secondary bounce height is%s M, D%d Secondary Ground Crossing%s rice' %(i-1,up_height,i,length)

 

Title: The monkey eats peaches: The monkey picks several peaches on the first day, eats half of them immediately, is not addicted to it, eats half of the remaining peaches the next morning, and eats one more.Every morning afterwards, I ate half and one half of the rest of the previous day.When I wanted to eat again the morning of the 10th day, I saw there was only one peach left.Ask how many you picked on the first day.

1 #coding=utf-8
2 
3 def peach(n):
4     if n == 1:
5         return 1
6     else:
7         return (peach(n-1) + 1) * 2
8 print peach(10)

Keywords: Python less Programming REST

Added by ajcrm125 on Sun, 09 Jun 2019 20:32:35 +0300