Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 0bc8b38

Browse files
committed
Merge pull request #32 from abhikandoi2000/master
Implemented anagram detection in Python.
2 parents 09bb000 + d5c173c commit 0bc8b38

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Simple function that will take a string of latin characters and return a unique hash
2+
def hashString(str):
3+
# Map characters to prime numbers to multiply
4+
charMap = {
5+
'a': 2,
6+
'b': 3,
7+
'c': 5,
8+
'd': 7,
9+
'e': 11,
10+
'f': 13,
11+
'g': 17,
12+
'h': 19,
13+
'i': 23,
14+
'j': 29,
15+
'k': 31,
16+
'l': 37,
17+
'm': 41,
18+
'n': 43,
19+
'o': 47,
20+
'p': 53,
21+
'q': 59,
22+
'r': 61,
23+
's': 67,
24+
't': 71,
25+
'u': 73,
26+
'v': 79,
27+
'w': 83,
28+
'x': 89,
29+
'y': 97,
30+
'z': 101,
31+
'A': 103,
32+
'B': 107,
33+
'C': 109,
34+
'D': 113,
35+
'E': 127,
36+
'F': 131,
37+
'G': 137,
38+
'H': 139,
39+
'I': 149,
40+
'J': 151,
41+
'K': 163,
42+
'L': 167,
43+
'M': 173,
44+
'N': 179,
45+
'O': 181,
46+
'P': 191,
47+
'Q': 193,
48+
'R': 197,
49+
'S': 199,
50+
'T': 211,
51+
'U': 223,
52+
'V': 227,
53+
'W': 229,
54+
'X': 233,
55+
'Y': 239,
56+
'Z': 241
57+
}
58+
59+
return reduce(lambda memo, char: memo * charMap[char], list(str), 1);
60+
61+
def anagramDetection(parent, child):
62+
length = len(child)
63+
anagram = hashString(child)
64+
total = 0
65+
66+
for i in range(0, len(parent) - length):
67+
if hashString(parent[i: i + length]) == anagram:
68+
total = total + 1
69+
70+
return total

0 commit comments

Comments
 (0)