Informatics Practices
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
Python List Manipulation
4 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
1 Like
Related Questions
Write a program to multiply an element by 2 if it is an odd index for a given list containing both numbers and strings.
Write a program to count the frequency of a given element in a list of numbers.
WAP to shift elements of a list so that first element moves to the second index and second index moves to the third index, etc., and last element shifts to the first position.
Suppose list is [10, 20, 30, 40]
After shifting it should look like: [40, 10, 20, 30]
The record of a student (Name, Roll No, Marks in five subjects and percentage of marks) is stored in the following list:
studRecord = ['Rinku', 'A-36', [56, 98, 99, 72, 69], 78.8]Write Python statements to retrieve the following information from the list studRecord.
(a) Percentage of the student
(b) Marks in the fifth subject
(c) Maximum marks of the student
(d) Roll No. of the student
(e) Change the name of the student from 'Rinku' to 'Rajat'