P ython p rogramming e xercises


Alternate Solution Design



Yüklə 1,51 Mb.
Pdf görüntüsü
səhifə31/124
tarix14.05.2023
ölçüsü1,51 Mb.
#113537
1   ...   27   28   29   30   31   32   33   34   ...   124
PythonProgrammingExercisesGentlyExplained

Alternate Solution Design 
Alternatively, instead of subtractions in a loop you can use Python’s // integer division operator to 
see how many 3600 amounts (for hours) or 60 amounts (for minutes) fit into totalSeconds. To get 
the remaining amount in totalSeconds after removing the seconds accounted for hours and 
minutes, you can use Python’s % modulo operator. 
For example, if totalSeconds were 10000, then 10000 // 3600 evaluates to 2, telling you 
that there are two hours in 10,000 seconds. Then 10000 % 3600 evaluates to 2800, telling you 
there are 2,800 seconds left over after removing the two hours from 10,000 seconds. You would then 
run 2800 // 60 to find the number of minutes in 2,800 seconds and 2800 % 60 to find the 
number of seconds remaining after removing those minutes. 
Alternate Solution Template 
Try to first write a solution from scratch. But if you have difficulty, you can use the following 
partial program as a starting place. Copy the following code from https://invpy.com/hoursminutesseconds2-
template.py and paste it into your code editor. Replace the underscores with code to make a working 
program: 
def getHoursMinutesSeconds(totalSeconds): 


Python Programming Exercises, Gently Explained 
37 
# If totalSeconds is 0, just return '0s': 
if totalSeconds == ____: 
return ____ 
# Set hours to how many times 3600 seconds can divide 
# totalSeconds. Then set totalSeconds to the remainder: 
if totalSeconds >= 3600: 
hours = totalSeconds // 3600 
totalSeconds = totalSeconds % 3600 
else: 
hours = 0 
# Set minutes to how many times 60 seconds can divide 
# totalSeconds. Then set totalSeconds to the remainder: 
if totalSeconds >= 60: 
minutes = totalSeconds // 60 
totalSeconds = totalSeconds % 60 
else: 
minutes = 0 
# Set seconds to the remaining totalSeconds value: 
seconds = ____ 
# Create an hms list that contains the string hour/minute/second amounts: 
hms = [] 
# If there are one or more hours, add the amount with an 'h' suffix: 
if hours > ____: 
____.append(str(____) + 'h') 
# If there are one or more minutes, add the amount with an 'm' suffix: 
if minutes > 0: 
hms.append(____(minutes) + 'm') 
# If there are one or more seconds, add the amount with an 's' suffix: 
if seconds > 0: 
hms.append(str(seconds) + ____) 
# Join the hour/minute/second strings with a space in between them: 
return ' '.join(____) 
The complete solution for this exercise is given in Appendix A and 
https://invpy.com/hoursminutesseconds2.py. You can view each step of this program as it runs under a 
debugger at https://invpy.com/hoursminutesseconds2-debug/


38 

Yüklə 1,51 Mb.

Dostları ilə paylaş:
1   ...   27   28   29   30   31   32   33   34   ...   124




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2025
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin