Author: RichardFu123
https://github.com/RichardFu123/Python100Cases
Example 001: Number Combinations
Problem: There are four numbers: 1, 2, 3, 4. How many different three-digit numbers can be formed without repeating digits? What are they?
Program Analysis: Traverse all possibilities and eliminate duplicates.
total=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if ((i!=j)and(j!=k)and(k!=i)):
print(i,j,k)
total+=1
print(total)
Simple Method: Use permutations from itertools.
import itertools
sum2=0
a=[1,2,3,4]
for i in itertools.permutations(a,3):
print(i)
sum2+=1
print(sum2)
Example 002: Income Tax Calculation
Problem: The bonuses given by a company are based on profits. If the profit (I) is less than or equal to 100,000, the bonus can be 10%; if the profit is more than 100,000 but less than 200,000, the bonus for the portion below 100,000 is 10%, and for the portion above 100,000, it can be 7.5%; for profits between 200,000 and 400,000, the portion above 200,000 can be 5%; for profits between 400,000 and 600,000, the portion above 400,000 can be 3%; for profits between 600,000 and 1,000,000, the portion above 600,000 can be 1.5%; for profits above 1,000,000, the portion exceeding 1,000,000 can be 1%. Given the profit I for the month input from the keyboard, calculate the total bonus to be paid out.
Program Analysis: Calculate based on intervals.
profit=int(input('Show me the money: '))
bonus=0
thresholds=[100000,100000,200000,200000,400000]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
for i in range(len(thresholds)):
if profit<=thresholds[i]:
bonus+=profit*rates[i]
profit=0
break
else:
bonus+=thresholds[i]*rates[i]
profit-=thresholds[i]
bonus+=profit*rates[-1]
print(bonus)
Example 003: Perfect Squares
Problem: An integer, when added to 100, becomes a perfect square, and when added to 168, also becomes a perfect square. What is the number?
Program Analysis: Since 168 is too small for exponential growth, we can directly skip mathematical analysis and use the simplest method to get the upper limit:
n=0
while (n+1)**2-n*n<=168:
n+=1
print(n+1)
----------
85
The idea is: the worst case is that the square of n and the square of (n+1) differ by exactly 168, and due to the square relationship, there cannot be a larger gap than this.
As for judging whether it is a perfect square, the simplest method is: the decimal value of the square root is 0.
Combining these:
n=0
while (n+1)**2-n*n<=168:
n+=1
for i in range((n+1)**2):
if i**0.5==int(i**0.5) and (i+168)**0.5==int((i+168)**0.5):
print(i-100)
Example 004: Day of the Year
Problem: Input a certain year, month, and day, and determine which day of the year it is.
Program Analysis: Special cases need to consider adding one day in February during leap years:
def isLeapYear(y):
return (y%400==0 or (y%4==0 and y%100!=0))
DofM=[0,31,28,31,30,31,30,31,31,30,31,30]
res=0
year=int(input('Year:'))
month=int(input('Month:'))
day=int(input('day:'))
if isLeapYear(year):
DofM[2]+=1
for i in range(month):
res+=DofM[i]
print(res+day)
Example 005: Sorting Three Numbers
Problem: Input three integers x, y, z, and output these three numbers in ascending order.
Program Analysis: Just practice sorting algorithms, or directly call functions.
raw=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw.append(x)
for i in range(len(raw)):
for j in range(i,len(raw)):
if raw[i]>raw[j]:
raw[i],raw[j]=raw[j],raw[i]
print(raw)
raw2=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw2.append(x)
print(sorted(raw2))