S1Q2 · Deinterleave Even and Odd Indices in String¶
⚡ Quick Reference
Function: deinterleave(s: str) -> str
Core idea: collect characters at even indices, then odd indices, concatenate.
Key rules:
- s[::2] → characters at indices 0, 2, 4, … (even)
- s[1::2] → characters at indices 1, 3, 5, … (odd)
- Even indices come first, odd indices after
- Indices are 0-based
Problem Statement¶
Problem
Write a function deinterleave(s: str) -> str that returns a new string with all characters at even indices placed first, followed by all characters at odd indices.
Examples:
"abcdef"
"acebdf"
"12345"
"13524"
Understanding the problem¶
Split the string into two groups by index parity, then put even-index characters first:
"abcdef"
Index: 0 1 2 3 4 5
Char: a b c d e f
↑ ↑ ↑ ← even indices: a, c, e
↑ ↑ ↑ ← odd indices: b, d, f
Even first: "ace" + "bdf" = "acebdf"
Step slicing: s[start::step]
Python's slice syntax s[start::step] picks every step-th character starting from start:
s[::2]→ start at 0, step 2 → indices 0, 2, 4, … → even indicess[1::2]→ start at 1, step 2 → indices 1, 3, 5, … → odd indices
This is the most Pythonic and concise way to deinterleave.
Tracing both examples¶
"abcdef"
s[::2] = s[0] + s[2] + s[4] = "a" + "c" + "e" = "ace"
s[1::2] = s[1] + s[3] + s[5] = "b" + "d" + "f" = "bdf"
Result = "ace" + "bdf" = "acebdf"
"12345"
Index: 0 1 2 3 4
Char: 1 2 3 4 5
s[::2] = s[0] + s[2] + s[4] = "1" + "3" + "5" = "135"
s[1::2] = s[1] + s[3] = "2" + "4" = "24"
Result = "135" + "24" = "13524"
Note: for an odd-length string, the even group has one more character than the odd group.
Solution approaches¶
One line. s[::2] grabs even-index characters, s[1::2] grabs odd-index characters. Concatenate and return.
def deinterleave(s: str) -> str:
even_chars = ""
odd_chars = ""
for i, c in enumerate(s):
if i % 2 == 0:
even_chars += c
else:
odd_chars += c
return even_chars + odd_chars
Walk through the string with enumerate to get both index and character. Sort into two buckets, then concatenate.
def deinterleave(s: str) -> str:
even_chars = "".join(s[i] for i in range(0, len(s), 2))
odd_chars = "".join(s[i] for i in range(1, len(s), 2))
return even_chars + odd_chars
Explicitly generate the even and odd index ranges with range(start, len(s), 2). More verbose than slicing but makes the index arithmetic transparent.
def deinterleave(s: str) -> str:
even_chars = "".join(c for i, c in enumerate(s) if i % 2 == 0)
odd_chars = "".join(c for i, c in enumerate(s) if i % 2 != 0)
return even_chars + odd_chars
Filter characters by index parity using enumerate. Two passes through the string - less efficient than slicing but readable.
Key takeaways¶
Step slicing s[::2] and s[1::2]
s[::2] picks every other character starting from index 0 (even indices). s[1::2] picks every other character starting from index 1 (odd indices). Memorise this pair.
enumerate() for index + value
When you need both the index and the character in a loop, use for i, c in enumerate(s) instead of for i in range(len(s)).
Odd-length strings
If len(s) is odd, the even group has one more character than the odd group. Step slicing handles this automatically - no special case needed.