Computer Science
A list Num contains the following elements:
3, 25, 13, 6, 35, 8, 14, 45
Write a program to swap the content with the next value divisible by 5 so that the resultant list will look like:
25, 3, 13, 35, 6, 8, 45, 14
Python
Python List Manipulation
6 Likes
Answer
Num = [3, 25, 13, 6, 35, 8, 14, 45]
for i in range(len(Num) - 1):
if Num[i] % 5 != 0 and Num[i + 1] % 5 == 0:
Num[i], Num[i + 1] = Num[i + 1], Num[i]
print("Resultant List:", Num)
Output
Resultant List: [25, 3, 13, 35, 6, 8, 45, 14]
Answered By
2 Likes
Related Questions
Write a program to count the frequency of a given element in a list of numbers.
Write a program rotates the elements of a list so that the element at the first index moves to the second index, the element in the second index moves to the third index, etc., and the element in the last index moves to the first index.
Write a program to accept values from a user in a tuple. Add a tuple to it and display its elements one by one. Also display its maximum and minimum value.
Write a program to input any values for two tuples. Print it, interchange it and then compare them.