Uppercase Conversion Recursively
Convert text to uppercase through recursive calls. Students transform one indexed character at a time and concatenate returned text without modifying the string.
1def to_upper(s, index=0):2 # Base case3 if index >= len(s):4 return ""56 # Convert current character7 char = s[index].upper()8 # Recurse9 return char + to_upper(s, index + 1)1011# Test12text = input("Enter a string: ")13result = to_upper(text)14print(f"Uppercase: {result}")
Output
Enter a string: Hello Uppercase: HELLO
In this one
- Transform one character at a time
- Return an empty tail
- Understand Unicode expansion
How it works
- 1
Stop at the length boundary with empty text.
- 2
Uppercase the current character.
- 3
Concatenate it with the recursively converted remainder.
Edge cases
What does empty input produce?
What does empty input produce?
An empty result.
Must output length equal input length?
Must output length equal input length?
No. Some Unicode uppercase mappings expand characters.
What's going on
At each position, upper produces that character's uppercase text. The function joins it to the processed result from the next index.
The base case returns an empty tail. Digits and punctuation generally remain unchanged. Unicode case mapping can expand a character into several characters, so output length need not equal input length. Calls and concatenations still require extra memory.
What does empty input produce? An empty result.
Must output length equal input length? No. Some Unicode uppercase mappings expand characters.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What advances the recursion?
Pick an answer to check it.
Question 2
What does empty input produce?
Pick an answer to check it.
Questions people ask
What does empty input produce?
An empty result.
Must output length equal input length?
No. Some Unicode uppercase mappings expand characters.
What advances the recursion?
index + 1.
๐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