Can you explain me why outpot is so?
Question:
if __name__ == '__main__':
N = int(input())
lst = []
nums = []
for i in range(N):
a = input()
temp = a.split()
if 'insert' in temp:
lst.insert(int(temp[1]), int(temp[2]))
elif 'print' in temp:
nums.append(lst)
elif 'remove' in temp:
del lst[lst.index(int(temp[1]))]
elif 'append' in temp:
lst.append(int(temp[1]))
elif 'sort' in temp:
lst.sort()
elif 'pop' in temp:
lst.pop(-1)
elif 'reverse' in temp:
lst = lst.reverse()
for i in nums:
print(i)
Input
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
Output
[9, 5, 1]
[9, 5, 1]
None
Expected output
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
I am doing task on HackerRank and I almost have done it, but suddenly in every loop in ‘for i in range(N)’ program add the last list three times independent on Input.
I have tried a lot of debug tests, but I can’t find mistake in my script.
Answers:
Two things that need to change:
lst.reverse()
instead of lst = lst.reverse()
- As you are appending, removing etc. to
lst
, the list that you saved in nums
will change with it. You need to use nums.append(lst.copy())
instead of nums.append(lst)
The full code becomes:
if __name__ == '__main__':
N = int(input())
lst = []
nums = []
for i in range(N):
a = input()
temp = a.split()
if 'insert' in temp:
lst.insert(int(temp[1]), int(temp[2]))
elif 'print' in temp:
nums.append(lst.copy())
elif 'remove' in temp:
del lst[lst.index(int(temp[1]))]
elif 'append' in temp:
lst.append(int(temp[1]))
elif 'sort' in temp:
lst.sort()
elif 'pop' in temp:
lst.pop(-1)
elif 'reverse' in temp:
lst.reverse()
for i in nums:
print(i)
if __name__ == '__main__':
N = int(input())
lst = []
nums = []
for i in range(N):
a = input()
temp = a.split()
if 'insert' in temp:
lst.insert(int(temp[1]), int(temp[2]))
elif 'print' in temp:
nums.append(lst)
elif 'remove' in temp:
del lst[lst.index(int(temp[1]))]
elif 'append' in temp:
lst.append(int(temp[1]))
elif 'sort' in temp:
lst.sort()
elif 'pop' in temp:
lst.pop(-1)
elif 'reverse' in temp:
lst = lst.reverse()
for i in nums:
print(i)
Input
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
Output
[9, 5, 1]
[9, 5, 1]
None
Expected output
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
I am doing task on HackerRank and I almost have done it, but suddenly in every loop in ‘for i in range(N)’ program add the last list three times independent on Input.
I have tried a lot of debug tests, but I can’t find mistake in my script.
Two things that need to change:
lst.reverse()
instead oflst = lst.reverse()
- As you are appending, removing etc. to
lst
, the list that you saved innums
will change with it. You need to usenums.append(lst.copy())
instead ofnums.append(lst)
The full code becomes:
if __name__ == '__main__':
N = int(input())
lst = []
nums = []
for i in range(N):
a = input()
temp = a.split()
if 'insert' in temp:
lst.insert(int(temp[1]), int(temp[2]))
elif 'print' in temp:
nums.append(lst.copy())
elif 'remove' in temp:
del lst[lst.index(int(temp[1]))]
elif 'append' in temp:
lst.append(int(temp[1]))
elif 'sort' in temp:
lst.sort()
elif 'pop' in temp:
lst.pop(-1)
elif 'reverse' in temp:
lst.reverse()
for i in nums:
print(i)