Created
June 13, 2019 18:54
-
-
Save sharmaeklavya2/a0a5f718f0f0b3fbc4d9ea9839757753 to your computer and use it in GitHub Desktop.
phrase anagram checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example invocations:
python3 phrase_anagram_checker.py < sample_input.txt
:python3 phrase_anagram_checker.py --debug < sample_input.txt
: