Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 01/NNRepos/wordvalue.py
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()))
Copy link
Collaborator

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(), word is already iterable. The solution also uses LETTER_SCORES.get(char.upper(), 0) which is safer, because with LETTER_SCORES[letter] any unexpected letter would raise a KeyError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i actually used the KeyError for debugging (that's how i've discovered that - is a legal character), but for correctness, i guess you're right.



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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted() already returns a list. I would break this out into multiple lines, which would make it more readable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the max() built-in here, for example: https://github.com/pybites/challenges/blob/solutions/01/wordvalue.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was actually looking for something like this: key=calc_word_value. that's very nice