mardi 24 mars 2020

Finding fault in this recursive list addition in python

I need to create a program that takes in an array of numbers, check if each number is greater than the sum of all previous numbers, and output true if the condition is met, and false, if not. My trial is presented below:

import fileinput
a0 = [int(i) for i in fileinput.input()]
a = a0[1:]
b=[]
for i in range(1, a0[0]+1):
  b.append(a[i])
  if (a[i+1] > sum(b)):
    print("true")
    break
  else:
    print ("false")
    break

My program works for half of the test cases but fails for the other test. Could you please help me figure out what i am doing wrongly. Many thanks for your assistance.

Aucun commentaire:

Enregistrer un commentaire