FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ThinkRubyBuild/code/Time1.py at master · learnbyexample/ThinkRubyBuild · GitHub
learnbyexample
/
ThinkRubyBuild
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
32
Code
Issues
7
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
ThinkRubyBuild
/
code
/
Time1.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
100 lines (69 loc) · 2.01 KB
Breadcrumbs
ThinkRubyBuild
/
code
/
Time1.py
Copy path
File metadata and controls
100 lines (69 loc) · 2.01 KB
Raw
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
()
Back
|
FazBrowse Home
|
New Git URL