mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
add boilerplate for second edition
This commit is contained in:
parent
dc3ebf3173
commit
5fafebba15
279 changed files with 24265 additions and 0 deletions
51
first_edition/ebook_src/builtin_structures/anagram.py
Executable file
51
first_edition/ebook_src/builtin_structures/anagram.py
Executable file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def is_anagram(s1, s2):
|
||||
'''
|
||||
>>> is_anagram('cat', 'tac')
|
||||
True
|
||||
>>> is_anagram('cat', 'hat')
|
||||
False
|
||||
'''
|
||||
counter = Counter()
|
||||
for c in s1:
|
||||
counter[c] += 1
|
||||
|
||||
for c in s2:
|
||||
counter[c] -= 1
|
||||
|
||||
for i in counter.values():
|
||||
if i:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
''' verify if words are anagrams by comparing a sum of Unicode code
|
||||
point of the character'''
|
||||
|
||||
def get_unicode_sum(word):
|
||||
s = 0
|
||||
for p in word:
|
||||
s += ord(p)
|
||||
return s
|
||||
|
||||
|
||||
def is_anagram2(word1, word2):
|
||||
'''
|
||||
>>> is_anagram2('cat', 'tac')
|
||||
True
|
||||
>>> is_anagram2('cat', 'hat')
|
||||
False
|
||||
'''
|
||||
return get_unicode_sum(word1) == get_unicode_sum(word2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue