S1Q3 · Move Even Indices to End Reversed¶
⚡ Quick Reference
Function: move_even_indices_to_end_reversed(t: tuple) -> tuple
Core idea: separate odd-indexed elements (keep order) and even-indexed elements (reverse), then concatenate.
def move_even_indices_to_end_reversed(t):
odd_indexed = t[1::2] # elements at indices 1, 3, 5, …
even_indexed = t[0::2] # elements at indices 0, 2, 4, …
return odd_indexed + even_indexed[::-1]
Key rules:
- t[1::2] → odd-indexed elements in original order
- t[0::2] → even-indexed elements in original order
- [::-1] reverses the even-indexed elements
- Concatenate: odd first, reversed evens last
Problem Statement¶
Problem
Write a function move_even_indices_to_end_reversed(t) that returns a new tuple with:
- Odd-indexed elements in their original relative order
- Even-indexed elements appended at the end in reversed order
Example:
(10, 20, 30, 40, 50)
(20, 40, 50, 30, 10)
Understanding the problem¶
For t = (10, 20, 30, 40, 50):
Step 1 - collect odd-indexed elements (keep order):
Step 2 - collect even-indexed elements and reverse:
Step 3 - concatenate:
Step slicing: t[start::step]
t[0::2]starts at index 0 and takes every 2nd element → even indicest[1::2]starts at index 1 and takes every 2nd element → odd indices
These are the cleanest way to split a sequence by index parity.
Solution approaches¶
def move_even_indices_to_end_reversed(t):
indexed = list(enumerate(t))
odd_vals = tuple(v for i, v in indexed if i % 2 != 0)
even_vals = tuple(v for i, v in indexed if i % 2 == 0)
return odd_vals + even_vals[::-1]
enumerate(t) pairs each value with its index. Filter by parity to separate the two groups, then reverse the evens.
Key takeaways¶
t[0::2] and t[1::2] split by parity
t[0::2] picks every element at an even index; t[1::2] picks every element at an odd index. Together they partition the entire tuple without any loop or condition.
[::-1] reverses a tuple
The step-slice [::-1] works on tuples just like on lists and strings - it returns a reversed copy without modifying the original.
Odd elements keep their order
Only the even-indexed elements are reversed. Odd-indexed elements appear at the front in their original relative order. Read the problem carefully - it's easy to reverse the wrong group.