I attempted ACODE problem and wrote the following python program
while True:
i = input().strip()
if i[0] == '0':
break
l = [int(x) for x in i]
a = [0] * len(l)
if l[-1] != 0:
a[-1] = 1
if l[-2]:
a[-2] = 1
if l[-2] * 10 + l[-1] <= 26:
a[-2] += a[-1]
for e in range(-3, -len(l)-1, -1):
if l[e] == 0:
continue
a[e] += a[e+1]
if (l[e] * 10) + l[e+1] <= 26:
a[e] += a[e+2]
print(a[0])
`
The above program runs fine on my computer but gives NZEC when I submit it. After getting a couple of NZECs I came upon a python program on Stackoverflow that used almost the same logic as my program.
`def acode_second_try(s):
n = len(s)
f = [0 for i in range(1 + n)]
f[n] = 1
for i in range(n - 1, -1, -1):
if s[i] != '0':
f[i] = f[1 + i]
if 1 + i < n:
if 10 * int(s[i]) + int(s[1 + i]) ≤ 26:
f[i] += f[2 + i]
return f[0]
def main():
i = input().strip()
while i[0] != '0':
print(acode_second_try(i))
#print(acode(i))
i = input().strip()
if __name__ == "__main__":
import sys
`
So just to check if the program works, I submitted it and it got accepted. I really do not understand why the second one worked and mine did not, as both the programs are using the same logic. Please help me understand the flaw in my program