Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Replace Evens with 1, Odds with 0

Encode even list values as one and odd values as zero in Python. Students trace in-place index assignments and avoid confusing this rule with remainder itself.

IntermediatePhase 4 - Level 3: TransformationExample 4 of 10
replace-evens-with-1-odds-with-0.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Replace
8for i in range(len(arr)):
9 if arr[i] % 2 == 0:
10 arr[i] = 1
11 else:
12 arr[i] = 0
13
14print(f"Modified array: {arr}")

Output

Enter array size: 5
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
Element 5: 6
Modified array: [1, 0, 1, 0, 1]

In this one

  • Encode a boolean property
  • Distinguish indicators from remainders
  • Overwrite original magnitudes

How it works

  1. 1

    Visit every index.

  2. 2

    Assign one for an even current value.

  3. 3

    Assign zero for an odd one and print arr.

Edge cases

What does zero become?

What does zero become?

One, because zero is even.

What does -3 become?

What does -3 become?

Zero, because it is odd.

What's going on

Each stored value is tested before its position is overwritten. Even values become one; the else branch writes zero for odd values.

This is the opposite of simply storing value % 2, which gives zero for evens. Every list position receives an indicator, so the original numeric magnitudes are lost. The length is unchanged and empty input performs no updates.

What does zero become? One, because zero is even.

What does -3 become? Zero, because it is odd.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why is this not just value % 2?

Pick an answer to check it.

Question 2

What does zero become?

Pick an answer to check it.

Questions people ask

What does zero become?

One, because zero is even.

What does -3 become?

Zero, because it is odd.

Why is this not just value % 2?

The desired indicator reverses that remainder mapping.