CBSE · 083Class XII · 2023Section C3 marks

Question 29

30 March 2023 · Computer Science (083)

Write a function EOReplace() in Python, which accepts a list L of numbers. Thereafter, it increments all even numbers by 1 and decrements all odd numbers by 1.

Example : If the input list is :

python
L = [10, 20, 30, 40, 35, 55]

The output will be :

python
L = [11, 21, 31, 41, 34, 54]

Answer

python
def EOReplace(L):
    for i in range(len(L)):
        if L[i] % 2 == 0:
            L[i] = L[i] + 1
        else:
            L[i] = L[i] - 1
    print(L)

Explanation

Iterates the list by index. Even values are incremented by 1, odd values are decremented by 1. Modifying by index updates the list in place.