Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- from itertools import islice, tee
- def sliding_counter(iterable, window):
- 'generates counts for iterable in window, starting at position 0, 1, ...'
- ita, itb = tee(iter(iterable))
- c = Counter(islice(itb, window))
- while 1:
- yield c
- c[next(ita)] -= 1
- c[next(itb)] += 1
- def is_anagram(first, second):
- 'ís a part of the second word an anagram of the first word?'
- target = Counter(first)
- return any(not (a - target) for a in sliding_counter(second, len(first)))
- print is_anagram("trel", "appletree") # True
- print is_anagram("reel", "appletree") # False
- print is_anagram("diep", "parallellepipedum") # True
- print is_anagram("dier", "parallellepipedum") # False
- print is_anagram("pparallelle", "parallellepipedum") # True
- print is_anagram(" gnimP", "Programming Praxis") # True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement