Answer to Question #347480 in Python for HH rurh

Question #347480

given n and read the next n lines as integers and convert n integers as array. calculate the sum of its prefixes at each index and return the number of positive sum of prefixes.



For Example :


n = 4, next n lines 1, 3, 0, -5


read inputs as array = [-9,3,0,1,-1]


converts into _____ format Like


[3, 1, 0, -9, -1]


Then calculate sum of prefixes like


[3, 4, 4, -5,-6]


Now count the positive numbers above the list is 3.


so output is 3





1
Expert's answer
2022-06-06T14:58:43-0400
a = []
for i in range(int(input('Enter n: '))):
    a.append(int(input()))
res1 = [i for i in a if i > -1]
res2 = [i for i in a if i < 0]
a = sorted(res1, reverse=True) + sorted(res2)
print(f'Converted Array: {a}')
result = []
result.append(a[0])
for i in range(1, len(a)): 
    result.append(result[-1] + a[i])
print(f'Prefix sums {result}')
print(f'{len([i for i in result if i >= 0])} positive numbers')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS