Question 68 of 97
Q.Write a functions 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 Sample Input data of the list is: L=(10,20,30,40,35,55) Output will be: L=(11,21,31,41,34,54)
Tripura TbseCBSE Class XII Board 2023Subjective· 3mImportance★★★★★
70% · 68/97 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →EOReplace() iterates through the list by index, increments every even number by 1 and decrements every odd number by 1, changing the list in place. For [10, 20, 30, 40, 35, 55] the result is [11, 21, 31, 41, 34, 54].
Concept
The task is a straightforward "modify each element by a rule" problem, a common pattern in CBSE Class 12 Computer Science list programs.
- An even number leaves no remainder when divided by 2:
n % 2 == 0. - An odd number leaves a remainder of 1:
n % 2 != 0.
To change the elements of the original list in place, we iterate by index using range(len(L)) so that we can assign back to L[i].
The function
def EOReplace(L):
for i in range(len(L)):
if L[i] % 2 == 0: # even number
L[i] = L[i] + 1
else: # odd number
L[i] = L[i] - 1
Dry run for L = [10, 20, 30, 40, 35, 55]
| i | L[i] | Even/Odd | New value |
|---|---|---|---|
| 0 | 10 | even | 11 |
| 1 | 20 | even | 21 |
| 2 | 30 | even | 31 |
| 3 | 40 | even | 41 |
| 4 | 35 | odd | 34 |
| 5 | 55 | odd | 54 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.