mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
cleanup
This commit is contained in:
parent
4d9c04e61d
commit
1d476139a8
17 changed files with 250 additions and 1 deletions
38
real_interview_problems/check_anagram.py
Normal file
38
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