-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathscrabble.py
69 lines (58 loc) · 1.43 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
from collections import Counter
from typing import Dict
from typing import List
from typing import Optional
def calculate_score(word: str, LETTER_POINTS: Dict[str, int]) -> int:
score = 0
for letter in word:
score += LETTER_POINTS.get(letter, 0)
return score
def find_max_score_word(dictionary: List[str], letters: str, LETTER_POINTS: Dict[str, int]) -> Optional[str]:
max_score = 0
max_score_word = None
for word in dictionary:
# Check if the word can be formed using the available letters
letters_counter = Counter(letters)
word_counter = Counter(word)
if all(letters_counter[letter] >= count for letter, count in word_counter.items()):
score = calculate_score(word, LETTER_POINTS)
if score > max_score:
max_score = score
max_score_word = word
return max_score_word
LETTER_POINTS = {
"e": 1,
"a": 1,
"i": 1,
"o": 1,
"n": 1,
"r": 1,
"t": 1,
"l": 1,
"s": 1,
"u": 1,
"d": 2,
"g": 2,
"b": 3,
"c": 3,
"m": 3,
"p": 3,
"f": 4,
"h": 4,
"v": 4,
"w": 4,
"y": 4,
"k": 5,
"j": 8,
"x": 8,
"q": 10,
"z": 10,
}
if __name__ == "__main__":
# Input
N = int(input())
dictionary = [input() for _ in range(N)]
letters = input()
# Output
result = find_max_score_word(dictionary, letters, LETTER_POINTS)
print(result)