X Tutup
Skip to content

Commit 4d2d470

Browse files
committed
Updating code
1 parent c8f440a commit 4d2d470

17 files changed

+291
-408
lines changed

code/Time1.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
"""
1+
"""This module contains a code example related to
22
3-
Code example from Think Python, by Allen B. Downey.
4-
Available from http://thinkpython.com
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
56
6-
Copyright 2012 Allen B. Downey.
7-
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
7+
Copyright 2015 Allen Downey
88
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

12+
from __future__ import print_function, division
13+
14+
1115
class Time(object):
1216
"""Represents the time of day.
1317
1418
attributes: hour, minute, second
1519
"""
1620

21+
1722
def print_time(t):
18-
print '%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second)
23+
"""Prints a string representation of the time.
24+
25+
t: Time object
26+
"""
27+
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
1928

2029

2130
def int_to_time(seconds):
@@ -40,14 +49,24 @@ def time_to_int(time):
4049

4150

4251
def add_times(t1, t2):
43-
"""Adds two time objects."""
52+
"""Adds two time objects.
53+
54+
t1, t2: Time
55+
56+
returns: Time
57+
"""
4458
assert valid_time(t1) and valid_time(t2)
4559
seconds = time_to_int(t1) + time_to_int(t2)
4660
return int_to_time(seconds)
4761

4862

4963
def valid_time(time):
50-
"""Checks whether a Time object satisfies the invariants."""
64+
"""Checks whether a Time object satisfies the invariants.
65+
66+
time: Time
67+
68+
returns: boolean
69+
"""
5170
if time.hour < 0 or time.minute < 0 or time.second < 0:
5271
return False
5372
if time.minute >= 60 or time.second >= 60:
@@ -62,19 +81,20 @@ def main():
6281
noon_time.minute = 0
6382
noon_time.second = 0
6483

65-
print 'Starts at',
84+
print('Starts at', end=' ')
6685
print_time(noon_time)
6786

6887
# and the run time of the movie is 109 minutes...
6988
movie_minutes = 109
7089
run_time = int_to_time(movie_minutes * 60)
71-
print 'Run time',
90+
print('Run time', end=' ')
7291
print_time(run_time)
7392

7493
# what time does the movie end?
7594
end_time = add_times(noon_time, run_time)
76-
print 'Ends at',
95+
print('Ends at', end=' ')
7796
print_time(end_time)
7897

98+
7999
if __name__ == '__main__':
80100
main()

code/Time1_soln.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
"""
1+
"""This module contains a code example related to
22
3-
Code example from Think Python, by Allen B. Downey.
4-
Available from http://thinkpython.com
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
56
6-
Copyright 2012 Allen B. Downey.
7-
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
7+
Copyright 2015 Allen Downey
88
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

12+
from __future__ import print_function, division
13+
14+
1115
from datetime import datetime
1216

