Skip to content

Commit 4a97918

Browse files
committed
first commit - added practice files for first class
0 parents  commit 4a97918

15 files changed

Lines changed: 4684 additions & 0 deletions

File tree

basic/alice.txt

Lines changed: 3600 additions & 0 deletions
Large diffs are not rendered by default.

basic/list1.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Basic list exercises
2+
# Fill in the code for the functions below. main() is already set up
3+
# to call the functions with a few different inputs,
4+
# printing 'OK' when each function is correct.
5+
# The starter code for each function includes a 'return'
6+
# which is just a placeholder for your code.
7+
# It's ok if you do not complete all the functions, and there
8+
# are some additional functions to try in list2.py.
9+
10+
# A. match_ends
11+
# Given a list of strings, return the count of the number of
12+
# strings where the string length is 2 or more and the first
13+
# and last chars of the string are the same.
14+
# Note: python does not have a ++ operator, but += works.
15+
def match_ends(words):
16+
# +++your code here+++
17+
return
18+
19+
20+
# B. front_x
21+
# Given a list of strings, return a list with the strings
22+
# in sorted order, except group all the strings that begin with 'x' first.
23+
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
24+
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
25+
# Hint: this can be done by making 2 lists and sorting each of them
26+
# before combining them.
27+
def front_x(words):
28+
# +++your code here+++
29+
return
30+
31+
32+
33+
# C. sort_last
34+
# Given a list of non-empty tuples, return a list sorted in increasing
35+
# order by the last element in each tuple.
36+
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
37+
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
38+
# Hint: use a custom key= function to extract the last element form each tuple.
39+
def sort_last(tuples):
40+
# +++your code here+++
41+
return
42+
43+
44+
# Simple provided test() function used in main() to print
45+
# what each function returns vs. what it's supposed to return.
46+
def test(got, expected):
47+
if got == expected:
48+
prefix = ' OK '
49+
else:
50+
prefix = ' X '
51+
print('{} got: {} expected: {}'.format(prefix, repr(got), repr(expected)))
52+
53+
54+
# Calls the above functions with interesting inputs.
55+
def main():
56+
print('match_ends')
57+
test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
58+
test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
59+
test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
60+
61+
print()
62+
print('front_x')
63+
test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
64+
['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
65+
test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
66+
['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
67+
test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
68+
['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
69+
70+
71+
print()
72+
print('sort_last')
73+
test(sort_last([(1, 3), (3, 2), (2, 1)]),
74+
[(2, 1), (3, 2), (1, 3)])
75+
test(sort_last([(2, 3), (1, 2), (3, 1)]),
76+
[(3, 1), (1, 2), (2, 3)])
77+
test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
78+
[(2, 2), (1, 3), (3, 4, 5), (1, 7)])
79+
80+
81+
if __name__ == '__main__':
82+
main()

basic/list2.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Additional basic list exercises
2+
3+
# D. Given a list of numbers, return a list where
4+
# all adjacent == elements have been reduced to a single element,
5+
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
6+
# modify the passed in list.
7+
def remove_adjacent(nums):
8+
# +++your code here+++
9+
return
10+
11+
12+
# E. Given two lists sorted in increasing order, create and return a merged
13+
# list of all the elements in sorted order. You may modify the passed in lists.
14+
# Ideally, the solution should work in "linear" time, making a single
15+
# pass of both lists.
16+
def linear_merge(list1, list2):
17+
# +++your code here+++
18+
return
19+
20+
# Note: the solution above is kind of cute, but unforunately list.pop(0)
21+
# is not constant time with the standard python list implementation, so
22+
# the above is not strictly linear time.
23+
# An alternate approach uses pop(-1) to remove the endmost elements
24+
# from each list, building a solution list which is backwards.
25+
# Then use reversed() to put the result back in the correct order. That
26+
# solution works in linear time, but is more ugly.
27+
28+
29+
# Simple provided test() function used in main() to print
30+
# what each function returns vs. what it's supposed to return.
31+
def test(got, expected):
32+
if got == expected:
33+
prefix = ' OK '
34+
else:
35+
prefix = ' X '
36+
print('{} got: {} expected: {}'.format(prefix, repr(got), repr(expected)))
37+
38+
39+
# Calls the above functions with interesting inputs.
40+
def main():
41+
print('remove_adjacent')
42+
test(remove_adjacent([1, 2, 2, 3]), [1, 2, 3])
43+
test(remove_adjacent([2, 2, 3, 3, 3]), [2, 3])
44+
test(remove_adjacent([]), [])
45+
46+
print()
47+
print('linear_merge')
48+
test(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']),
49+
['aa', 'bb', 'cc', 'xx', 'zz'])
50+
test(linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz']),
51+
['aa', 'bb', 'cc', 'xx', 'zz'])
52+
test(linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb']),
53+
['aa', 'aa', 'aa', 'bb', 'bb'])
54+
55+
56+
if __name__ == '__main__':
57+
main()

basic/mimic.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Mimic pyquick exercise -- optional extra exercise.
2+
3+
Read in the file specified on the command line.
4+
Do a simple split() on whitespace to obtain all the words in the file.
5+
Rather than read the file line by line, it's easier to read
6+
it into one giant string and split it once.
7+
8+
Build a "mimic" dict that maps each word that appears in the file
9+
to a list of all the words that immediately follow that word in the file.
10+
The list of words can be be in any order and should include
11+
duplicates. So for example the key "and" might have the list
12+
["then", "best", "then", "after", ...] listing
13+
all the words which came after "and" in the text.
14+
We'll say that the empty string is what comes before
15+
the first word in the file.
16+
17+
With the mimic dict, it's fairly easy to emit random
18+
text that mimics the original. Print a word, then look
19+
up what words might come next and pick one at random as
20+
the next work.
21+
Use the empty string as the first word to prime things.
22+
If we ever get stuck with a word that is not in the dict,
23+
go back to the empty string to keep things moving.
24+
25+
Note: the standard python module 'random' includes a
26+
random.choice(list) method which picks a random element
27+
from a non-empty list.
28+
29+
For fun, feed your program to itself as input.
30+
Could work on getting it to put in linebreaks around 70
31+
columns, so the output looks better.
32+
33+
"""
34+
35+
import random
36+
import sys
37+
38+
39+
def mimic_dict(filename):
40+
"""Returns mimic dict mapping each word to list of words which follow it."""
41+
# +++your code here+++
42+
return
43+
44+
45+
def print_mimic(mimic_dict, word):
46+
"""Given mimic dict and start word, prints 200 random words."""
47+
# +++your code here+++
48+
return
49+
50+
51+
# Provided main(), calls mimic_dict() and mimic()
52+
def main():
53+
if len(sys.argv) != 2:
54+
print('usage: ./mimic.py file-to-read')
55+
sys.exit(1)
56+
57+
dict = mimic_dict(sys.argv[1])
58+
print_mimic(dict, '')
59+
60+
61+
if __name__ == '__main__':
62+
main()

basic/small.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
We are not what we should be
2+
We are not what we need to be
3+
But at least we are not what we used to be
4+
-- Football Coach
5+

basic/solution/list1.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Basic list exercises
2+
# Fill in the code for the functions below. main() is already set up
3+
# to call the functions with a few different inputs,
4+
# printing 'OK' when each function is correct.
5+
# The starter code for each function includes a 'return'
6+
# which is just a placeholder for your code.
7+
# It's ok if you do not complete all the functions, and there
8+
# are some additional functions to try in list2.py.
9+
10+
# A. match_ends
11+
# Given a list of strings, return the count of the number of
12+
# strings where the string length is 2 or more and the first
13+
# and last chars of the string are the same.
14+
# Note: python does not have a ++ operator, but += works.
15+
def match_ends(words):
16+
# +++your code here+++
17+
# LAB(begin solution)
18+
count = 0
19+
for word in words:
20+
if len(word) >= 2 and word[0] == word[-1]:
21+
count = count + 1
22+
return count
23+
# LAB(replace solution)
24+
# return
25+
# LAB(end solution)
26+
27+
28+
# B. front_x
29+
# Given a list of strings, return a list with the strings
30+
# in sorted order, except group all the strings that begin with 'x' first.
31+
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
32+
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
33+
# Hint: this can be done by making 2 lists and sorting each of them
34+
# before combining them.
35+
def front_x(words):
36+
# +++your code here+++
37+
# LAB(begin solution)
38+
# Put each word into the x_list or the other_list.
39+
x_list = []
40+
other_list = []
41+
for w in words:
42+
if w.startswith('x'):
43+
x_list.append(w)
44+
else:
45+
other_list.append(w)
46+
return sorted(x_list) + sorted(other_list)
47+
# LAB(replace solution)
48+
# return
49+
# LAB(end solution)
50+
51+
52+
# LAB(begin solution)
53+
# Extract the last element from a tuple -- used for custom sorting below.
54+
def last(a):
55+
return a[-1]
56+
# LAB(end solution)
57+
58+
# C. sort_last
59+
# Given a list of non-empty tuples, return a list sorted in increasing
60+
# order by the last element in each tuple.
61+
# e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
62+
# [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
63+
# Hint: use a custom key= function to extract the last element form each tuple.
64+
def sort_last(tuples):
65+
# +++your code here+++
66+
# LAB(begin solution)
67+
return sorted(tuples, key=last)
68+
# LAB(replace solution)
69+
# return
70+
# LAB(end solution)
71+
72+
73+
# Simple provided test() function used in main() to print
74+
# what each function returns vs. what it's supposed to return.
75+
def test(got, expected):
76+
if got == expected:
77+
prefix = ' OK '
78+
else:
79+
prefix = ' X '
80+
print('{} got: {} expected: {}'.format(prefix, repr(got), repr(expected)))
81+
82+
83+
# Calls the above functions with interesting inputs.
84+
def main():
85+
print('match_ends')
86+
test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
87+
test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
88+
test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
89+
90+
print()
91+
print('front_x')
92+
test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
93+
['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
94+
test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
95+
['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
96+
test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
97+
['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
98+
99+
100+
print()
101+
print('sort_last')
102+
test(sort_last([(1, 3), (3, 2), (2, 1)]),
103+
[(2, 1), (3, 2), (1, 3)])
104+
test(sort_last([(2, 3), (1, 2), (3, 1)]),
105+
[(3, 1), (1, 2), (2, 3)])
106+
test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
107+
[(2, 2), (1, 3), (3, 4, 5), (1, 7)])
108+
109+
110+
if __name__ == '__main__':
111+
main()

0 commit comments

Comments
 (0)