Skip to content

Commit dfc9a95

Browse files
committed
Updating code
1 parent 79d61d9 commit dfc9a95

11 files changed

+215
-400
lines changed

code/Card.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
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 random
1115

1216

@@ -50,24 +54,34 @@ class Deck(object):
5054
"""
5155

5256
def __init__(self):
57+
"""Initializes the Deck with 52 cards.
58+
"""
5359
self.cards = []
5460
for suit in range(4):
5561
for rank in range(1, 14):
5662
card = Card(suit, rank)
5763
self.cards.append(card)
5864

5965
def __str__(self):
66+
"""Returns a string representation of the deck.
67+
"""
6068
res = []
6169
for card in self.cards:
6270
res.append(str(card))
6371
return '\n'.join(res)
6472

6573
def add_card(self, card):
66-
"""Adds a card to the deck."""
74+
"""Adds a card to the deck.
75+
76+
card: Card
77+
"""
6778
self.cards.append(card)
6879

6980
def remove_card(self, card):
70-
"""Removes a card from the deck."""
81+
"""Removes a card from the deck or raises exception if it is not there.
82+
83+
card: Card
84+
"""
7185
self.cards.remove(card)
7286

7387
def pop_card(self, i=-1):
@@ -122,8 +136,8 @@ def find_defining_class(obj, method_name):
122136
deck.shuffle()
123137

124138
hand = Hand()
125-
print find_defining_class(hand, 'shuffle')
139+
print(find_defining_class(hand, 'shuffle'))
126140

127141
deck.move_cards(hand, 5)
128142
hand.sort()
129-
print hand
143+
print(hand)

code/button_demo.py

-23
This file was deleted.

code/canvas_demo.py

-34
This file was deleted.

code/cartalk1.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
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
8-
http://www.cartalk.com/content/puzzler/transcripts/200725
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

12+
from __future__ import print_function, division
13+
14+
1115
def is_triple_double(word):
12-
"""Tests if a word contains three consecutive double letters."""
16+
"""Tests if a word contains three consecutive double letters.
17+
18+
word: string
19+
20+
returns: bool
21+
"""
1322
i = 0
1423
count = 0
1524
while i < len(word)-1:
@@ -30,12 +39,12 @@ def find_triple_double():
3039
for line in fin:
3140
word = line.strip()
3241
if is_triple_double(word):
33-
print word
42+
print(word)
3443

3544

36-
print 'Here are all the words in the list that have'
37-
print 'three consecutive double letters.'
45+
print('Here are all the words in the list that have')
46+
print('three consecutive double letters.')
3847
find_triple_double()
39-
print ''
48+
print('')
4049

4150

code/cartalk2.py

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
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
8-
http://www.cartalk.com/content/puzzler/transcripts/200803
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

11-
def has_palindrome(i, start, len):
12-
"""return True if the integer i, when written as a string,
13-
contains a palindrome with length (len), starting at index (start).
12+
from __future__ import print_function, division
13+
14+
15+
def has_palindrome(i, start, length):
16+
"""Checks if the string representation of i has a palindrome.
17+
18+
i: integer
19+
start: where in the string to start
20+
length: length of the palindrome to check for
1421
"""
15-
s = str(i)[start:start+len]
22+
s = str(i)[start:start+length]
1623
return s[::-1] == s
24+
1725

1826
def check(i):
19-
"""check whether the integer (i) has the properties described
20-
in the puzzler
27+
"""Checks if the integer (i) has the desired properties.
28+
29+
i: int
2130
"""
22-
return (has_palindrome(i, 2, 4) and
31+
return (has_palindrome(i, 2, 4) and
2332
has_palindrome(i+1, 1, 5) and
2433
has_palindrome(i+2, 1, 4) and
2534
has_palindrome(i+3, 0, 6))
2635

27-
def check_all():
28-
"""enumerate the six-digit numbers and print any that satisfy the
29-
requirements of the puzzler"""
3036

37+
def check_all():
38+
"""Enumerate the six-digit numbers and print any winners.
39+
"""
3140
i = 100000
32-
while i <= 999999:
41+
while i <= 999996:
3342
if check(i):
34-
print i
43+
print(i)
3544
i = i + 1
3645

37-
print 'The following are the possible odometer readings:'
46+
47+
print('The following are the possible odometer readings:')
3848
check_all()
39-
print
49+
print()
4050

4151

code/cartalk3.py

+46-24
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,80 @@
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
8-
http://www.cartalk.com/content/puzzler/transcripts/200813
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
910
"""
1011

11-
def str_fill(i, len):
12-
"""return the integer (i) written as a string with at least
13-
(len) digits"""
14-
return str(i).zfill(len)
12+
from __future__ import print_function, division
13+
14+
15+
def str_fill(i, n):
16+
"""Returns i as a string with at least n digits.
17+
18+
i: int
19+
n: int length
20+
21+
returns: string
22+
"""
23+
return str(i).zfill(n)
1524

1625

1726
def are_reversed(i, j):
18-
""" return True if the integers i and j, written as strings,
19-
are the reverse of each other"""
20-
return str_fill(i,2) == str_fill(j,2)[::-1]
27+
"""Checks if i and j are the reverse of each other.
28+
29+
i: int
30+
j: int
31+
32+
returns:bool
33+
"""
34+
return str_fill(i, 2) == str_fill(j, 2)[::-1]
2135

2236

2337
def num_instances(diff, flag=False):
24-
"""returns the number of times the mother and daughter have
25-
pallindromic ages in their lives, given the difference in age.
26-
If flag==True, prints the details."""
38+
"""Counts the number of palindromic ages.
39+
40+
Returns the number of times the mother and daughter have
41+
palindromic ages in their lives, given the difference in age.
42+
43+
diff: int difference in ages
44+
flag: bool, if True, prints the details
45+
"""
2746
daughter = 0
2847
count = 0
2948
while True:
3049
mother = daughter + diff
3150
if are_reversed(daughter, mother) or are_reversed(daughter, mother+1):
3251
count = count + 1
3352
if flag:
34-
print daughter, mother
53+
print(daughter, mother)
3554
if mother > 120:
3655
break
3756
daughter = daughter + 1
3857
return count
3958

4059

4160
def check_diffs():
42-
"""enumerate the possible differences in age between mother
43-
and daughter, and for each difference, count the number of times
61+
"""Finds age differences that satisfy the problem.
62+
63+
Enumerates the possible differences in age between mother
64+
and daughter, and for each difference, counts the number of times
4465
over their lives they will have ages that are the reverse of
45-
each other."""
66+
each other.
67+
"""
4668
diff = 10
4769
while diff < 70:
4870
n = num_instances(diff)
4971
if n > 0:
50-
print diff, n
72+
print(diff, n)
5173
diff = diff + 1
5274

53-
print 'diff #instances'
75+
print('diff #instances')
5476
check_diffs()
5577

56-
print
57-
print 'daughter mother'
78+
print()
79+
print('daughter mother')
5880
num_instances(18, True)

code/circle_demo.py

-43
This file was deleted.

0 commit comments

Comments
 (0)