Skip to content

Commit 37b5c22

Browse files
author
boraxpr
committed
bite 122 - is_anagram : return true if two words are anagrams - Python built-in functions.
1 parent 111b514 commit 37b5c22

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@
8686
/175/README.md
8787
/175/.cache/v/cache/lastfailed
8888
/147/README.md
89+
/122/README.md

122/anagram.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def is_anagram(word1, word2):
2+
"""Receives two words and returns True/False (boolean) if word2 is
3+
an anagram of word1, ignore case and spacing.
4+
About anagrams: https://en.wikipedia.org/wiki/Anagram"""
5+
words = [word1, word2]
6+
7+
def lower_replace(word: str):
8+
return word.replace(" ", "").lower()
9+
words = list(map(lower_replace, words))
10+
if len(words[0]) != len(words[1]):
11+
return False
12+
char_sets = [set(word) for word in words]
13+
return char_sets[0] == char_sets[1]

122/test_anagram.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# https://en.wikipedia.org/wiki/Anagram
2+
# Anagrams may be created as a commentary on the subject.
3+
# They may be a synonym or antonym of their subject,
4+
# a parody, a criticism or satire.
5+
import pytest
6+
7+
from anagram import is_anagram
8+
9+
10+
@pytest.mark.parametrize("word1, word2", [
11+
("rail safety", "fairy tales"),
12+
("roast beef", "eat for BSE"),
13+
# An anagram which means the opposite of its subject is
14+
# called an "antigram". For example:
15+
("restful", "fluster"),
16+
("funeral", "real fun"),
17+
("adultery", "true lady"),
18+
("customers", "store scum"),
19+
("forty five", "over fifty"),
20+
# They can sometimes change from a proper noun or personal
21+
# name into an appropriate sentence:
22+
("William Shakespeare", "I am a weakish speller"),
23+
("Madam Curie", "Radium came"),
24+
])
25+
def test_is_anagram(word1, word2):
26+
assert is_anagram(word1, word2)
27+
28+
29+
@pytest.mark.parametrize("word1, word2", [
30+
("rail safety", "fairy fun"),
31+
("roast beef", "eat for ME"),
32+
("restful", "fluester"),
33+
("funeral", "real funny"),
34+
("adultery", "true ladie"),
35+
("customers", "store scam"),
36+
("forty five", "over fifty1"),
37+
("William Shakespeare", "I am a strong speller"),
38+
("Madam Curie", "Radium come"),
39+
])
40+
def test_is_not_anagram(word1, word2):
41+
assert not is_anagram(word1, word2)

0 commit comments

Comments
 (0)