-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_repetition.py
113 lines (85 loc) · 3.38 KB
/
longest_repetition.py
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
from UnitaryTest.test_tools import TestTools
# Longest Repetition
def eval_key(key_str):
try:
key = eval(key_str)
except:
key = key_str
return key
def get_max(d, l):
max_value = None
max_key = None
for i in l:
value = d[str(i)]
if not max_value or value > max_value:
max_key = i
max_value = value
return max_key
def create_dict(l):
rept = {}
prev = None
count = 0
for i in l:
if not prev:
prev = i
if i != prev:
rept[str(prev)] = count
count = 0
prev = i
count += 1
rept[str(prev)] = count
return rept
# Takes as input a list, and returns the element in the list that has the most
# consecutive repetitions. If there are multiple elements that
# have the same number of longest repetitions, the result should
# be the one that appears first. If the input list is empty,
# it returns None.
def longest_repetition(l):
count = create_dict(l)
key = get_max(count, l)
return key
# A shorter versions of the above functions
def get_max_pythonic(d):
if not d:
return None
return sorted(d.items(), key=lambda x: x[1], reverse=True)[0][0]
def longest_repetition_pythonic(l):
count = create_dict(l)
key = eval_key(get_max_pythonic(count))
return key
def main():
t = TestTools()
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition([1, 2, 2, 3, 3, 3, 2, 2, 1]), expected=3)
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition(['a', 'b', 'b', 'b', 'c', 'd', 'd', 'd']), expected='b')
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition([1,2,3,4,5]), expected=1)
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition([]), expected=None)
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition([[1], [2, 2], [2, 2], [2, 2], [3, 3, 3]]), expected=[2, 2])
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition([2, 2, 3, 3, 3]), expected=3)
t.new_test(func=longest_repetition)
t.evaluate_result(longest_repetition([2, 2, 2, 3, 3, 3]), expected=2)
# Tests pythonic functions
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic([1, 2, 2, 3, 3, 3, 2, 2, 1]), expected=3)
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic(['a', 'b', 'b', 'b', 'c', 'd', 'd', 'd']), expected='b')
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic([1,2,3,4,5]), expected=1)
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic([]), expected=None)
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic([[1], [2, 2], [2, 2], [2, 2], [3, 3, 3]]), expected=[2, 2])
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic([2, 2, 3, 3, 3]), expected=3)
t.new_test(func=longest_repetition_pythonic)
t.evaluate_result(longest_repetition_pythonic([2, 2, 2, 3, 3, 3]), expected=2)
# Tests create_dict
t.new_test(func=create_dict)
t.evaluate_result(create_dict([1, 2, 2, 3, 3, 3, 2, 2, 1]), expected={'1': 1, '2': 2, '3': 3})
if __name__ == '__main__':
main()