diff --git a/CHANGELOG.md b/CHANGELOG.md index a4c102f..a1a46a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `uuid` command to generate UUIDs (`--count`). +- `anagram` command to check if two words or phrases are anagrams (ignores case and spaces). - `slugify` command to turn text into a URL-friendly slug. - `case` command to convert text between `snake`, `kebab`, `camel` and `pascal` naming. - `top-words` command to show the most frequent words (`--n`). diff --git a/fixit/cli.py b/fixit/cli.py index 254c493..32d4f17 100644 --- a/fixit/cli.py +++ b/fixit/cli.py @@ -101,6 +101,11 @@ def run_palindrome(args: argparse.Namespace) -> None: print(textutils.is_palindrome(args.text)) +def run_anagram(args: argparse.Namespace) -> None: + """Print whether the two given words or phrases are anagrams.""" + print(textutils.is_anagram(args.first, args.second)) + + def run_reverse(args: argparse.Namespace) -> None: print(textutils.reverse_words(args.text)) @@ -226,6 +231,7 @@ def run_morse(args: argparse.Namespace) -> None: "uuid": run_uuid, "json": run_json, "palindrome": run_palindrome, + "anagram": run_anagram, "reverse": run_reverse, "slugify": run_slugify, "case": run_case, @@ -316,6 +322,12 @@ def build_parser() -> argparse.ArgumentParser: pal = sub.add_parser("palindrome", help="check if text is a palindrome") pal.add_argument("text") + ana = sub.add_parser( + "anagram", help="check if two words or phrases are anagrams" + ) + ana.add_argument("first", help="first word or phrase") + ana.add_argument("second", help="second word or phrase") + rw = sub.add_parser("reverse", help="reverse the words in a sentence") rw.add_argument("text") diff --git a/fixit/textutils.py b/fixit/textutils.py index 839eb04..e0e1f09 100644 --- a/fixit/textutils.py +++ b/fixit/textutils.py @@ -22,6 +22,23 @@ def reverse_words(text: str) -> str: return " ".join(reversed(text.split())) +def is_anagram(first: str, second: str) -> bool: + """Check whether two words or phrases are anagrams of each other. + + Case and spaces are ignored, so phrases with different spacing match. + + Example: + is_anagram("listen", "silent") returns True. + is_anagram("Dormitory", "dirty room") returns True. + is_anagram("hello", "world") returns False. + """ + + def letters(text: str) -> list: + return sorted(ch for ch in text.lower() if not ch.isspace()) + + return letters(first) == letters(second) + + def slugify(text: str) -> str: """Convert text into a URL-friendly slug. diff --git a/tests/test_anagram.py b/tests/test_anagram.py new file mode 100644 index 0000000..b502d64 --- /dev/null +++ b/tests/test_anagram.py @@ -0,0 +1,80 @@ +import pytest + +from fixit.cli import main +from fixit.textutils import is_anagram + + +@pytest.mark.parametrize( + ("first", "second"), + [ + ("listen", "silent"), + ("evil", "vile"), + ("Listen", "SILENT"), + ("dormitory", "dirty room"), + ("The eyes", "They see"), + ("a gentleman", "elegant man"), + ], +) +def test_is_anagram_true(first, second): + assert is_anagram(first, second) + + +@pytest.mark.parametrize( + ("first", "second"), + [ + ("hello", "world"), + ("abc", "abcd"), + ("aab", "abb"), + ("rat", "car"), + ], +) +def test_is_anagram_false(first, second): + assert not is_anagram(first, second) + + +def test_is_anagram_same_word(): + assert is_anagram("stop", "stop") + + +def test_is_anagram_empty_strings(): + assert is_anagram("", "") + + +def test_is_anagram_only_spaces_equals_empty(): + assert is_anagram(" ", "") + + +def test_is_anagram_empty_and_word(): + assert not is_anagram("", "a") + + +def test_is_anagram_keeps_punctuation_and_digits(): + assert is_anagram("a1!", "!1a") + assert not is_anagram("a1", "a2") + + +def test_is_anagram_is_symmetric(): + assert is_anagram("silent", "listen") == is_anagram("listen", "silent") + + +@pytest.mark.parametrize( + ("first", "second", "expected"), + [ + ("listen", "silent", "True"), + ("Dormitory", "dirty room", "True"), + ("hello", "world", "False"), + ], +) +def test_anagram_command(capsys, first, second, expected): + result = main(["anagram", first, second]) + + assert result == 0 + assert capsys.readouterr().out.strip() == expected + + +def test_anagram_command_needs_two_arguments(capsys): + with pytest.raises(SystemExit) as exc: + main(["anagram", "listen"]) + + assert exc.value.code == 2 + assert "second" in capsys.readouterr().err