-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha3.py
248 lines (154 loc) · 5.45 KB
/
a3.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'''A board is a list of list of str. For example, the board
ANTT
XSOB
is represented as the list
[['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']]
A word list is a list of str. For example, the list of words
ANT
BOX
SOB
TO
is represented as the list
['ANT', 'BOX', 'SOB', 'TO']
'''
def is_valid_word(wordlist, word):
''' (list of str, str) -> bool
Return True if and only if word is an element of wordlist.
>>> is_valid_word(['ANT', 'BOX', 'SOB', 'TO'], 'TO')
True
>>> is_valid_word(['Stephen', 'Karen', 'Michael', 'Rose'], 'Michael')
True
>>> is_valid_word(['Stephen', 'Karen', 'Michael', 'Rose'], 'Thomas')
False
'''
found = True
if word in wordlist:
return found
elif word not in wordlist:
found = False
return found
def make_str_from_row(board, row_index):
''' (list of list of str, int) -> str
Return the characters from the row of the board with index row_index
as a single string.
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
'ANTT'
'''
row = ''
row_list = ''
row_list = board[row_index]
for char in row_list:
if char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
row = row + char
return row
def make_str_from_column(board, column_index):
''' (list of list of str, int) -> str
Return the characters from the column of the board with index column_index
as a single string.
>>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
'NS'
'''
total = ''
i = column_index
for item in board:
total = total + item[i]
return total
def board_contains_word_in_row(board, word):
''' (list of list of str, str) -> bool
Return True if and only if one or more of the rows of the board contains
word.
Precondition: board has at least one row and one column, and word is a
valid word.
>>> board_contains_word_in_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'SOB')
True
'''
for row_index in range(len(board)):
if word in make_str_from_row(board, row_index):
return True
return False
def board_contains_word_in_column(board, word):
''' (list of list of str, str) -> bool
Return True if and only if one or more of the columns of the board
contains word.
Precondition: board has at least one row and one column, and word is a
valid word.
>>> board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'NO')
False
'''
for column_index in range(-2, len(board)):
if word in make_str_from_column(board, column_index):
return True
return False
def board_contains_word(board, word):
'''(list of list of str, str) -> bool
Return True if and only if word appears in board.
Precondition: board has at least one row and one column.
>>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
True
'''
for column_index in range(-2, len(board)):
if word in make_str_from_column(board, column_index):
return True
for row_index in range(len(board)):
if word in make_str_from_row(board, row_index):
return True
return False
def word_score(word):
'''(str) -> int
Return the point value the word earns.
Word length: < 3: 0 points
3-6: 1 point per character in word
7-9: 2 points per character in word
10+: 3 points per character in word
>>> word_score('DRUDGERY')
16
'''
if len(word) < 3:
return 0
elif len(word) <=3:
return len(word)
elif len(word) <=6:
return len(word)
elif len(word) <= 7:
return len(word) * 2
elif len(word) <= 9:
return len(word) * 2
elif len(word) <= 10:
return len(word) * 3
elif len(word) <= 100:
return len(word) * 3
def update_score(player_info, word):
'''([str, int] list, str) -> NoneType
player_info is a list with the player's name and score. Update player_info
by adding the point value word earns to the player's score.
>>> update_score(['Jonathan', 4], 'ANT')
'''
score = word_score(word)
info = player_info
player_info.append(score)
return sum(info[1:3])
# Not working yet
def num_words_on_board(board, words):
'''(list of list of str, list of str) -> int
Return how many words appear on board.
>>> num_words_on_board([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], ['ANT', 'BOX', 'SOB', 'TO'])
3
'''
s = words
for s in range(len(s)):
return(i)
#Not finished
def read_words(words_file):
''' (file open for reading) -> list of str
Return a list of all words (with newlines removed) from open file
words_file.
Precondition: Each line of the file contains a word in uppercase characters
from the standard English alphabet.
'''
file = open(words_file, 'r')
#Not finished
def read_board(board_file):
''' (file open for reading) -> list of list of str
Return a board read from open file board_file. The board file will contain
one row of the board per line. Newlines are not included in the board.
'''