-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03-reverse-string.py
More file actions
36 lines (30 loc) · 1.11 KB
/
03-reverse-string.py
File metadata and controls
36 lines (30 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Challenge: Reverse String
Difficulty: Beginner
Concepts: strings, loops, iteration, string building
Time: 15 minutes
Reverse a string without using slice notation ([::-1]).
You may use loops, recursion, or any other approach.
Examples:
>>> reverse_string("hello")
'olleh'
>>> reverse_string("Python")
'nohtyP'
"""
def reverse_string(s: str) -> str:
"""Reverse a string without using [::-1]. Implement this function."""
# Hint: Build a new string by iterating from the end, or use a loop to prepend characters.
pass
# --- Tests (do not modify) ---
if __name__ == "__main__":
# Test 1: Basic word
assert reverse_string("hello") == "olleh", "Basic word failed"
# Test 2: Palindrome
assert reverse_string("racecar") == "racecar", "Palindrome failed"
# Test 3: Empty string
assert reverse_string("") == "", "Empty string failed"
# Test 4: Single character
assert reverse_string("x") == "x", "Single character failed"
# Test 5: Spaces and punctuation
assert reverse_string("hi there!") == "!ereht ih", "Spaces and punctuation failed"
print("All tests passed!")