Skip to content

Commit

Permalink
feat(utils): build guessed word
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Dec 1, 2023
1 parent d18d752 commit ddcc2ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions hangman/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ def join_guessed_letters(guessed_letters: Set[str]) -> str:
str: joined letters as a string separated by a space delimiter
"""
return " ".join(sorted(guessed_letters))


def build_guessed_word(target_word: str, guessed_letters: Set[str]) -> str:
"""
Build the word to show the player. This takes in the target word and a set of guessed letters. If any of the letters
in the target word exist in the guessed letters, the are added to the output. If not, an underscore is added in its
place.
Args:
target_word (str): Target word to build from guessed letters
guessed_letters (set): set of guessed letters
Returns:
str: returns the guessed word
"""
current_letters = []

for letter in target_word:
if letter in guessed_letters:
current_letters.append(letter)
else:
current_letters.append("_")

return " ".join(current_letters)
9 changes: 8 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from hangman.utils import join_guessed_letters
from hangman.utils import join_guessed_letters, build_guessed_word


class UtilsTestCase(unittest.TestCase):
Expand All @@ -9,6 +9,13 @@ def test_join_guessed_letters(self):
actual = join_guessed_letters(letters)
self.assertEqual(expected, actual)

def test_build_guessed_word(self):
guessed_letters = {"a", "b", "c", "d", "p", "o"}
target_word = "airport"
expected = 'a _ _ p o _ _'
actual = build_guessed_word(target_word, guessed_letters)
self.assertEqual(expected, actual)


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

0 comments on commit ddcc2ae

Please sign in to comment.