+ 1
A simple question because i'm 🤕
How it works when entering numbers 0 3 9 8 9 https://code.sololearn.com/cq1ef3tOWL1P/?ref=app
2 Antworten
+ 4
The code searches for the minimum number among the numbers for which the preceding and next elements are both greater than the number itself.
you can rewrite your condition as follow for better understanding:
if a[i-1] > a[i] < a[i+1]:
# cur element is less than both prev and next?
if (k == -1) or (a[i]<k):
# k equals to "invalid" value (-1) or cur element less than k?
k = a[i]
For list [0, 3, 9, 8, 9] the all conditions will be true only on 3rd iteration(when i==3) because
(a[2] > a[3]) and a([3] < a[4]) and (k == -1):
(9 > 8) and (8 < 9) and (-1==-1)
loop iterates for i from 1 to 3 inclusively.
After first and second iterations k will be unchanged (conditions are evaluated to false).
On 3rd iteration k will be set to a[3] = 8
So, the output will be 8
+ 1
Indeed, it was only neccesary to seperate the condition in order to understand 😊
Thank you (๑-﹏-๑)