+ 2
Can anyone tell me what's wrong with my code and how to correct it with explanation on how it works please?
https://code.sololearn.com/cnoD7K36S0zM/?ref=app The question is An array Num contains the following elements: 3, 25, 13, 6, 35, 8, 14, 45 Write a function to swap the content with next value divisible by 5 so that the resultant array looks like 25, 3, 13, 35, 6, 8, 45, 14
3 Réponses
+ 2
There's no need of checking the last element. If your last element is divisible by 5, you've got no last+1 th element with whom you can swap.
Here, to me, for loop is better than while. Because you've to say while loop how it will increment. But as the question says, you are asked to check every element (except last) whether it is divisible by 5 or not and then ...
Use this:
for i in range (len(num)-1):
if num[i+1]%5==0:
num[i], num[i+1] = num[i+1], num[i]
+ 1
Faheem Hossain thanks for ur ans. I got it now
0
NotAPythonNinja I still don't get the resultant array but thanks for trying