forked from Nandini13-rgb/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happy_string.py
38 lines (36 loc) · 959 Bytes
/
happy_string.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
def happy_string(input,elements):
def findindex(element, input):
for i in range(len(input)):
if input[i] == element:
return i
max_index = findindex(max(input), input)
#print(max_index)
n = input[max_index]
output = []
# n = input[max_index]
while n:
output.append(elements[max_index])
n -= 1
#print(output)
input.pop(max_index)
elements.pop(max_index)
index = 2
while input[0] > 0:
output.insert(index, elements[0])
index += 3
input[0] -= 1
ndex = 3
while input[1] > 0:
output.insert(index,elements[1])
index += 4
input[1] -= 1
j = len(output)-1
if output[j] == output[j-1]:
if output[j] == output[j-2]:
output.pop(j)
output.pop(j-1)
output.pop(j-2)
return output
input = [7, 7, 7]
elements = ['a', 'b', 'c']
print(happy_string(input,elements))