Skip to content

Commit 7a2c8c1

Browse files
committed
Updating list_exercises
1 parent 96df30d commit 7a2c8c1

File tree

2 files changed

+159
-4
lines changed

2 files changed

+159
-4
lines changed

code/inlist.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import print_function, division
1313

14-
from bisect import bisect_left
14+
import bisect
1515

1616

1717
def make_word_list():
@@ -35,17 +35,43 @@ def in_bisect(word_list, word):
3535
word_list: list of strings
3636
word: string
3737
"""
38-
i = bisect_left(word_list, word)
39-
if i != len(word_list) and word_list[i] == word:
38+
if len(word_list) == 0:
39+
return False
40+
41+
i = len(word_list) // 2
42+
if word_list[i] == word:
4043
return True
44+
45+
if word_list[i] > word:
46+
# search the first half
47+
return in_bisect(word_list[:i], word)
4148
else:
49+
# search the second half
50+
return in_bisect(word_list[i+1:], word)
51+
52+
53+
def in_bisect_cheat(word_list, word):
54+
"""Checks whether a word is in a list using bisection search.
55+
56+
Precondition: the words in the list are sorted
57+
58+
word_list: list of strings
59+
word: string
60+
"""
61+
i = bisect.bisect_left(word_list, word)
62+
if i == len(word_list):
4263
return False
4364

65+
return word_list[i] == word
66+
4467

4568
if __name__ == '__main__':
4669
word_list = make_word_list()
4770

48-
for word in ['alien', 'allen']:
71+
for word in ['aa', 'alien', 'allen', 'zymurgy']:
4972
print(word, 'in list', in_bisect(word_list, word))
5073

74+
for word in ['aa', 'alien', 'allen', 'zymurgy']:
75+
print(word, 'in list', in_bisect_cheat(word_list, word))
76+
5177

code/list_exercises.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""This module contains a code example related to
2+
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
6+
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
10+
"""
11+
12+
from __future__ import print_function, division
13+
14+
import random
15+
16+
17+
def nested_sum(t):
18+
"""Computes the total of all numbers in a list of lists.
19+
20+
t: list of list of numbers
21+
22+
returns: number
23+
"""
24+
total = 0
25+
for nested in t:
26+
total += sum(nested)
27+
return total
28+
29+
30+
def cumsum(t):
31+
"""Computes the cumulative sum of the numbers in t.
32+
33+
t: list of numbers
34+
35+
returns: list of numbers
36+
"""
37+
total = 0
38+
res = []
39+
for x in t:
40+
total += x
41+
res.append(total)
42+
return res
43+
44+
45+
def middle(t):
46+
"""Returns all but the first and last elements of t.
47+
48+
t: list
49+
50+
returns: new list
51+
"""
52+
return t[1:-1]
53+
54+
55+
def chop(t):
56+
"""Removes the first and last elements of t.
57+
58+
t: list
59+
60+
returns: None
61+
"""
62+
del t[0]
63+
del t[-1]
64+
65+
66+
def is_sorted(t):
67+
"""Checks whether a list is sorted.
68+
69+
t: list
70+
71+
returns: boolean
72+
"""
73+
return t == sorted(t)
74+
75+
76+
def is_anagram(word1, word2):
77+
"""Checks whether two words are anagrams
78+
79+
word1: string or list
80+
word2: string or list
81+
82+
returns: boolean
83+
"""
84+
return sorted(word1) == sorted(word2)
85+
86+
87+
def has_duplicates(s):
88+
"""Returns True if any element appears more than once in a sequence.
89+
90+
s: string or list
91+
92+
returns: bool
93+
"""
94+
# make a copy of t to avoid modifying the parameter
95+
t = list(s)
96+
t.sort()
97+
98+
# check for adjacent elements that are equal
99+
for i in range(len(t)-1):
100+
if t[i] == t[i+1]:
101+
return True
102+
return False
103+
104+
105+
def main():
106+
t = [[1, 2], [3], [4, 5, 6]]
107+
print(nested_sum(t))
108+
109+
t = [1, 2, 3]
110+
print(cumsum(t))
111+
112+
t = [1, 2, 3, 4]
113+
print(middle(t))
114+
chop(t)
115+
print(t)
116+
117+
print(is_sorted([1, 2, 2]))
118+
print(is_sorted(['b', 'a']))
119+
120+
print(is_anagram('stop', 'pots'))
121+
print(is_anagram('different', 'letters'))
122+
print(is_anagram([1, 2, 2], [2, 1, 2]))
123+
124+
print(has_duplicates('cba'))
125+
print(has_duplicates('abba'))
126+
127+
128+
if __name__ == '__main__':
129+
main()

0 commit comments

Comments
 (0)