-
Notifications
You must be signed in to change notification settings - Fork 273
Reverse Letters
codepath-wiki-review[bot] edited this page Jun 2, 2026
·
6 revisions
TIP101 Unit 3 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What will the function output?
- It should return a string with only the letters reversed, keeping non-letter characters in their original positions.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Make a list of all letters in the string, then build a new string that reverses only the letters.
1) Create an empty list for the letters
2) Loop through the string, and add any letters to the list
3) Make an index variable, set to end of letters list
4) Create an empty results string
5) Loop through each character in the original string
a) If the char was a letter:
i) add the value at letters[index] to results
ii) reduce index by 1
b) Otherwise, add the char itself to results
6) Return resultsdef reverse_only_letters(s):
letters = [] # Collect letters
for c in s:
if c.isalpha():
letters.append(c)
result = ""
letters_index = len(letters) - 1 # Start from the end of the letters list
for c in s:
if c.isalpha():
result += letters[letters_index]
letters_index -= 1
else:
result += c
return result