mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
38
ebook_src/real_interview_problems/check_anagram.py
Normal file
38
ebook_src/real_interview_problems/check_anagram.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def check_if_anagram(word1, word2):
|
||||
counter = Counter()
|
||||
|
||||
for c in word1:
|
||||
counter[c] += 1
|
||||
|
||||
for c in word2:
|
||||
counter[c] -= 1
|
||||
|
||||
for values in counter.values():
|
||||
if values != 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
word1 = 'abc'
|
||||
word2 = 'bca'
|
||||
assert(check_if_anagram(word1, word2) == True)
|
||||
|
||||
word2 = 'bcd'
|
||||
assert(check_if_anagram(word1, word2) == False)
|
||||
|
||||
word1 = ''
|
||||
word2 = ''
|
||||
assert(check_if_anagram(word1, word2) == True)
|
||||
|
||||
word1 = 'a'
|
||||
word2 = 'a'
|
||||
assert(check_if_anagram(word1, word2) == True)
|
Loading…
Add table
Add a link
Reference in a new issue