Write a function listreplace (ARR) in Python, which accepts a list ARR of numbers, the function will replace the odd number by value 100 and multiply even number by 10. Sample Input Data of the list is: a=[10,20,23,45]
listreplace(a,4) output:
[100, 200, 123, 145]
Answers
Answered by
6
Answer:
def listreplace(mylist):
mylist2 = []
for num in mylist:
if num % 2 != 0:
num = 100
mylist2.append(num)
else:
num = num * 10
mylist2.append(num)
return mylist2
print(listreplace([10,20,23,45]))
Explanation:
Answered by
6
The given code is written in Python.
def listreplace(arr):
for i in range(len(arr)):
if arr[i]%2==0:
arr[i]*=10
else:
arr[i]=100
return arr
One line code for the question -
listreplace=lambda arr:[100 if i%2 else i*10 for i in arr]
print(listreplace([1,2,3,4,5,6,7,8,9,10]))
>> [100, 20, 100, 40, 100, 60, 100, 80, 100, 100]
- Logic for the program is very simple. Transverse through the array elements, if the number in the list is odd, replace the element with the value of 100 or else, multiply the element with 10 and store it.
- At last, return the modified array.
Attachments:
Similar questions
Math,
15 days ago
Hindi,
15 days ago
Accountancy,
15 days ago
Hindi,
1 month ago
Physics,
8 months ago
Business Studies,
8 months ago
English,
8 months ago