Q. 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 will look like:

25, 3, 13, 35, 6, 8, 45, 14


Answer :-

lst = [ 3, 25, 13, 6, 35, 8, 14, 45 ]

for i in range(len(lst)) :
    if lst [ i] % 5 == 0 :
        lst [ i ], lst [i-1] = lst [ i - 1 ] , lst [i]
print(lst)

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post