any computer science student of 12th class plz solve this
Attachments:
Answers
Answered by
4
Question:
Write a function swap2List(arr,n) in Python, that would accept a list arr of n numbers, The functions should modify the content of the List in such a way that the elements, which are multiples of 5 swap with the value present in very next position in the array.
example if the list arr contains:
[11, 15, 7, 18,20, 14]
then after rearrangement the list should contain
[1,7,18, 20, 14, 15]
Language:
Python
Logic:
- Make a function to get array and n
- Run a loop on array
- check if the number is divisible by 5
- if yes swap.
- return the list
Program:
def swap2list(arr,n):
for i in range(0,n-1):
if arr[i]%5==0:
arr[i],arr[i+1]=arr[1+i],arr[i]
return arr
Input\Output:
swap2list([1,15,7,18,20,14],6)
Out[17]: [1, 7, 18, 20, 14, 15]
Attachments:
Attachments:
Similar questions
English,
26 days ago
Computer Science,
26 days ago
Biology,
26 days ago
English,
1 month ago
Chemistry,
9 months ago
India Languages,
9 months ago