Reverse Array
Reverse the order of array elements.
IntermediatePhase 4 - Level 3: TransformationExample 6 of 10
reverse-array.py
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Reverse array8print(f"Original: {arr}")9for i in range(len(arr) // 2):10 arr[i], arr[len(arr) - 1 - i] = arr[len(arr) - 1 - i], arr[i]1112print(f"Reversed: {arr}")
Output
Enter array size: 5 Element 1: 1 Element 2: 2 Element 3: 3 Element 4: 4 Element 5: 5 Original: [1, 2, 3, 4, 5] Reversed: [5, 4, 3, 2, 1]
What's going on
Swap elements from both ends.
Key Concepts:
Swap arr[i] with arr[len-1-i]
Loop only to middle
In-place reversal
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum