Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
100 lines (69 loc) · 2.01 KB

File metadata and controls

100 lines (69 loc) · 2.01 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
class Time:
"""Represents the time of day.
attributes: hour, minute, second
"""
def print_time(t):
"""Prints a string representation of the time.
t: Time object
"""
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
def int_to_time(seconds):
"""Makes a new Time object.
seconds: int seconds since midnight.
"""
time = Time()
minutes, time.second = divmod(seconds, 60)
time.hour, time.minute = divmod(minutes, 60)
return time
def time_to_int(time):
"""Computes the number of seconds since midnight.
time: Time object.
"""
minutes = time.hour * 60 + time.minute
seconds = minutes * 60 + time.second
return seconds
def add_times(t1, t2):
"""Adds two time objects.
t1, t2: Time
returns: Time
"""
assert valid_time(t1) and valid_time(t2)
seconds = time_to_int(t1) + time_to_int(t2)
return int_to_time(seconds)
def valid_time(time):
"""Checks whether a Time object satisfies the invariants.
time: Time
returns: boolean
"""
if time.hour < 0 or time.minute < 0 or time.second < 0:
return False
if time.minute >= 60 or time.second >= 60:
return False
return True
def main():
# if a movie starts at noon...
noon_time = Time()
noon_time.hour = 12
noon_time.minute = 0
noon_time.second = 0
print('Starts at', end=' ')
print_time(noon_time)
# and the run time of the movie is 109 minutes...
movie_minutes = 109
run_time = int_to_time(movie_minutes * 60)
print('Run time', end=' ')
print_time(run_time)
# what time does the movie end?
end_time = add_times(noon_time, run_time)
print('Ends at', end=' ')
print_time(end_time)
if __name__ == '__main__':
main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.