+ 2
how do i define SumOfPeak(arr)??
here is the question: https://www.sololearn.com/post/1237462/?ref=app here is my solution: https://code.sololearn.com/cvn3ifb3zD4v/?ref=app
16 Answers
+ 1
#The solution:
def SumOfPeak():
x = input('enter integers: ').split()
sum = 0
if x == []:
return -1
elif len(x) == 1:
return int(x[0])
else:
ls = list(map(lambda x: int(x),x))
for i in range(len(ls)):
if i != len(ls)-1 and i != 0:
if ls[i] > ls[i+1] and ls[i] > ls[i-1]:
sum += ls[i]
if i == len(ls)-1:
if ls[i] > ls[i-1]:
sum += ls[i]
if i == 0:
if ls[i] > ls[i+1]:
sum += ls[i]
return sum
num = int(input('For how many lists do you want Sum of peak? '))
for i in range(num):
print(SumOfPeak())
+ 3
Calvin Thomas yeah
+ 2
def SumOfPeak(arr):
ls=arr
sum = 0
for i in range(len(ls)):
if i != len(ls)-1 and i != 0:
if ls[i] > ls[i+1] and ls[i] > ls[i-1]:
sum += ls[i]
if i == len(ls)-1:
if ls[i] > ls[i-1]:
sum += ls[i]
if i == 0:
if ls[i] > ls[i+1]:
sum += ls[i]
return sum
num = int(input('For how many lists do you want Sum of peak? '))
for i in range(num):
x=input().split()
arr=list(map(lambda y:int(y),x))
print(SumOfPeak(arr))
+ 1
Calvin Thomas This was meant as a question
The problem has some more requests which I couldn't complete. Take a look:
https://www.sololearn.com/post/1237462/?ref=app
+ 1
Harsha S I see, let me just check it out. What should be the output if all the elements in the array are equal?
+ 1
Calvin Thomas that's not mentioned in the problem, consider all elements are different
+ 1
Harsha S Please mention others in your original challenge thread rather than here, if you're inviting them to participate. Else, if you're asking for help, then it's fine.
+ 1
5 is the output
+ 1
Calvin Thomas 5 is the only number in the list which is greater than it's adjacent numbers
+ 1
no, you need to output the sum of all positive peak integers
+ 1
Calvin Thomas the function only accepts positive integers
0
Calvin Thomas thanks for your suggestion
I would like you guys to participate :
Sω(ᗒ✧ᕙ☞Aᕗ✧ᗕ)ti
Jan Markus
NotHuman
Ipang
0
Harsha S What should be the output for this array? :-
[2, 2, 2, 3, 4, 5]
0
Harsha S Could you please explain how you had got the answer?
0
Harsha S Oh, I see, so equal elements shouldn't be considered.
0
Should negative elements also be considered?