0
Hi. I’m trying to solve a problem where I have to create a function that receives a string of 0 and 1 and a list of commands, which can be 'I' and 'Q'.
If it is 'I' the program must modify all values within the given limits and if it is 'Q' should add the specified widget to a list. For example:
binary_simulation("0011001100", [['I', 1, 10], ['I', 2, 7], ['Q', 2], ['Q', 1], ['Q', 7], ['Q', 5]])
That should bring us back: ['0','1','1','0']
It turns out I’ve already solved the problem, but it exceeds the time limit running the site. I was wondering if there was a way to reduce the execution time of my code. Follow him:
def binary_simulation(s, q):
lst = []
for e in q:
if(e[0] == 'I'):
for j in range(e[1]-1,e[2],1):
if ( s[j] == '1'):
s = s[:j] + '0' + s[j+1:]
else:
s = s[:j] + '1' + s[j+1:]
else:
lst.append(s[e[1]-1])
return lst
Thank you, any help is welcome!
Got it, Thank you very much!!
– DiegoN
@Diegon If this answer solved your problem and you have no further questions, click the " " on the left of the answer to mark it as accepted/correct, which also marks your question as solved/answered. Otherwise, feel free to comment.
– Victor Stafusa