Skip to content

Commit

Permalink
Add unique_letters function and add option to word-finder to print ou…
Browse files Browse the repository at this point in the history
…t words containing unique letters (#13)
  • Loading branch information
olilag authored Sep 28, 2024
1 parent 2bf3565 commit a0c8c38
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/applications/word-finder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Program vie meniť svoje správanie podľa toho, čo napíšeš za ``susi-word-f
- tu nasleduje cesta k textovému súboru, v ktorom chceme hľadať alebo skratku slovníku :any:`Dictionary`
* - ``-w/--word-length``
- nasleduje číslo, aké dlhé slová má hľadať alebo rozpätie ``x-y``, ktoré znamená, že vyhľadá všetky slová dlhé ``x`` až ``y``
* - ``-u/--unique``
- ak pridáme túto možnosť, tak program vypíše iba slová, ktorých písmená sa neopakujú

Číslo pri ``-w/--word-length`` sa použije hlavne vtedy, ak nasleduje iba
jeden argument. Ak sa ``-w/--word-length`` nepoužije, dĺžka slova sa určí
Expand Down
1 change: 1 addition & 0 deletions src/susi_lib/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
encode,
find_anagrams,
is_palindrome,
unique_letters,
)
18 changes: 18 additions & 0 deletions src/susi_lib/functions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,21 @@ def find_anagrams(word: str, word_list: list[str]) -> list[str]:
found_words.append(w)

return found_words


def unique_letters(word: str) -> bool:
"""Function for checking if given ``word`` contains only unique letters.
:raises: :py:class:`TypeError` when ``word`` is not :py:class:`str`
:param word: Word to check
:return: ``True`` if it contains unique letters, ``False`` otherwise
"""
if not isinstance(word, str):
raise TypeError("Word must be a string")
found: set[str] = set()
for c in word:
if c in found:
return False
found.add(c)
return True
13 changes: 11 additions & 2 deletions src/susi_lib/word_finder_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Literal, TypedDict, cast

from susi_lib.dictionary import Dictionary, _Mapping
from susi_lib.functions import unique_letters
from susi_lib.regex import Selection, create_regex


Expand Down Expand Up @@ -83,11 +84,16 @@ def _translate(
parser.add_argument(
"-i",
"--input-file",
type=str,
default=None,
help="Path to a input file, each word should be on separate line. Or a dictionary short: \
'PM', 'PM_a', 'S', 'S_a', 'ZT', 'ZT_a'. Default: stdin",
)
parser.add_argument(
"-u",
"--unique",
action="store_true",
help="If set, it finds only words with unique letters",
)
parser.add_argument(
"wanted_letters",
help="type wanted letters without spaces or space separated groups of wanted \
Expand Down Expand Up @@ -124,5 +130,8 @@ def main() -> Literal[0, 1]:
args_parsed.wanted_letters, word_length, args_parsed.input_file
)
regex = create_regex(*args, **kwargs)
print("\n".join(regex.execute()))
result = regex.execute()
if args_parsed.unique:
result = [w for w in result if unique_letters(w)]
print("\n".join(result))
return 0
6 changes: 5 additions & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: skip-file
import unittest

from susi_lib.functions import find_anagrams, is_palindrome
from susi_lib.functions import find_anagrams, is_palindrome, unique_letters
from susi_lib.types import Symbols


Expand All @@ -27,6 +27,10 @@ def test_find_anagrams(self):
with self.assertRaises(TypeError):
find_anagrams("kokos", [1, 2, 3])

def test_unique_letters(self):
self.assertTrue(unique_letters("abc"))
self.assertFalse(unique_letters("kokos"))


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

0 comments on commit a0c8c38

Please sign in to comment.