-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word Wrangler.py
213 lines (178 loc) · 5.86 KB
/
Word Wrangler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Student code for Word Wrangler game
"""
import urllib2
import codeskulptor
import poc_wrangler_provided as provided
WORDFILE = "assets_scrabble_words3.txt"
# Functions to manipulate ordered word lists
def remove_duplicates(list1):
"""
Eliminate duplicates in a sorted list.
Returns a new sorted list with the same elements in list1, but
with no duplicates.
This function can be iterative.
"""
new_list = list(list1)
for poi in xrange(len(new_list)):
try:
while new_list[poi] == new_list[poi+1]:
del new_list[poi+1]
except IndexError:
return new_list
return new_list
def intersect(list1, list2):
"""
Compute the intersection of two sorted lists.
Returns a new sorted list containing only elements that are in
both list1 and list2.
This function can be iterative.
"""
clist1 = list(list1)
clist2 = list(list2)
new_list = []
while clist1 and clist2:
if clist1[0] < clist2[0]:
del clist1[0]
elif clist1[0] > clist2[0]:
del clist2[0]
else:
del clist1[0]
new_list.append(clist2.pop(0))
return new_list
# Functions to perform merge sort
def merge(list1, list2):
"""
Merge two sorted lists.
Returns a new sorted list containing all of the elements that
are in either list1 and list2.
This function can be iterative.
"""
clist1 = list(list1)
clist2 = list(list2)
new_list = []
while clist1 or clist2:
if clist1 and clist2:
if clist1[0] < clist2[0]:
new_list.append(clist1.pop(0))
elif clist1[0] > clist2[0]:
new_list.append(clist2.pop(0))
else:
new_list.append(clist1.pop(0))
new_list.append(clist2.pop(0))
elif clist1 and not clist2:
while clist1:
new_list.append(clist1.pop(0))
elif not clist1 and clist2:
while clist2:
new_list.append(clist2.pop(0))
return new_list
def merge_sort(list1):
"""
Sort the elements of list1.
Return a new sorted list with the same elements as list1.
This function should be recursive.
"""
#base case
if len(list1) == 1:
return list1
elif len(list1) == 0:
return []
# induction case
else:
mid = len(list1)//2
left = list1[:mid]
right = list1[mid:]
return merge(merge_sort(left), merge_sort(right))
# Function to generate all strings for the word wrangler game
def gen_all_strings(word):
"""
Generate all strings that can be composed from the letters in word
in any order.
Returns a list of all strings that can be formed from the letters
in word.
This function should be recursive.
"""
#base case
if len(word)==0:
return [""]
# induction case
else:
first = word[0]
rest = word[1:]
add = lambda letter, word: [word[:poi] + letter + word[poi:] for poi in xrange(len(word)+1)]
rest_all_word = gen_all_strings(rest)
word_for_first = []
for word in rest_all_word:
word_for_first += add(first, word)
return rest_all_word + word_for_first
# Function to load words from a file
def load_words(filename):
"""
Load word list from the file named filename.
Returns a list of strings.
"""
url = codeskulptor.file2url(WORDFILE )
netfile = urllib2.urlopen(url)
string_list = []
for word in netfile.readlines():
string_list.append(word)
return string_list
def run():
"""
Run game.
"""
words = load_words(WORDFILE)
wrangler = provided.WordWrangler(words, remove_duplicates,
intersect, merge_sort,
gen_all_strings)
provided.run_game(wrangler)
# Uncomment when you are ready to try the game
# run()
import poc_simpletest
function_to_test = [remove_duplicates, intersect
,merge, merge_sort
,gen_all_strings]
def run_suite(function):
"""
test the functions
"""
suite = poc_simpletest.TestSuite()
print function[0]
suite.run_test(function[0]([]), [])
suite.run_test(function[0]([1,2,2,3]), [1,2,3])
suite.run_test(function[0]([1,2,3,2]), [1,2,3,2])
suite.report_results()
suite = poc_simpletest.TestSuite()
print function[1]
suite.run_test(function[1]([], [1,2,3]), [])
suite.run_test(function[1]([1,2,3], []), [])
suite.run_test(function[1]([1], [1,2,3]), [1])
suite.run_test(function[1]([3], [1,2,3]), [3])
suite.run_test(function[1]([1,3,5], [4,5]), [5])
suite.run_test(function[1]([1,3,5], [3]), [3])
suite.run_test(function[1]([1,3,5], [1,3,5]), [1,3,5])
suite.run_test(function[1]([8, 19, 32, 47], [1, 5, 7, 8]), [8])
suite.report_results()
suite = poc_simpletest.TestSuite()
print function[2]
suite.run_test(function[2]([], [1,2,3]), [1,2,3])
suite.run_test(function[2]([1,2,3], []), [1,2,3])
suite.run_test(function[2]([2,3,4], [1,2,3]), [1,2,2,3,3,4])
suite.run_test(function[2]([3,5,7], [3,3,3]), [3,3,3,3,5,7])
suite.report_results()
suite = poc_simpletest.TestSuite()
print function[3]
suite.run_test(function[3]([]), [])
suite.run_test(function[3]([1,1]), [1,1])
suite.run_test(function[3]([2,1]), [1,2])
suite.run_test(function[3]([3,4,1,5]), [1,3,4,5])
suite.run_test(function[3]([2,2,3,3,5]), [2,2,3,3,5])
suite.report_results()
suite = poc_simpletest.TestSuite()
print function[4]
suite.run_test(function[4]('a'), ['', 'a'])
suite.run_test(function[4]('ab'), ['', 'b', 'a', 'ab', 'ba'], '#2')
suite.run_test(function[4]('aa'), ['', 'a', 'a', 'aa', 'aa'], '#3')
suite.report_results()
run_suite(function_to_test)