CodeJam 2020 Problem 2

I thought of sharing my python code for code jam 2020, incase it would help anyone. I have explained it below. Also, if anyone has a efficient solution. I would love to know.

for tests in range(0, int(input())):
s = list(input())
highest=[] # to store the values as in integer
for j in s:
highest.append(int(j))
maximum=max(highest) # explained later
new_list = [] # to store the values after adding parenthesis
for i in s:
if i == ‘0’:
new_list.append(i) # need not to change anything if 0 is there in main number
else:
new_list.append(int(i) * ‘(’) # adding exactly same number of ‘(’ as the integer
new_list.append(i)
new_list.append(int(i)*’)’)
new_str=’’.join(new_list)
for k in range(0,maximum): # suppose number is 221. new_str=’((2))((2))(1)’
list_new = new_str.split(’)(’) # split it where ‘)(’ is present cuz we dont need it.
new = ‘’.join(list_new)
new_str=new # storing the new string in older one. Till we get rid of all ‘)(’.
print(‘Case #{}: {}’.format(tests+1,new_str))

I’ll explain the use of maximum with an example.
Suppose we have 541. First my code would create a string - ‘(((((5)))))((((4))))(1)’. If you closely look, there are maximum ‘)(’ between 5 and 4(which can’t be more than 5). So we can get rid of it iterating 5 times.

Check this out ! It’s detailed.