Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update anagram.py #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion lib/anagram.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# your code goes here!
# your code goes here!
class Anagram:
def __init__(self, word):
self.word = word.lower()

def match(self, word_list):
return [w for w in word_list if self.is_anagram(w)]

def is_anagram(self, other_word):
other_word_lower = other_word.lower()

# Words of different lengths can't be anagrams
if len(self.word) != len(other_word_lower):
return False

# Anagrams have the same sorted letters
return sorted(self.word) == sorted(other_word_lower)
listen = Anagram("listen")
result = listen.match(['enlists', 'google', 'inlets', 'banana'])
print(result) # Output: ['inlets']