python Exercise Questions 1-10

Posted by ivalea on Sat, 15 Jun 2019 20:51:58 +0200

1. There are four digits: 1, 2, 3 and 4. How many different three digits can be formed without repeating numbers? How much is each?
2. The bonus paid by the enterprise shall be deducted on the basis of profit. When the profit is less than or equal to 100,000 yuan, the bonus can be increased by 10%; when the profit is higher than 100,000 yuan, or less than 200,000 yuan, the part less than 100,000 yuan will be deducted by 10%, and the part higher than 100,000 yuan will be deducted by 75%; when the profit is between 200,000 and 400,000 yuan, the part higher than 200,000 yuan will be deducted by 5%; when the profit is between 400,000 and 600,000 yuan, the part higher than 400,000 yuan will be deducted by 3%; when the profit is between 600,000 and 1,000 yuan Part I can be deducted by 1.5%. When it is over 1 million yuan, the part over 1 million yuan will be deducted by 1%. If the profit I is input from the keyboard, the total amount of bonus should be paid?
3. An integer, which is a complete square after adding 100, and 168 is a complete square. What is the number, please?
4. Enter the date of a certain year, a certain month and a certain day. Judge that day is the date of the year?
5. Enter three integers x,y,z. Please output the three integers from small to large.
6. Fibonacci sequence.
7. Copy data from one list to another.
8. Output 9*9 multiplication tables.
9. Pause output for one second.
10. Pause the output for one second and format the current time.

1. There are four digits: 1, 2, 3 and 4. How many different three digits can be formed without repeating numbers? How much is each?
Method 1:
1 l = []
2 for i in range(1,5):
3     for j in range(1,5):
4         for m in range(1,5):
5             if len({i,j,m}) == 3:
6                 l.append(i * 100 + j * 10 + m)
7 print(l)
8 print(len(l))
1 [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]
2 24
Method 2:
1 from itertools import permutations
2 
3 l = []
4 for i in permutations([1, 2, 3, 4], 3):
5     l.append(i)
6 
7 print(l)
8 print(len(l))
1 [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
2 24

2. The bonus paid by the enterprise shall be deducted on the basis of profit. When the profit is less than or equal to 100,000 yuan, the bonus can be increased by 10%; when the profit is higher than 100,000 yuan, or less than 200,000 yuan, the part less than 100,000 yuan will be deducted by 10%, and the part higher than 100,000 yuan will be deducted by 75%; when the profit is between 200,000 and 400,000 yuan, the part higher than 200,000 yuan will be deducted by 5%; when the profit is between 400,000 and 600,000 yuan, the part higher than 400,000 yuan will be deducted by 3%; when the profit is between 600,000 and 1,000 yuan Part I can be deducted by 1.5%. When it is over 1 million yuan, the part over 1 million yuan will be deducted by 1%. If the profit I is input from the keyboard, the total amount of bonus should be paid?
Method 1:
 1 # That's the weakest way, but that's what I'm thinking of...
 2 a = [1000000,600000,400000,200000,100000,0]
 3 b = [0.01,0.015,0.03,0.05,0.075,0.1]
 4 x = int(input('Sales profits are:'))
 5 
 6 if x >= a[0]:
 7     bonus = (x - a[0]) * b[0] + 400000 * b[1] + 200000 * b[2] + 200000 * b[3] + 100000 * b[4] + 100000 * b[5]
 8     print('The Commission is:',bonus)
 9 elif a[0] > x >= a[1]:
10     bonus = (x - a[1]) * b[1] + 200000 * b[2] + 200000 * b[3] + 100000 * b[4] + 100000 * b[5]
11     print('The Commission is:', bonus)
12 elif a[1] > x >= a[2]:
13     bonus = (x - a[2]) * b[2] + 200000 * b[3] + 100000 * b[4] + 100000 * b[5]
14     print('The Commission is:', bonus)
15 elif a[2] > x >= a[3]:
16     bonus = (x - a[3]) * b[3] + 100000 * b[4] + 100000 * b[5]
17     print('The Commission is:', bonus)
18 elif a[3] > x >= a[4]:
19     bonus = (x - a[4]) * b[4] + 100000 * b[5]
20     print('The Commission is:', bonus)
21 elif a[4] > x >= a[5]:
22     bonus = (x - a[5]) * b[5]
23     print('The Commission is:', bonus)
24 elif x < 0:
25     print('x Must be greater than 0')
1 Sales Profit: 200001
 2 commission: 17500.05
Method 2:
 1 # Reference to others
 2 a = [1000000, 600000, 400000, 200000, 100000, 0]
 3 b = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1]
 4 count = 0
 5 while count < 5:
 6     x = int(input('Sales profits are:'))
 7     sum = 0
 8     for n in range(0,6):
 9         if x > a[n]:
10             tmp = (x-a[n])*b[n] # Calculate the level of contribution
11             sum += tmp
12             x = a[n]  # Full Commission is calculated at each subsequent level
13     print(sum)
14     count += 1

 

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

Method 1:

import math

x = -99  # This value comes to mind when you look back at the answers of others. x+100 Must be greater than 0
while x < 10000:
    if math.pow(int(math.sqrt(x+100)),2) == x+100:
        if math.pow(int(math.sqrt(x + 100 + 168)), 2) == x + 100 + 168:  # Simple column equation, who won't... mdzz
# print(x) x += 1

Method 2: Looking at other people's ideas, I think I'm just mentally retarded.

'''
1,Then: x + 100 = n2, x + 100 + 168 = m2
2,Computational Equation: m2 - n2 = (m + n)(m - n) = 168
3,Set up: m + n = i,m - n = j,i * j =168,i and j At least one is even.
4,Available: m = (i + j) / 2, n = (i - j) / 2,i and j Either even or odd.
5,Derivation from 3 and 4 shows that, i and j They are even numbers greater than or equal to 2.
6,Because i * j = 168, j>=2,Then 1 < i < 168 / 2 + 1. 
7,Next, the i All digital cyclic calculations are enough.
'''

i = 1
#Find the maximum range
while ((i+1)*(i+1)-i*i) <= 168:
      i += 1
#Cyclic testing and printing
for j in range(1,i):
    for k in range(1,i):
        if (k*k - j*j) == 168:
            print(k*k-268)

 

4. Enter the date of a certain year, a certain month and a certain day. Judge that day is the date of the year?

Method 1:

import datetime

Y = int(input('Year:'))
m = int(input('Month:'))
d = int(input('Day:'))

days = datetime.datetime(Y,m,d) - datetime.datetime(Y,1,1) + datetime.timedelta(1)  # Minus January 1 of that year
n = int(str(days).split(' ')[0])
print(n)


//Year:2003
//Month:12
//Day:2
336

Method 2:

import time
# D=input("Please enter the year in the form of XXXX-XX-XX: ")
D='2017-4-3'
d=time.strptime( D,'%Y-%m-%d').tm_yday
# print("the {} day of this year!" .format(d))
print(d)
print(time.strptime( D,'%Y-%m-%d'))



# Or use datetime Modular
import datetime
print(datetime.date(2017,4,3).timetuple().tm_yday)
print(datetime.date(2017,4,3).timetuple())
93
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=93, tm_isdst=-1)
93
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=93, tm_isdst=-1)

 

5. Enter three integers x,y,z. Please output the three integers from small to large.

Method 1

L = [a,b,c]
L.sort()
print('\n'.join(l))

Method 2:

 1 # The most retarded approach
 2 # a = input('a=')
 3 # b = input('b=')
 4 # c = input('c=')
 5 a = 10
 6 b = 20
 7 c = 30
 8 
 9 l = []
10 
11 l.append(a)
12 if b < a:
13     l.insert(0,b)
14     if c < b:
15         l.insert(0,c)
16     elif c > a:
17         l.append(c)
18     else:
19         l.insert(1,c)
20 else:
21     l.append(b)
22     if c < a:
23         l.insert(0,c)
24     elif c > b:
25         l.append(c)
26     else:
27         l.insert(1,c)
28 
29 print('\n'.join(l))

Method 3:

# bubble sorting
a=[1,3,5,2,4,5,7]

n=len(a)

for i in range(0,n):
  for j in range(i,n) :
     if (a[i] >= a[j] ):
        a[i],a[j] = a[j],a[i]

print(a)
[1, 2, 3, 4, 5, 5, 7]

6. Fibonacci sequence.
Method 1:
l = [0,1]

def f(n):
    for x in range(n):
        l.append(l[x]+l[x+1])

f(20)
print(l)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]

Method 2:

# Using recursion
def fib(n):
    if n == 1 or n == 2:
        return 1
    return fib(n - 1) + fib(n - 2)


# Output of the 10th Fibonacci sequence
print(fib(20))

7. Copy data from one list to another.
a = list(range(20))
b = a[:]

print(a)
print(b)
print(id(a))
print(id(b))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
2030007944584
2030007104200

8. Output 9*9 multiplication tables.
 1 # First Baidu multiplication formula table look like, how to arrange
 2 '''
 3 The multiplication formula is as follows:
 4 1*1=1
 5 2*1=2 2*2=4
 6 3*1=3 3*2=6 3*3=9
 7 4*1=4 4*2=8 4*3=12 4*4=16
 8 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
 9 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
10 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
11 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
12 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
13 '''
14 formula = '{a} * {b} = {c}'
15 
16 s = ''
17 for i in range(1,10):
18     print(s)
19     s = ''
20     for j in range(1,i+1):
21         s += formula.format(a=i,b=j,c=i*j) + ' '
22         # print(formula.format(a=i,b=j,c=i*j))
1 * 1 = 1 
2 * 1 = 2 2 * 2 = 4 
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 

9. Pause output for one second.
import time


for n in range(1,10):
    print(n)
    time.sleep(1)

10. Pause the output for one second and format the current time.
import time

for i in range(5):
    struct_time = time.localtime(time.time())
    t = time.strftime('%Y-%m-%d %H:%M:%S',struct_time)
    t = time.strftime('%Y-%m-%d',struct_time)
    print(t)
    time.sleep(1)

Topics: Python less