Replace Evens with 1, Odds with 0

Replace even elements with 1, odd with 0.

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]

What's going on

Transform based on even/odd.

Key Concepts:

Check even/odd
Replace with 1 or 0
Binary transformation