Why i am getting wrong answer for this code
try:
def change(arrr):
for i in range(len(arrr)-1):
val = ((arrr[0]*arrr[1])%(109+7) + arrr[0] + arrr[1])%(109+7)
arrr.append(val)
del arrr[0:2]
print(val)
time = int(input())
while time>0:
arr = []
size = int(input())
for j in range(size):
val = j+1
arr.append(val)
change(arr)
time-=1
except:
pass
In my local editor it is giving proper solution.
try:
def change(arrr):
for i in range(len(arrr)-1):
val = ((arrr[0]*arrr[1])%(10**9+7) + arrr[0] + arrr[1])%(10**9+7)
arrr.append(val)
del arrr[0:2]
print(val)
time = int(input())
while time>0:
arr = []
size = int(input())
for j in range(size):
val = j+1
arr.append(val)
change(arr)
time-=1
except:
pass`Preformatted text`
Close enough (except for the literal Preformatted text that you appended on the end XD)
Your code throws an exception when run with the example testcase
3
1
2
4
but you catch and hide the exception.
Try e.g.
try:
def change(arrr):
for i in range(len(arrr)-1):
val = ((arrr[0]*arrr[1])%(10**9+7) + arrr[0] + arrr[1])%(10**9+7)
arrr.append(val)
del arrr[0:2]
print(val)
time = int(input())
while time>0:
arr = []
size = int(input())
for j in range(size):
val = j+1
arr.append(val)
change(arr)
time-=1
except Exception as exc:
print exc
pass
to help you debug. Or just remove the try … except around the code.