Skip to content

Instantly share code, notes, and snippets.

@sharmaeklavya2
Created June 13, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharmaeklavya2/a0a5f718f0f0b3fbc4d9ea9839757753 to your computer and use it in GitHub Desktop.
Save sharmaeklavya2/a0a5f718f0f0b3fbc4d9ea9839757753 to your computer and use it in GitHub Desktop.
phrase anagram checker
#!/usr/bin/env python3
"""Takes multiple lines as input and checks if they are anagrams."""
import re
import hashlib
import argparse
def normalize(s):
return ''.join(sorted(''.join(re.findall(string=s.lower(), pattern=r'\w+'))))
def hashhex(x):
return hashlib.md5(x.encode('utf-8')).hexdigest()[:8]
def preview(s, n):
if len(s) <= n:
return s
else:
dots = '...'
return s[:n - len(dots)] + '...'
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--debug', action='store_true', default=False)
args = parser.parse_args()
prev_x = None
try:
while True:
s = input()
x = normalize(s)
parts = [hashhex(x), preview(s, 30)]
if args.debug:
parts.append(x)
print(*parts, sep=' ')
if prev_x is not None:
if x == prev_x:
print(' same as previous')
else:
print(' different from previous')
prev_x = x
except EOFError:
pass
if __name__ == '__main__':
main()
Great alchemist! Transform this dull, worthless lead and wood into something valuable. Make me rich.
Lo and behold! True value is not in rocks that glimmer. A man cheats himself with metals. Words are gold!
Hah! I am broke as hell, trust me. I need cash, not drama or truths! Wisdom cannot fill wallets. Give me gold.
I am lord voldemort
Tom Marvolo Riddle
@sharmaeklavya2
Copy link
Author

sharmaeklavya2 commented Jun 13, 2019

Example invocations:

python3 phrase_anagram_checker.py < sample_input.txt:

d1739ad5    Great alchemist! Transform ...
d1739ad5    Lo and behold! True value i...
  same as previous
d1739ad5    Hah! I am broke as hell, tr...
  same as previous
d2813bda    I am lord voldemort
  different from previous
d2813bda    Tom Marvolo Riddle
  same as previous

python3 phrase_anagram_checker.py --debug < sample_input.txt:

d1739ad5    Great alchemist! Transform ...    aaaaaaaabccddddeeeeeeeefgghhhhhiiiiiklllllllmmmmmnnnnoooooorrrrrsssssstttttttuuvww
d1739ad5    Lo and behold! True value i...    aaaaaaaabccddddeeeeeeeefgghhhhhiiiiiklllllllmmmmmnnnnoooooorrrrrsssssstttttttuuvww
  same as previous
d1739ad5    Hah! I am broke as hell, tr...    aaaaaaaabccddddeeeeeeeefgghhhhhiiiiiklllllllmmmmmnnnnoooooorrrrrsssssstttttttuuvww
  same as previous
d2813bda    I am lord voldemort    addeillmmooorrtv
  different from previous
d2813bda    Tom Marvolo Riddle    addeillmmooorrtv
  same as previous
d2813bda    Mr. Tom, a dildo lover    addeillmmooorrtv
  same as previous

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment