Python daily two questions series (December 30)
In each question, I will use n methods to solve it, to help you broaden your thinking and practice the program
There are four numbers: 1, 2, 3 and 4. How many three digits that are different from each other and have no repetition? How many are each?
Program analysis: the numbers that can be filled in hundreds, tens and ones are all 1, 2, 3 and 4. Make up all permutations before removing the ones that do not meet the conditions.
Program source code:
for i in range(1,5): for j in range(1,5): for k in range(1,5): if( i != k ) and (i != j) and (j != k): #If not, enter print (i,j,k)
Simplify code: remove redundant judgment
for i in range(1, 5): for j in range(1, 5): if (j==i) : continue; for k in range(1, 5): if (k==i or k==j): continue; print(i,j,k);
Another way to write code: use list
list = [1,2,3,4] for i in list: list1 = list.copy() list1.remove(i) for j in list1: list2 = list1.copy() list2.remove(j) for k in list2: print(i, j, k)
Another way to write code: using bit operation
#From 00 01 10 to 11 10 01 for num in range(6,58): a = num >> 4 & 3 b = num >> 2 & 3 c = num & 3 if( (a^b) and (b^c) and (c^a) ): print(a+1,b+1,c+1)
Further simplify the writing method: use list derivation, take away ← in a sentence, that's how it is
[(x,y,z) for x in range(1,5) for y in range(1,5) for z in range(1,5) if(x!=y)and(x!=z)and(y!=z)]
The bonus paid by the enterprise is based on the profit commission. When the profit is less than or equal to RMB 100000, the bonus can be increased by 10%; when the profit is higher than RMB 100000, the part lower than RMB 100000 will be increased by 10%; when the profit is higher than RMB 100000, the bonus can be increased by 7.5%; when the profit is between RMB 200000 and RMB 400000, the bonus can be increased by 5%; when the profit is between RMB 400000 and RMB 600000, the bonus can be increased by 3%; when the profit is between RMB 600000 and RMB 1000000, the bonus can be increased by 3%; when the profit is between RMB 600000 and RMB 1000000, the bonus can be increased by 7.5%; when the profit is between RMB 200000 and RMB 400000 Score: 1.5%; if it is higher than 1 million yuan, the part exceeding 1 million yuan will be charged as 1%. Input the profit I of the month from the keyboard, and calculate the total amount of bonus to be paid?
Topic analysis: the profit of this topic has multiple ranges. Naturally, the first idea is to take it away with a nested loop
def get_reward(i): rewards = 0 if i <= 10: rewards = i * 0.1 elif (i > 10) and (i <= 20): rewards = (i - 10) * 0.075 + get_reward(10) elif (i > 20) and (i <= 40): rewards = (i - 20) * 0.05 + get_reward(20) elif (i > 40) and (i <= 60): rewards = (i - 40) * 0.03 + get_reward(40) elif (I > 60) and (I <= 100): rewards = (i - 60) * 0.015 + get_reward(60) else: rewards = get_reward(100) + (i - 100) * 0.01 return rewards
But do you think nested loops are a little troublesome
Let's take a closer look at the conditions:
When i ≤ 10w, the commission rate can be directly multiplied by the profit. When i > 10w, the excess is multiplied by the commission rate
i = int(input('Net profit:')) arr = [1000000,600000,400000,200000,100000,0] rat = [0.01,0.015,0.03,0.05,0.075,0.1] r = 0 for idx in range(0,6): if i>arr[idx]: r+=(i-arr[idx])*rat[idx] print((i-arr[idx])*rat[idx]) i=arr[idx] print(r)
We can also use the two-dimensional list to complete a more concise writing method:
Bonus = 0; BonusRateList = [[100,0.010],[60,0.015],[40,0.030],[20,0.050],[10,0.075],[0,0.100]]; Profit = 120000; Profit /= 10000; for i in range(0, len(BonusRateList)) : if (Profit > BonusRateList[i]) : Bonus += ((Profit - BonusRateList[i]) * BonusRateList[i]); Profit = BonusRateList[i]; print (Bonus * 10000);