Increasing + Decreasing in One Function
Print numbers increasing then decreasing recursively.
AdvancedPhase 3 - Level 3: Pattern RecursionExample 8 of 10
increasing-decreasing-in-one-function.py
1def print_inc_dec(n, current=1):2 # Base case3 if current > n:4 return56 # Print increasing7 print(current, end=" ")8 # Recurse9 print_inc_dec(n, current + 1)10 # Print decreasing (after recursion)11 if current < n:12 print(current, end=" ")1314# Test15n = int(input("Enter n: "))16print_inc_dec(n)17print()
Output
Enter n: 5 1 2 3 4 5 4 3 2 1
What's going on
Print before recursion (increasing), after (decreasing).
Key Concepts:
Print before recursion: increasing order
Print after recursion: decreasing order
Creates symmetric pattern
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum