+ 1

Snoballing numbers

You are given a list of numbers in a particular order. You need to check to see if each number is greater than the sum of all the previous number of the list. If they are, you have created successful snowball numbers. Task: 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. Input Format: The first input denotes the length of the list (N). The next N lines contain the list elements as integers. Output Format: A string, true or false. Sample Input: 4 1 3 7 58 Sample Output: true a = int(input()) b = 0 gh = [] while b < a: c = int(input()) gh.append(c) b += 1 for i in gh: if gh[1] > gh[0] and gh[2] > gh[0] + gh[1]: print("true") break else: print("false") break I'm unable to use looping in this problem. Please help.

27th May 2021, 3:59 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
3 ответов
+ 3
# try this: v = int(input()) s = 0 r = True for _ in range(v): x = int(input()) if x<=s: r = False s += x print(str(r).lower())
27th May 2021, 4:35 PM
visph
visph - avatar
+ 2
Hello Swapnil Kamdi You don't know the length, so you should not use hard coded indices. for i in range(len(gh)): or for i,j in enumerate(gh): (i is the index, j the value) is more save But look closer to your problem. You need the sum of the previous values of each element. Here I would say it is much easier to start at the end. 58 > 7 + 3 + 1 + 4 7 > 3 + 1 + 4 ... gbh[-1] is the last element, gbh[-4] the first. And you can work with list slicing. gb[:-1] = [7,3,1,4] gb[:-2] = [3,1,4] ... gb[-1] > sum of gb[:-1]? gb[-2] > sum of gb[:-2]? ... Btw: Python has a sum() function. sum(gh) = 4+1+3+7+58 But it would be also possible to use an inner loop to calculate the sum.
27th May 2021, 4:59 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Thanks visph Thank you Denise Roßberg
27th May 2021, 8:58 PM
Swapnil Kamdi
Swapnil Kamdi - avatar