X Tutup
Skip to content

Commit c8f440a

Browse files
committed
Updating code
1 parent cbc2a15 commit c8f440a

File tree

6 files changed

+117
-257
lines changed

6 files changed

+117
-257
lines changed

code/Point1.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
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 Point(object):
12-
"""Represents a point in 2-D space."""
16+
"""Represents a point in 2-D space.
17+
18+
attributes: x, y
19+
"""
1320

1421

1522
def print_point(p):
1623
"""Print a Point object in human-readable format."""
17-
print '(%g, %g)' % (p.x, p.y)
24+
print('(%g, %g)' % (p.x, p.y))
1825

1926

2027
class Rectangle(object):
@@ -25,15 +32,20 @@ class Rectangle(object):
2532

2633

2734
def find_center(rect):
28-
"""Returns a Point at the center of a Rectangle."""
35+
"""Returns a Point at the center of a Rectangle.
36+
37+
rect: Rectangle
38+
39+
returns: new Point
40+
"""
2941
p = Point()
3042
p.x = rect.corner.x + rect.width/2.0
3143
p.y = rect.corner.y + rect.height/2.0
3244
return p
3345

3446

3547
def grow_rectangle(rect, dwidth, dheight):
36-
"""Modify the Rectangle by adding to its width and height.
48+
"""Modifies the Rectangle by adding to its width and height.
3749
3850
rect: Rectangle object.
3951
dwidth: change in width (can be negative).
@@ -47,7 +59,7 @@ def main():
4759
blank = Point()
4860
blank.x = 3
4961
blank.y = 4
50-
print 'blank',
62+
print('blank', end=' ')
5163
print_point(blank)
5264

5365
box = Rectangle()
@@ -58,15 +70,15 @@ def main():
5870
box.corner.y = 0.0
5971

6072
center = find_center(box)
61-
print 'center',
73+
print('center', end=' ')
6274
print_point(center)
6375

64-
print box.width
65-
print box.height
66-
print 'grow'
76+
print(box.width)
77+
print(box.height)
78+
print('grow')
6779
grow_rectangle(box, 50, 100)
68-
print box.width
69-
print box.height
80+
print(box.width)
81+
print(box.height)
7082

7183

7284
if __name__ == '__main__':

code/Point1_soln.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
1014
import copy
1115
import math
1216

13-
# to avoid duplicating code, I'm importing everything from Point1
14-
from Point1 import *
17+
from Point1 import Point, Rectangle
1518

1619

1720
def distance_between_points(p1, p2):
18-
"""Computes the distance between two Point objects."""
21+
"""Computes the distance between two Point objects.
22+
23+
p1: Point
24+
p2: Point
25+
26+
returns: float
27+
"""
1928
dx = p1.x - p2.x
2029
dy = p1.y - p2.y
2130
dist = math.sqrt(dx**2 + dy**2)
@@ -39,6 +48,8 @@ def move_rectangle_copy(rect, dx, dy):
3948
rect: Rectangle object.
4049
dx: change in x coordinate (can be negative).
4150
dy: change in y coordinate (can be negative).
51+
52+
returns: new Rectangle
4253
"""
4354
new = copy.deepcopy(rect)
4455
move_rectangle(new, dx, dy)
@@ -54,9 +65,8 @@ def main():
5465
grosse.x = 3
5566
grosse.y = 4
5667

57-
print 'distance',
58-
print distance_between_points(grosse, blank)
59-
68+
print('distance', end=' ')
69+
print(distance_between_points(grosse, blank))
6070

6171
box = Rectangle()
6272
box.width = 100.0
@@ -65,16 +75,17 @@ def main():
6575
box.corner.x = 50.0
6676
box.corner.y = 50.0
6777

68-
print box.corner.x
69-
print box.corner.y
70-
print 'move'
78+
print(box.corner.x)
79+
print(box.corner.y)
80+
print('move')
7181
move_rectangle(box, 50, 100)
72-
print box.corner.x
73-
print box.corner.y
82+
print(box.corner.x)
83+
print(box.corner.y)
7484

7585
new_box = move_rectangle_copy(box, 50, 100)
76-
print box.corner.x
77-
print box.corner.y
86+
print(new_box.corner.x)
87+
print(new_box.corner.y)
88+
7889

7990
if __name__ == '__main__':
8091
main()

code/Poker.py

Lines changed: 0 additions & 184 deletions
This file was deleted.

code/PokerHand.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
from Card import *
12+
from __future__ import print_function, division
13+
14+
from Card import Hand, Deck
1115

1216

1317
class PokerHand(Hand):
18+
"""Represents a poker hand."""
1419

1520
def suit_hist(self):
1621
"""Builds a histogram of the suits that appear in the hand.
@@ -43,7 +48,7 @@ def has_flush(self):
4348
hand = PokerHand()
4449
deck.move_cards(hand, 7)
4550
hand.sort()
46-
print hand
47-
print hand.has_flush()
48-
print ''
51+
print(hand)
52+
print(hand.has_flush())
53+
print('')
4954

0 commit comments

Comments
 (0)
X Tutup