-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q05_letters_and_numbers.py
49 lines (39 loc) · 1.42 KB
/
Q05_letters_and_numbers.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
import unittest
def letters_and_numbers(arr):
binary_arr = convert(arr)
diff = 0
first_idx_with_diff_value = {0: 0}
max_idx_pair = (0, 0)
max_arr_length = 0
for i, x in enumerate(binary_arr):
if x:
diff += 1
else:
diff -= 1
if diff in first_idx_with_diff_value:
pair_idx = first_idx_with_diff_value[diff]
if i - pair_idx > max_arr_length:
max_arr_length = i - pair_idx
max_idx_pair = (pair_idx, i)
else:
first_idx_with_diff_value[diff] = i + 1
return arr[max_idx_pair[0] : max_idx_pair[1] + 1]
def convert(arr):
res = [None] * len(arr)
for i, x in enumerate(arr):
if x.isalpha():
res[i] = 1
elif x.isnumeric():
res[i] = 0
return res
class Test(unittest.TestCase):
def test_convert(self):
arr = ["1", "a", "2", "3", "b", "c", "2", "1"]
self.assertEqual([0, 1, 0, 0, 1, 1, 0, 0], convert(arr))
def test_letters_and_numbers(self):
arr = ["1", "a", "2", "3", "b", "c", "2", "1"]
self.assertEqual(["1", "a", "2", "3", "b", "c"], letters_and_numbers(arr))
arr = ["b", "a", "2", "3", "b", "c", "2", "1"]
self.assertEqual(arr, letters_and_numbers(arr))
arr = ["a", "b", "c", "1", "1", "b", "2", "c"]
self.assertEqual(["b", "c", "1", "1", "b", "2",], letters_and_numbers(arr))