-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEncoder.py
More file actions
91 lines (73 loc) · 2.99 KB
/
Copy pathStringEncoder.py
File metadata and controls
91 lines (73 loc) · 2.99 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
"""
Work interview for python developer
convert: a4g2h6 to: aaaagghhhhhh
"""
import unittest
class StringEncoder:
def stage_one(self, st: str) -> str: # brute force
return_string = ''
for i in range(len(st)):
if i % 2 == 1:
for j in range(int(st[i])):
return_string += st[i - 1]
return return_string
def stage_two(self, st: str) -> str: # lets think about optimisation
return_string = ''
for i in range(int(len(st) / 2)):
st_temp = st[2 * i: 2 * (i + 1)]
for j in range(int(st_temp[1])):
return_string += st_temp[0]
return return_string
def stage_three(self, st: str) -> str: # more optimisation
return_string = ''
for i in range(int(len(st) / 2)):
for j in range(int(st[2 * i + 1])):
return_string += st[2 * i]
return return_string
def stage_four(self, st: str) -> str: # let it lok nice too
return_string = ''
for i in range(len(st)):
if i % 2 == 0:
return_string += str(int(st[i + 1]) * st[i])
return return_string
def stage_five(self, st: str) -> str: # final version
return_string = ''
for i in range(int(len(st) / 2)):
return_string += str(int(st[2 * i + 1]) * st[2 * i])
return return_string
def stage_six(self, st: str) -> str: # regexp bonus :P
import re
return_string = ''
for st_temp in re.findall(r"\D+\d+", st):
return_string += str(int(st_temp[1]) * st_temp[0])
return return_string
def stage_functional(self, str:str) -> str:
return ''.join(map(lambda x: x[1] * int(str[x[0] + 1]) if x[0] % 2 == 0 else "", enumerate(list(str))))
class SimpleTestCase(unittest.TestCase):
def setUp(self):
self.string_encoder = StringEncoder()
def testForStageOneEncoder(self):
"""Test case A. note that all test method names must begin with 'test.'"""
s = "a4g2h6"
assert self.string_encoder.stage_one(s) == "aaaagghhhhhh"
def testForStageTwoEncoder(self):
s = "a4g2h6"
assert self.string_encoder.stage_two(s) == "aaaagghhhhhh"
def testForStageThreeEncoder(self):
s = "a4g2h6"
assert self.string_encoder.stage_three(s) == "aaaagghhhhhh"
def testForStageFourEncoder(self):
s = "a4g2h6"
assert self.string_encoder.stage_four(s) == "aaaagghhhhhh"
def testForStageFiveEncoder(self):
s = "a4g2h6"
assert self.string_encoder.stage_five(s) == "aaaagghhhhhh"
def testForStageSixEncoder(self):
s = "a4g2h6"
assert self.string_encoder.stage_six(s) == "aaaagghhhhhh"
def testForStageFunctionalEncoder(self):
"""Test case A. note that all test method names must begin with 'test.'"""
s = "a4g2h6"
assert self.string_encoder.stage_functional(s) == "aaaagghhhhhh"
if __name__ == "__main__":
unittest.main() # run all tests