-
Notifications
You must be signed in to change notification settings - Fork 2.2k
PCC01 NNRepos #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PCC01 NNRepos #845
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from data import DICTIONARY, LETTER_SCORES | ||
| from typing import Optional, List | ||
|
|
||
|
|
||
| def load_words() -> List[str]: | ||
| """Load dictionary into a list and return list""" | ||
| with open(DICTIONARY) as f: | ||
| return [line.strip() for line in f.readlines()] | ||
|
|
||
|
|
||
| def calc_word_value(word: str) -> int: | ||
| """Calculate the value of the word entered into function | ||
| using imported constant mapping LETTER_SCORES""" | ||
| return sum(LETTER_SCORES[letter] for letter in list(word.replace('-', '').upper())) | ||
|
|
||
|
|
||
| def max_word_value(dictionary: Optional[List[str]] = None) -> str: | ||
| """Calculate the word with the max value, can receive a list | ||
| of words as arg, if none provided uses default DICTIONARY""" | ||
| if dictionary is None: | ||
| dictionary = load_words() | ||
| print("crap:", [x for x in dictionary if "-" in x]) | ||
| return list(sorted(([calc_word_value(word), word] for word in dictionary), reverse=True))[0][1] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was actually looking for something like this: |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the
list(),wordis already iterable. The solution also usesLETTER_SCORES.get(char.upper(), 0)which is safer, because withLETTER_SCORES[letter]any unexpected letter would raise aKeyError.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i actually used the
KeyErrorfor debugging (that's how i've discovered that-is a legal character), but for correctness, i guess you're right.