-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem2.py
170 lines (141 loc) · 5.22 KB
/
Problem2.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
'''
R1C2: Words With Friends Cheater
Your goal is to create a program that finds the highest scoring arrangement of Words With
Friends tiles. The points awarded for each tile are as follows:
BLANK 0
A 1
B 4
C 4
D 2
E 1
F 4
G 3
H 3
I 1
J 10
K 5
L 2
M 4
N 2
O 1
P 4
Q 10
R 1
S 1
T 1
U 2
V 5
W 4
X 8
Y 3
Z 10
A few notes:
● Your program will not accept any arguments this time. When the program is run, it will
output a prompt asking the user to input a spacedelimited string of their tiles.
● The maximum number of tiles is 7. Near the end of a game a user may have as few as 1.
● You can assume the input is valid and follows the specified format exactly. You don’t
necessarily need to add any error handling for the input.
● Take into account that inputs may not necessarily contain a valid output.
● Words With Friends awards an extra 35 points if all 7 tiles are used. Your program
should take that into account.● Your output should include:
○ the final word, each tile separated by spaces
○ the point value of that word
○ the remaining unused tiles (if any), each tile separated by spaces
● If a BLANK tile is used in your final word, include BLANK in the appropriate place, and add
its interpreted letter in parentheses at the end of the word. Take into account that Word
With Friends has two BLANK tiles in each game, so two sets of parentheses may be
necessary.
● We should all use a main() function like Ian did in R1C1. It’s probably a good habit to get
into, and also it’ll keep our code uniform. That’ll help keep your submissions anonymous.
'''
# Creates wordlist from UNIX dictionary.
def get_words():
words_file = open('/usr/share/dict/words')
word_list = words_file.read().splitlines()
return word_list
# Compares word from wordlist with tiles and determines if word can be made,
# taking blanks into account. If word can be made, it returns the word.
def can_make(poss_word, tiles):
x = 0
tiles_copy = sorted(tiles)
for letter in poss_word:
if letter in tiles_copy:
tiles_copy.remove(letter)
x += 1
elif 'blank' in tiles_copy:
tiles_copy.remove('blank')
x += 1
if len(poss_word) == x:
return poss_word
# Calculates the points for the word, taking into account blanks and length bonus.
def poss_points(word, points, tiles):
tiles_copy = sorted(tiles)
total = 0
for letter in word:
if letter in tiles_copy:
total += points[letter]
tiles_copy.remove(letter)
else:
tiles_copy.remove('blank')
if len(word) == 7:
total += 35
return total
# Calls previous functions to pull wordlist, check each word in list, compute score for
# buildable words, and print the highest scoring word.
def find_word(tiles, word_list):
my_list = {}
replaced = []
points = {
'a':1,'b':4,'c':4,'d':2,'e':1,'f':4,'g':3,'h':3,'i':1,'j':10,'k':5,'l':
2,'m':4,'n':2,'o':1,'p':4,'q':10,'r':1,'s':1,'t':1,'u':2,'v':5,'w':4,'x'
:8,'y':3,'z':10,'blank':0
}
# Checks if word can be made with tiles and if so, assigns it and its point
# value as key:value dict pair.Ignores capitalized words.
for word in word_list:
if not word.istitle() and can_make(word, tiles):
my_list[word] = poss_points(word, points, tiles)
# If no buildable words found, restarts module.
if my_list == {}:
print "\nNo valid word found.\n"
main()
# Pulls highest scoring word and its score from dictionary.
sorted_list = sorted(my_list.items(), key=lambda point: point[1])
best_word = sorted_list[-1]
high_word = best_word[0]
high_score = best_word[1]
# Reduces tiles to only unused letters, subs blanks in word where applicable
# and places the 'blanked' letters in their own list.
high_word = list(high_word)
for letter in high_word:
if letter in tiles:
tiles.remove(letter)
elif 'blank' in tiles:
tiles.remove('blank')
ind = high_word.index(letter)
high_word.remove(letter)
high_word.insert(ind, 'blank')
replaced.append(letter)
# Formats and prints word, any blanked letters, word score, and any unused
# letters.
replaced = ['('+ each + ')' for each in replaced]
print (
' '.join(high_word) + ' ' + ' '.join(replaced) + '\n' + str(high_score)
+ '\n' + ' '.join(tiles)).upper(
)
# Prompts user input and passes it through find_word function, then prompts for
# additional attempts
def main():
word_list = get_words()
get_tiles = raw_input(
'Please enter your tile letters. Enter BLANK for a blank tile:\n'
)
my_tiles = get_tiles.lower().split()
find_word(my_tiles, word_list)
ask = raw_input("Enter 1 to exit, or any other key to play again.")
if ask == '1':
exit()
else:
main()
if __name__ == '__main__':
main()