Created
April 25, 2021 18:39
-
-
Save tonogram/9b81a162ff57882f3ddf38cd21474f60 to your computer and use it in GitHub Desktop.
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
# https://twitter.com/fermatslibrary/status/1385957963429515266 | |
import tables | |
from strutils import toLower | |
const PrimeNumbers = toTable [ | |
('a', 2), | |
('b', 3), | |
('c', 5), | |
('d', 7), | |
('e', 11), | |
('f', 13), | |
('g', 17), | |
('h', 19), | |
('i', 23), | |
('j', 29), | |
('k', 31), | |
('l', 37), | |
('m', 41), | |
('n', 43), | |
('o', 47), | |
('p', 53), | |
('q', 59), | |
('r', 61), | |
('s', 67), | |
('t', 71), | |
('u', 73), | |
('v', 79), | |
('w', 83), | |
('x', 89), | |
('y', 97), | |
('z', 101) | |
] | |
proc g(c: char): int = | |
try: return PrimeNumbers[c] | |
except KeyError: return 0 | |
proc isAnagram*(a, b: string): bool = | |
var x, y: int | |
for c in a.toLower: x += g(c) | |
for c in b.toLower: y += g(c) | |
return x == y | |
when isMainModule: | |
assert isAnagram("fRiEd", "FiReD") | |
assert isAnagram("gAiNlY", "LaYiNg") | |
assert isAnagram("sAdDeR", "DrEaDs") | |
assert isAnagram("lIsTeN", "SiLeNt") | |
assert not isAnagram("Not an", "anagram") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment