A list Divisibility contains the following elements:

9, 35, 4, 19, 55, 12, 55, 36

Write a program to swap the content with next value divisible by 5 so that the resultant list will look like

9,4, 35, 19, 12, 55, 36, 55

 

Answer:

Code:

Divisibility = [ 9 , 35 , 4 , 19 , 55 , 12 , 55 , 36

n = len(Divisibility) 

i = 0  

 

while i<n: 

    if Divisibility[i]% 5 == 0

        temp=Divisibility[i] 

        Divisibility[i]=Divisibility[i+ 1

        Divisibility[i+ 1 ]=temp

        i = i + 2  

    else: 

i = i + 1  

     

print(Divisibility)

 

[Computer Science] A list Divisibility contains the following elements - Long Answer Type Questions (5 Marks each)

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.