Why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
16th Apr 2020, 11:02 AM
shiryaeva
shiryaeva - avatar
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
16th Apr 2020, 11:17 AM
John Robotane
John Robotane - avatar
+ 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
16th Apr 2020, 11:06 AM
Arsenic
Arsenic - avatar
+ 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!
16th Apr 2020, 11:46 AM
John Robotane
John Robotane - avatar
+ 1
the conditions are tested from left to right, so the order matters.
16th Apr 2020, 12:19 PM
John Robotane
John Robotane - avatar
+ 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?
16th Apr 2020, 12:32 PM
shiryaeva
shiryaeva - avatar
+ 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].
16th Apr 2020, 12:39 PM
John Robotane
John Robotane - avatar
+ 1
🙏🙏
16th Apr 2020, 12:49 PM
John Robotane
John Robotane - avatar
0
Does the order of conditions in while loop change the essence?...
16th Apr 2020, 11:40 AM
shiryaeva
shiryaeva - avatar
0
to be honest, I didn’t understand anything, removed the while loop and now everything works as it should
16th Apr 2020, 11:42 AM
shiryaeva
shiryaeva - avatar
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? 🙃
16th Apr 2020, 12:11 PM
shiryaeva
shiryaeva - avatar
0
for sure! cool ahahahaa it finally dawned on me
16th Apr 2020, 12:46 PM
shiryaeva
shiryaeva - avatar
0
thank you🌚
16th Apr 2020, 12:47 PM
shiryaeva
shiryaeva - avatar
0
Why what? And where
17th Apr 2020, 9:42 PM
.exe
.exe - avatar