Weekly Python Practice: Selected Problems (Twenty-Six)

Problem 1: TimeProblem Description:This problem requires writing a program to output the time value after n seconds from a given time in the format hh:mm:ss (if it exceeds 23:59:59, it starts counting from 0:00).Input Format:The first line contains the starting time in the format hh:mm:ss, and the second line gives the integer number nn (<60<60).Output Format:The output should be a single line with the result time in hh:mm:ss format.Input Example:11:59:4030Output Example:12:00:10Analysis:1. Read the first line of input, which is the starting time string in the format “hh:mm:ss”. Use the split method to separate it by colons and convert hours, minutes, and seconds to integers.2. Read the second line of input, which is the integer number n.3. Convert the starting time to total seconds: hours * 3600 + minutes * 60 + seconds.4. Add n to the total seconds to get the new total seconds.5. Since it starts counting from 0:00 after exceeding 23:59:59, take the total seconds modulo 86400 (the number of seconds in 24 hours) to get the valid seconds.6. Convert the valid seconds back to hours, minutes, and seconds: hours = total seconds // 3600, minutes = (total seconds % 3600) // 60, seconds = total seconds % 60.7. Use formatted output to ensure that hours, minutes, and seconds are two digits, padding with zeros if necessary.Reference Code

# Read starting time
time_str = input().strip()
# Read integer seconds
n = int(input().strip())
# Split time string to get hours, minutes, seconds
parts = time_str.split(':')
h = int(parts[0])
m = int(parts[1])
s = int(parts[2])
# Convert starting time to total seconds
total_seconds = h * 3600 + m * 60 + s
# Add n seconds
total_seconds += n
# Modulo to handle overflow
total_seconds %= 24 * 3600  # 24*3600=86400
# Calculate new hours, minutes, seconds
new_h = total_seconds // 3600
new_m = (total_seconds % 3600) // 60
new_s = total_seconds % 60
# Formatted output to ensure two digits
print("{:02d}:{:02d}:{:02d}".format(new_h, new_m, new_s))

Problem 2: Personal Income TaxProblem Description:Assuming the personal income tax is: tax rate × (salary – 1600). Please write a program to calculate the income tax payable, where the tax rate is defined as follows:When the salary does not exceed 1600, the tax rate is 0;When the salary is in the range (1600, 2500), the tax rate is 5%;When the salary is in the range (2500, 3500), the tax rate is 10%;When the salary is in the range (3500, 4500), the tax rate is 15%;When the salary exceeds 4500, the tax rate is 20%.Input Format:The input is a single line containing a non-negative salary.Output Format:The output should be the personal income tax, accurate to two decimal places.Input Example 1:1600Output Example 1:0.00Input Example 2:1601Output Example 2:0.05Analysis:1. Read the input salary, which is a non-negative number.2. Determine the tax rate based on the salary range:If the salary does not exceed 1600, the tax rate is 0.If the salary is in (1600, 2500], the tax rate is 5%.If the salary is in (2500, 3500], the tax rate is 10%.If the salary is in (3500, 4500], the tax rate is 15%.If the salary exceeds 4500, the tax rate is 20%.3. Calculate the income tax: tax rate × (salary – 1600). Note that when the salary does not exceed 1600, it is directly 0.4. Output the income tax, keeping two decimal places.Reference Code

# Read salary
salary = float(input().strip())
# Calculate income tax based on salary range
if salary <= 1600:
    tax = 0.0
elif salary <= 2500:
    tax = 0.05 * (salary - 1600)
elif salary <= 3500:
    tax = 0.10 * (salary - 1600)
elif salary <= 4500:
    tax = 0.15 * (salary - 1600)
else:
    tax = 0.20 * (salary - 1600)
# Output income tax, keeping two decimal places
print("{:.2f}".format(tax))

Problem 3: Number of ArgumentsProblem Description:It is normal for a couple to argue, but it is not good to keep a record of it. You should write a program to automatically keep track…The requirement of this problem is quite simple: just count the number of arguments. The grandmother gives the couple two buttons, each time they argue, they press the red button, and when they want to know how many times they have argued, they press the green button. You will implement the functionality of these two buttons.Input Format:The input consists of a series of 0s and 1s, each number occupying a line. 1 represents the red button being pressed, and 0 represents the green button being pressed. When any number that is neither 0 nor 1 appears, it indicates that they have unplugged the power cord, and the input ends; do not process the last number.Output Format:For each input of 0, output how many times they have argued before pressing the green button on a new line.The problem guarantees that each output number does not exceed 104104.Input Example:111011012Output Example:35Analysis:1. Initialize a variable count to record the number of arguments (i.e., the number of times the red button is pressed).2. Use a loop to read the input, one number per line.3. If the input is 1, increment count by 1.4. If the input is 0, output the current count value (i.e., the number of arguments before pressing the green button).5. If the input is neither 0 nor 1, exit the loop and do not process that number.Reference Code

# Initialize argument count
count = 0
# Loop to read input
while True:
    line = input().strip()
    # If input is empty, skip (but the problem does not have empty lines, so can be ignored)
    if line == '':
        continue
    # Try to convert to integer
    try:
        num = int(line)
    except:
        # If conversion fails, skip (but the problem input is numbers, so may not be needed)
        continue
    if num == 1:
        # Red button: increment argument count
        count += 1
    elif num == 0:
        # Green button: output current argument count
        print(count)
    else:
        # Neither 0 nor 1, end input
        break

Weekly Python Practice: Selected Problems (Twenty-Six)

Leave a Comment