Computer Science, asked by akankhya1426, 6 hours ago

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 CrazyKanav
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 anindyaadhikari13
6

\texttt{\textsf{\large{\underline{Solution}:}}}

The given co‎de 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 co‎de for the question -

listreplace=lambda arr:[100 if i%2 else i*10 for i in arr]

\texttt{\textsf{\large{\underline{Sample I/O}:}}}

print(listreplace([1,2,3,4,5,6,7,8,9,10]))

>> [100, 20, 100, 40, 100, 60, 100, 80, 100, 100]

\texttt{\textsf{\large{\underline{Logic}:}}}

  • 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