1317
# to avoid duplicating code, I'm importing everything from Time1
@@ -56,27 +60,29 @@ def double_day(b1, b2):
5660
"""
5761
assert b1 > b2
5862
delta = b1 - b2
59-
double_day = b1 + delta
60-
return double_day
63+
dday = b1 + delta
64+
return dday
6165

6266

6367
def datetime_exercises():
68+
"""Exercise solutions."""
69+
6470
# print today's day of the week
6571
today = datetime.today()
66-
print today.weekday()
67-
print today.strftime('%A')
72+
print(today.weekday())
73+
print(today.strftime('%A'))
6874

6975
# compute the number of days until the next birthday
7076
# (note that it usually gets rounded down)
7177
birthday = datetime(1967, 5, 2)
72-
print 'Days until birthday',
73-
print days_until_birthday(birthday)
78+
print('Days until birthday', end=' ')
79+
print(days_until_birthday(birthday))
7480

7581
# compute the day one person is twice as old as another
7682
b1 = datetime(2006, 12, 26)
7783
b2 = datetime(2003, 10, 11)
78-
print 'Double Day',
79-
print double_day(b1, b2)
84+
print('Double Day', end=' ')
85+
print(double_day(b1, b2))
8086

8187

8288
def main():
@@ -86,24 +92,24 @@ def main():
8692
noon_time.minute = 0
8793
noon_time.second = 0
8894

89-
print 'Starts at',
95+
print('Starts at', end=' ')
9096
print_time(noon_time)
9197

9298
# and the run time of the movie is 109 minutes...
9399
movie_minutes = 109
94100
run_time = int_to_time(movie_minutes * 60)
95-
print 'Run time',
101+
print('Run time', end=' ')
96102
print_time(run_time)
97103

98104
# what time does the movie end?
99105
end_time = add_times(noon_time, run_time)
100-
print 'Ends at',
106+
print('Ends at', end=' ')
101107
print_time(end_time)
102108

103-
print 'Does it end after it begins?',
104-
print is_after(end_time, noon_time)
109+
print('Does it end after it begins?', end=' ')
110+
print(is_after(end_time, noon_time))
105111

106-
print 'Home by',
112+
print('Home by', end=' ')
107113
travel_time = 600 # 10 minutes
108114
home_time = increment(end_time, travel_time)
109115
print_time(home_time)
@@ -113,13 +119,13 @@ def main():
113119
race_time.minute = 34
114120
race_time.second = 05
115121

116-
print 'Half marathon time',
122+
print('Half marathon time', end=' ')
117123
print_time(race_time)
118124

119125
distance = 13.1 # miles
120126
pace = mul_time(race_time, 1/distance)
121127

122-
print 'Time per mile',
128+
print('Time per mile', end=' ')
123129
print_time(pace)
124130

125131
datetime_exercises()

code/Time2.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
"""
1+
"""This module contains a code example related to
22
3-
Code example from Think Python, by Allen B. Downey.
4-
Available from http://thinkpython.com
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
56
6-
Copyright 2012 Allen B. Downey.
7-
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
7+
Copyright 2015 Allen Downey
88
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

12+
from __future__ import print_function, division
13+
14+
1115
class Time(object):
1216
"""Represents the time of day.
1317
1418
attributes: hour, minute, second
1519
"""
1620
def __init__(self, hour=0, minute=0, second=0):
21+
"""Initializes a time object.
22+
23+
hour: int
24+
minute: int
25+
second: int or float
26+
"""
1727
self.hour = hour
1828
self.minute = minute
1929
self.second = second
2030

2131
def __str__(self):
32+
"""Returns a string representation of the time."""
2233
return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
2334

2435
def print_time(self):
25-
print str(self)
36+
"""Prints a string representation of the time."""
37+
print(str(self))
2638

2739
def time_to_int(self):
2840
"""Computes the number of seconds since midnight."""
@@ -86,24 +98,24 @@ def main():
8698
end = start.increment(1337)
8799
end.print_time()
88100

89-
print 'Is end after start?',
90-
print end.is_after(start)
101+
print('Is end after start?')
102+
print(end.is_after(start))
91103

92-
print 'Using __str__'
93-
print start, end
104+
print('Using __str__')
105+
print(start, end)
94106

95107
start = Time(9, 45)
96108
duration = Time(1, 35)
97-
print start + duration
98-
print start + 1337
99-
print 1337 + start
109+
print(start + duration)
110+
print(start + 1337)
111+
print(1337 + start)
100112

101-
print 'Example of polymorphism'
113+
print('Example of polymorphism')
102114
t1 = Time(7, 43)
103115
t2 = Time(7, 41)
104116
t3 = Time(7, 37)
105117
total = sum([t1, t2, t3])
106-
print total
118+
print(total)
107119

108120

109121
if __name__ == '__main__':

code/Time2_soln.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
"""
1+
"""This module contains a code example related to
22
3-
Code example from Think Python, by Allen B. Downey.
4-
Available from http://thinkpython.com
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
56
6-
Copyright 2012 Allen B. Downey.
7-
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
7+
Copyright 2015 Allen Downey
88
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

12+
from __future__ import print_function, division
13+
14+
1115
class Time(object):
1216
"""Represents the time of day.
1317
1418
attributes: hour, minute, second
1519
"""
1620
def __init__(self, hour=0, minute=0, second=0):
21+
"""Initializes a time object.
22+
23+
hour: int
24+
minute: int
25+
second: int or float
26+
"""
1727
minutes = hour * 60 + minute
1828
self.seconds = minutes * 60 + second
1929

2030
def __str__(self):
31+
"""Returns a string representation of the time."""
2132
minutes, second = divmod(self.seconds, 60)
2233
hour, minute = divmod(minutes, 60)
2334
return '%.2d:%.2d:%.2d' % (hour, minute, second)
2435

2536
def print_time(self):
26-
print str(self)
37+
"""Prints a string representation of the time."""
38+
print(str(self))
2739

2840
def time_to_int(self):
2941
"""Computes the number of seconds since midnight."""
@@ -78,24 +90,24 @@ def main():
7890
end = start.increment(1337)
7991
end.print_time()
8092

81-
print 'Is end after start?',
82-
print end.is_after(start)
93+
print('Is end after start?')
94+
print(end.is_after(start))
8395

84-
print 'Using __str__'
85-
print start, end
96+
print('Using __str__')
97+
print(start, end)
8698

8799
start = Time(9, 45)
88100
duration = Time(1, 35)
89-
print start + duration
90-
print start + 1337
91-
print 1337 + start
101+
print(start + duration)
102+
print(start + 1337)
103+
print(1337 + start)
92104

93-
print 'Example of polymorphism'
105+
print('Example of polymorphism')
94106
t1 = Time(7, 43)
95107
t2 = Time(7, 41)
96108
t3 = Time(7, 37)
97109
total = sum([t1, t2, t3])
98-
print total
110+
print(total)
99111

100112

101113
if __name__ == '__main__':

0 commit comments

Comments
 (0)
X Tutup