Skip to content

Solution C02 I fixed a bug that was mutating the draw list during validation. #154

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

Merged
merged 1 commit into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions 02/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ def input_word(draw):


def _validation(word, draw):
unused_letters = list(draw) # copied because we are mutating the list
for char in word.upper():
if char in draw:
draw.remove(char)
if char in unused_letters:
unused_letters.remove(char)
else:
raise ValueError("{} is not a valid word!".format(word))
if not word.lower() in DICTIONARY:
Expand Down
4 changes: 3 additions & 1 deletion 02/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ def test_get_possible_dict_words(self):
self.assertEqual(len(words), 137)

def test_validation(self):
draw = list('garytev'.upper())
letters = list('garytev'.upper())
draw = list(letters)
word = 'GARYTEV'
self.assertRaises(ValueError, _validation, word, draw)
word = 'F'
self.assertRaises(ValueError, _validation, word, draw)
word = 'GARETTA'
self.assertRaises(ValueError, _validation, word, draw)
self.assertEqual(letters, draw)

if __name__ == "__main__":
unittest.main()