-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.py
More file actions
172 lines (120 loc) · 3.68 KB
/
Copy pathanagram.py
File metadata and controls
172 lines (120 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Anagram functions
Algorithms with different orders of magnitude
"""
import unittest
from collections import OrderedDict
def anagram_first(first, second):
"""
Strategy: Tick off
"""
if first == second:
return True
if not len(first) == len(second):
return False
for char in first:
if char in second:
second = second.replace(char, '-', 1)
first = first.replace(char, '-', 1)
def is_hyphens(a_str):
return all(map(lambda x: x == '-', a_str))
if is_hyphens(first) and is_hyphens(second):
return True
else:
return False
def anagram_tick_off(first, second):
first = [char for char in first]
idx = 0
ok = True
while idx < len(first) and ok:
jdx = 0
found = False
while jdx < len(second) and not found:
if first[idx] == second[jdx]:
found = True
else:
jdx += 1
if not found:
ok = False
idx += 1
return ok
def anagram_sort_and_compare(first, second):
if first == second:
return True
if not len(first) == len(second):
return False
first = list(first)
second = list(second)
first.sort()
second.sort()
for char_a, char_b in zip(first, second):
if not char_a == char_b:
return False
return True
def anagram_count_and_compare_first(first, second):
# sacrifice space for time
if first == second:
return True
if not len(first) == len(second):
return False
def count_chars(string):
res = OrderedDict()
for char in first:
if char in res:
res[char] += 1
else:
res[char] = 1
return res
first_res = count_chars(first)
second_res = count_chars(second)
for count_one, count_two in zip(first_res.values(), second_res.values()):
if not count_one == count_two:
return False
return True
def anagram_count_and_compare(first, second):
# init letter counts
letter_count_a = [0]*26
letter_count_b = [0]*26
for char in first:
idx = ord(char) - ord('a')
current = letter_count_a[idx]
letter_count_a[idx] = current + 1
for char in second:
idx = ord(char) - ord('a')
current = letter_count_b[idx]
letter_count_b[idx] = current + 1
for i in range(26):
if not letter_count_a[i] == letter_count_b[i]:
return False
return True
class TestAnagram(unittest.TestCase):
tests = [
('', '', True), # empty stings
('abc', 'abc', True), # identical strings
('abcd', 'abc', False), # strings of unequal length
('heart', 'earth', True),
('python', 'typhon', True),
('apple', 'pleap', True),
]
@classmethod
def _wrap(cls, anagram_function):
def inner():
for arg1, arg2, res in cls.tests:
got = anagram_function(arg1, arg2)
err_msg = '"{}" "{}" failed, expected "{}"'.format(
arg1, arg2, res
)
assert got == res, err_msg
return inner
def test_anagram_first(self):
self._wrap(anagram_first)()
def test_anagram_tick_off(self):
self._wrap(anagram_tick_off)()
def test_anagram_sort_and_compare(self):
self._wrap(anagram_sort_and_compare)()
def test_anagram_count_and_compare_first(self):
self._wrap(anagram_count_and_compare_first)()
def test_anagram_count_and_compare(self):
self._wrap(anagram_count_and_compare)()
if __name__ == '__main__':
unittest.main()