Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
12 changes: 12 additions & 0 deletions fixit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")

Expand Down
17 changes: 17 additions & 0 deletions fixit/textutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
80 changes: 80 additions & 0 deletions tests/test_anagram.py
Original file line number Diff line number Diff line change
@@ -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