+ 2
Why
What is the problem in 9 line?š¤ https://code.sololearn.com/c92jhNvlMemf/?ref=app
13 Answers
+ 3
the problem is that you try to acces a[i-1] before testing if i<n
try this instead
while i<n and a[i]>a[i-1]:
in Python, a[-1] will return the last element of the list
+ 2
In the starting of the loop "i"=0 and you are checking ( a[i]>a[i-1] ) here " i-1" is "-1" which is not possible as array index starts from 0
+ 2
yes, the order matters. as it's a 'and' test, if the first condition is not satisfied, the second will not be tested
but you solved it!
+ 1
the conditions are tested from left to right, so the order matters.
+ 1
but itās an operator "and", not "or", if even one condition is false, then thatās it, no matter how you change their places. is not it so?
+ 1
when you test
a[i]>a[i-1] and i<n
it will first try to get a[i] and a[i-1] then compare them, but if i>=n, a[i] will raise an error and the program will crash
but if it's
i<n and a[i] >a[i-1]
it will first test if i<n, if this condition is not satisfied, it will not try to acces a[i].
+ 1
šš
0
Does the order of conditions in while loop change the essence?...
0
to be honest, I didnāt understand anything, removed the while loop and now everything works as it should
0
Well, yes, if one of the conditions is not satisfied, then the second one does not need to be checked. is logical. but thereās no sense in changing places, isnāt that so? š
0
for sure! cool ahahahaa
it finally dawned on me
0
thank youš
0
Why what? And where