Construct a Python program that reverses an array. [1, 5, 3, 10, 4, 8]
- User
Construct a Python program that reverses an array. [1, 5, 3, 10, 4, 8]
Assistant
Construct a Python program that reverses an array. [1, 5, 3, 10, 4, 8]
Problem: Construct a Python program that reverses an array. [1, 5, 3, 10, 4, 8]
Approach: | Time | Space | In-Place | Notes |
Slicing [::-1] | O(n) | O(n) | No | Most Pythonic, concise |
| list.reverse() | O(n) | O(1) | Yes | Built-in, modifies original |
| Two-Pointer Swap | O(n) | O
Key considerations:
Args: arr: List to reverse in_place: If True, modify original; if False, return new list
Returns: Reversed list (new
Complexity: O(n)
Let me implement this solution.
Reverse an array [1, 5, 3, 10, 4, 8] to produce [8, 4, 10, 3, 5, 1].
| Approach | Time | Space | In-Place | Notes |
|---|---|---|---|---|
Slicing [::-1] | O(n) | O(n) | No | Most Pythonic, concise |
list.reverse() | O(n) | O(1) | Yes | Built-in, modifies original |
| Two-Pointer Swap | O(n) | O(1) | Yes | Educational, no built-ins |
reversed() | O(n) | O(n) | No | Returns iterator |
I'll implement a modular solution that demonstrates:
This balances algorithmic clarity with Python best practices.
def reverse_array(arr, in_place=False):
"""
Reverse an array using the two-pointer technique.
Args:
arr: List to reverse
in_place: If True, modify original; if False, return new list
Returns:
Reversed list (new list if in_place=False, same list if in_place=True)
Time Complexity: O(n)
Space Complexity: O(1) if in_place, O(n) otherwise
"""
# Work on original or copy based on in_place flag
result = arr if in_place else arr.copy()
# Two-pointer technique: swap elements from ends toward center
left, right = 0, len(result) - 1
while left < right:
# Swap elements at left and right positions
result[left], result[right] = result[right], result[left]
left += 1
right -= 1
return result
def reverse_array_pythonic(arr):
"""
Pythonic one-liner using slicing.
Most concise approach for practical use.
"""
return arr[::-1]
# ============== DEMONSTRATION ==============
if __name__ == "__main__":
original = [1, 5, 3, 10, 4, 8]
print(f"Original array: {original}")
print("-" * 40)
# Method 1: Returns new reversed array (preserves original)
reversed_new = reverse_array(original, in_place=False)
print(f"Method 1 (new array): {reversed_new}")
print(f"Original preserved: {original}")
# Method 2: In-place reversal
arr_copy = original.copy()
reverse_array(arr_copy, in_place=True)
print(f"\nMethod 2 (in-place): {arr_copy}")
# Method 3: Pythonic slicing
reversed_slice = reverse_array_pythonic(original)
print(f"Method 3 (slicing): {reversed_slice}")
# Verify all methods produce same result
print("-" * 40)
print(f"✓ All methods equal: {reversed_new == arr_copy == reversed_slice}")
Original array: [1, 5, 3, 10, 4, 8]
----------------------------------------
Method 1 (new array): [8, 4, 10, 3, 5, 1]
Original preserved: [1, 5, 3, 10, 4, 8]
Method 2 (in-place): [8, 4, 10, 3, 5, 1]
Method 3 (slicing): [8, 4, 10, 3, 5, 1]
----------------------------------------
✓ All methods equal: True
Input: [1, 5, 3, 10, 4, 8]
↑ ↑
left right
Step 1: Swap index 0↔5 → [8, 5, 3, 10, 4, 1]
Step 2: Swap index 1↔4 → [8, 4, 3, 10, 5, 1]
Step 3: Swap index 2↔3 → [8, 4, 10, 3, 5, 1]
left ≥ right → STOP
Output: [8, 4, 10, 3, 5, 1]