-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrabbleWords.py
More file actions
152 lines (104 loc) · 4.08 KB
/
Copy pathscrabbleWords.py
File metadata and controls
152 lines (104 loc) · 4.08 KB
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
#!/usr/bin/env python3
import sys
import pytest
# Mason Corey, Fall 2017
def createWordList(filename):
'''
Reads in the in the filename and creates a list L which contains all the words.
You must remove the newline character ('\n') at the end of each word.
:param filename: the string which represents the filename you are reading from.
:return: A list of strings
'''
if filename == '' or type(filename)!=str:
return False
L = []
file = open(filename)
line = file.readline()
L.append(line[:-1])
for line in file:
L.append(line[:-1])
return L
def canWeMakeIt(myWord, myLetters):
'''
Takes in a word and a string of letters and checks whether the word can be made with those letters.
:param myWord: the string we are checking.
:param myLetters: a string of letters we can use to build a string.
:return: A bool whether the string can be made or not.
'''
myList = []
for item in myLetters:
myList.append(item)
for i in range(len(myWord)):
if myWord[i] not in myList:
return False
index = myList.index(myWord[i])
myList.pop(index)
return True
def getWordPoints(myWord, letterPoints):
'''
Returns the total points of a given word.
:param myWord: the string you want to calculate points for
:param LetterPoints: a dictionary of (letter, value) pairs which gives the point value of each letter
:return: The total point value of the word.
'''
dict_points = {
'a':1,'b':3,'c':3,'d':2,'e':1,'f':4,'g':2,'h':4,'i':1,'j':8,'k':5,
'l':1,'m':3,'n':1,'o':1,'p':3,'q':10,'r':1,'s':1,'t':1,'u':1,'v':4,
'w':4,'x':8,'y':4,'z':10}
numPoints = 0
for item in myWord:
numPoints += letterPoints.get(item,0)
return numPoints
def outputWordPointPairs(pointWordList, myLetters, toFile):
'''
Outputs the contents of pointWordList in a specified format to the terminal
or a file.
:param pointWordList: a list of tuples to output to, with each tuple
containing a (pointValue, word) pair
:param myLetters: a string that you will name the file with if toFile is True
:param toFile: a boolean to decide whether I want to print to file or not. If True then output to file else output to terminal.
:return: None
'''
width = len(myLetters)+4
if toFile == True:
#write possible pointValue,word to text file
file = open(myLetters + '.txt','w')
for i in range(len(pointWordList)):
lenSpace = width - len(pointWordList[i][1])
file.write(pointWordList[i][1] + ' '*lenSpace + str(pointWordList[i][0]) + '\n')
file.close()
if toFile == False:
#print values to python shell
for i in range(len(pointWordList)):
lenSpace = width - len(pointWordList[i][1])
print(pointWordList[i][1] + ' '*lenSpace + str(pointWordList[i][0]) + '\n',end = '')
return
def scrabbleWords(myLetters):
'''
A function which takes in a string of letters and shows what words can be constructed from it.
It should use the helper functions defined above to do so.
:param myLetters: a string of letters we are using.
:return: None
'''
letterPoints = {
'a':1,'b':3,'c':3,'d':2,'e':1,'f':4,'g':2,'h':4,'i':1,'j':8,'k':5,
'l':1,'m':3,'n':1,'o':1,'p':3,'q':10,'r':1,'s':1,'t':1,'u':1,'v':4,
'w':4,'x':8,'y':4,'z':10}
myWords = []
wordList = createWordList('wordlist.txt')
for item in wordList:
if canWeMakeIt(item,myLetters)==True:
myWords.append(item)
pointWordList = []
for word in myWords:
pointValue = getWordPoints(word, letterPoints)
pointWordList.append((pointValue, word))
pointWordList.sort(reverse = True)
outputWordPointPairs(pointWordList, myLetters, False)
outputWordPointPairs(pointWordList, myLetters, True)
return
if __name__=="__main__":
if len(sys.argv) >= 2:
scrabbleWords(sys.argv[1])
else:
print("Invalid input, please try again")