Skip to content

Commit 68eaf39

Browse files
committed
Updating for Python 3
1 parent eb4112c commit 68eaf39

11 files changed

+14
-15
lines changed

code/Card.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ def __str__(self):
3535
return '%s of %s' % (Card.rank_names[self.rank],
3636
Card.suit_names[self.suit])
3737

38-
def __cmp__(self, other):
38+
def __lt__(self, other):
3939
"""Compares this card to other, first by suit, then rank.
4040
41-
Returns a positive number if this > other; negative if other > this;
42-
and 0 if they are equivalent.
41+
returns: boolean
4342
"""
4443
t1 = self.suit, self.rank
4544
t2 = other.suit, other.rank
46-
return cmp(t1, t2)
45+
return t1 < t2
4746

4847

4948
class Deck(object):

code/Markov.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def random_text(self, n=100):
6969
n: number of words to generate
7070
"""
7171
# choose a random prefix (not weighted by frequency)
72-
start = random.choice(self.suffix_map.keys())
72+
start = random.choice(list(self.suffix_map.keys()))
7373

7474
for i in range(n):
7575
suffixes = self.suffix_map.get(start, None)

code/PokerHandSoln.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def make_histograms(self):
5151
self.suits.count(c.suit)
5252
self.ranks.count(c.rank)
5353

54-
self.sets = self.ranks.values()
54+
self.sets = list(self.ranks.values())
5555
self.sets.sort(reverse=True)
5656

5757
def has_highcard(self):

code/Time1_soln.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def main():
117117
race_time = Time()
118118
race_time.hour = 1
119119
race_time.minute = 34
120-
race_time.second = 05
120+
race_time.second = 5
121121

122122
print('Half marathon time', end=' ')
123123
print_time(race_time)

code/anagram_sets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def filter_length(d, n):
8484
returns: new map from word to list of anagrams
8585
"""
8686
res = {}
87-
for word, anagrams in d.iteritems():
87+
for word, anagrams in d.items():
8888
if len(word) == n:
8989
res[word] = anagrams
9090
return res

code/find_duplicates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def print_duplicates(d):
114114
115115
d: map from checksum to list of files with that checksum
116116
"""
117-
for key, names in d.iteritems():
117+
for key, names in d.items():
118118
if len(names) > 1:
119119
print('The following files have the same checksum:')
120120
for name in names:

code/find_duplicates_copy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def print_duplicates(d):
114114
115115
d: map from checksum to list of files with that checksum
116116
"""
117-
for key, names in d.iteritems():
117+
for key, names in d.items():
118118
if len(names) > 1:
119119
print('The following files have the same checksum:')
120120
for name in names:

code/invert_dict.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ def invert_dict(d):
2323
Returns: dict
2424
"""
2525
inverse = {}
26-
for key, val in d.iteritems():
26+
for key, val in d.items():
2727
inverse.setdefault(val, []).append(key)
2828
return inverse
2929

3030

3131
if __name__ == '__main__':
3232
d = dict(a=1, b=2, c=3, z=1)
3333
inverse = invert_dict(d)
34-
for val, keys in inverse.iteritems():
34+
for val, keys in inverse.items():
3535
print(val, keys)
3636

code/markov.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def random_text(n=100):
7777
n: number of words to generate
7878
"""
7979
# choose a random prefix (not weighted by frequency)
80-
start = random.choice(suffix_map.keys())
80+
start = random.choice(list(suffix_map.keys()))
8181

8282
for i in range(n):
8383
suffixes = suffix_map.get(start, None)

code/most_frequent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def most_frequent(s):
2222
hist = make_histogram(s)
2323

2424
t = []
25-
for x, freq in hist.iteritems():
25+
for x, freq in hist.items():
2626
t.append((freq, x))
2727

2828
t.sort(reverse=True)

code/zipf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def rank_freq(hist):
2626
returns: list of (rank, freq) tuples
2727
"""
2828
# sort the list of frequencies in decreasing order
29-
freqs = hist.values()
29+
freqs = list(hist.values())
3030
freqs.sort(reverse=True)
3131

3232
# enumerate the ranks and frequencies

0 commit comments

Comments
 (0)