-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrabble.py
135 lines (106 loc) · 3.25 KB
/
scrabble.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
# Import modules to be used in program
import sys
from checking_functions import *
from scoring_functions import *
# Open Dictionary
with open("sowpods.txt","r") as infile:
raw_input = infile.readlines()
sowpods = [datum.strip('\n').lower() for datum in raw_input]
# Illegal characters and scoring dictionary
illegal_characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '+', ',', '-', '.', '/', ':', ';', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
# Initial input from command line
o = sys.argv
# Input validity Checking
class Error(Exception):
"""Base class for other exceptions"""
pass
class SysLenError1(Error):
"""If no rack entered during initial input."""
pass
class SysLenError2(Error):
"""If space used in initial input."""
pass
class LenError1(Error):
"""If space used in initial input."""
pass
illegal_characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '+', ',', '-', '.', '/', ':', ';', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', ' ']
try:
o = sys.argv
if len(o) == 1:
raise SysLenError1
if len(o) > 2:
raise SysLenError2
except SysLenError1:
print("You didn't enter a rack, please re-run and enter a rack of valid characters.")
sys.exit()
except SysLenError2:
print("No spaces permitted, please re-run and enter a rack without spaces and valid characters.")
sys.exit()
else:
a = (sys.argv[1]).strip('""')
try:
if len(a) < 2 or len(a) > 7:
raise LenError1
else:
pass
except LenError1:
print("You must only enter between 2-7 legal characters. Please re-run program.")
sys.exit()
else:
pass
try:
for letter in a:
if letter in illegal_characters:
raise ValueError
except ValueError:
print("You entered an invalid character, only letters, '*', and '?' are allowed. Please re-run program.")
sys.exit()
else:
pass
try:
for letter in a:
if a.count('*') > 1:
raise ValueError
except ValueError:
print("Only one of each wildcard character is allowed. Please re-run program.")
sys.exit()
else:
pass
try:
for letter in a:
if a.count('?') > 1:
raise ValueError
except ValueError:
print("Only one of each wildcard character is allowed. Please re-run program.")
sys.exit()
else:
pass
# Matching processes
b = input_sorter(a)
data = data_cutter(sowpods, b)
c = wildcard_checker(b)
if '*' in b and not '?' in b:
d = wildcard_match(data, c)
elif '?' in b and not '*' in b:
d = wildcard_match(data, c)
elif '*' and '?' in b:
d = wildcard_match(data, c)
else:
d = match_words(data, c)
# Scoring Processes
if '*' in b and not '?' in b:
e = wildcard_scorer(d, b)
elif '?' in b and not '*' in b:
e = wildcard_scorer(d, b)
elif '*' and '?' in b:
e = wildcard_scorer(d, b)
else:
e = score_word(d)
# Print outputs
for item in e:
print('({0}, {1})'.format(item[0],item[1]))