how do i define SumOfPeak(arr)?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
13th Aug 2021, 5:35 PM
Harsha S
Harsha S - avatar
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())
14th Aug 2021, 1:23 PM
Ahmad Azez Rahhal
Ahmad Azez Rahhal - avatar
14th Aug 2021, 5:51 AM
Harsha S
Harsha S - avatar
+ 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))
13th Aug 2021, 6:21 PM
Abhay
Abhay - avatar
+ 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
14th Aug 2021, 5:31 AM
Harsha S
Harsha S - avatar
+ 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?
14th Aug 2021, 5:33 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas that's not mentioned in the problem, consider all elements are different
14th Aug 2021, 5:34 AM
Harsha S
Harsha S - avatar
+ 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.
14th Aug 2021, 5:38 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
5 is the output
14th Aug 2021, 5:41 AM
Harsha S
Harsha S - avatar
+ 1
Calvin Thomas 5 is the only number in the list which is greater than it's adjacent numbers
14th Aug 2021, 5:48 AM
Harsha S
Harsha S - avatar
+ 1
no, you need to output the sum of all positive peak integers
14th Aug 2021, 5:52 AM
Harsha S
Harsha S - avatar
+ 1
Calvin Thomas the function only accepts positive integers
14th Aug 2021, 5:56 AM
Harsha S
Harsha S - avatar
0
Calvin Thomas thanks for your suggestion I would like you guys to participate : Sω(ᗒ✧ᕙ☞Aᕗ✧ᗕ)ti Jan Markus NotHuman Ipang
14th Aug 2021, 5:33 AM
Harsha S
Harsha S - avatar
0
Harsha S What should be the output for this array? :- [2, 2, 2, 3, 4, 5]
14th Aug 2021, 5:39 AM
Calvin Thomas
Calvin Thomas - avatar
0
Harsha S Could you please explain how you had got the answer?
14th Aug 2021, 5:44 AM
Calvin Thomas
Calvin Thomas - avatar
0
Harsha S Oh, I see, so equal elements shouldn't be considered.
14th Aug 2021, 5:50 AM
Calvin Thomas
Calvin Thomas - avatar
0
Should negative elements also be considered?
14th Aug 2021, 5:51 AM
Calvin Thomas
Calvin Thomas - avatar