+ 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

20th Nov 2020, 5:38 PM
Sam
Sam - avatar
3 odpowiedzi
+ 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]
20th Nov 2020, 7:37 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 1
Faheem Hossain thanks for ur ans. I got it now
27th Nov 2020, 6:47 PM
Sam
Sam - avatar
0
NotAPythonNinja I still don't get the resultant array but thanks for trying
20th Nov 2020, 5:53 PM
Sam
Sam - avatar