-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_walker.py
More file actions
209 lines (168 loc) · 6.64 KB
/
string_walker.py
File metadata and controls
209 lines (168 loc) · 6.64 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class StringWalker:
"""
A class that allows walking through a string forward and backward,
getting characters at positions, and replacing characters.
"""
def __init__(self, text):
"""
Initialize the StringWalker with a string.
Args:
text (str): The string to walk through
"""
self.text = text
self.position = 0
self.length = len(text)
def get_current_position(self):
"""Return the current position in the string."""
return self.position
def get_current_character(self):
"""Return the character at the current position."""
if 0 <= self.position < self.length:
return self.text[self.position]
return None
def get_character_at(self, position):
"""
Return the character at a specific position.
Args:
position (int): The position to get the character from
Returns:
str or None: The character at the position, or None if out of bounds
"""
if 0 <= position < self.length:
return self.text[position]
return None
def walk_forward(self, steps=1):
"""
Walk forward through the string by the specified number of steps.
Args:
steps (int): Number of steps to move forward (default: 1)
Returns:
bool: True if the move was successful, False if out of bounds
"""
new_position = self.position + steps
if 0 <= new_position < self.length:
self.position = new_position
return True
return False
def walk_backward(self, steps=1):
"""
Walk backward through the string by the specified number of steps.
Args:
steps (int): Number of steps to move backward (default: 1)
Returns:
bool: True if the move was successful, False if out of bounds
"""
new_position = self.position - steps
if 0 <= new_position < self.length:
self.position = new_position
return True
return False
def go_to_position(self, position):
"""
Move to a specific position in the string.
Args:
position (int): The position to move to
Returns:
bool: True if the move was successful, False if out of bounds
"""
if 0 <= position < self.length:
self.position = position
return True
return False
def go_to_start(self):
"""Move to the beginning of the string."""
self.position = 0
def go_to_end(self):
"""Move to the end of the string."""
self.position = self.length - 1
def replace_character_at(self, position, new_char):
"""
Replace a character at a specific position.
Args:
position (int): The position to replace the character at
new_char (str): The new character to place at the position
Returns:
bool: True if the replacement was successful, False if out of bounds
"""
if 0 <= position < self.length and len(new_char) == 1:
# Convert string to list for modification
text_list = list(self.text)
text_list[position] = new_char
self.text = ''.join(text_list)
return True
return False
def replace_current_character(self, new_char):
"""
Replace the character at the current position.
Args:
new_char (str): The new character to place at the current position
Returns:
bool: True if the replacement was successful, False if out of bounds
"""
return self.replace_character_at(self.position, new_char)
def get_string(self):
"""Return the current string."""
return self.text
def get_length(self):
"""Return the length of the string."""
return self.length
def is_at_start(self):
"""Check if the walker is at the beginning of the string."""
return self.position == 0
def is_at_end(self):
"""Check if the walker is at the end of the string."""
return self.position == self.length - 1
def __str__(self):
"""String representation showing current position and character."""
current_char = self.get_current_character()
return f"StringWalker at position {self.position} (char: '{current_char}') of '{self.text}'"
# Example usage and demonstration
if __name__ == "__main__":
# Create a StringWalker with a sample string
walker = StringWalker("Hello, World!")
print("=== StringWalker Demo ===")
print(f"Initial string: '{walker.get_string()}'")
print(f"Length: {walker.get_length()}")
print(f"Current position: {walker.get_current_position()}")
print(f"Current character: '{walker.get_current_character()}'")
print()
# Walk forward
print("Walking forward 3 steps...")
walker.walk_forward(3)
print(f"Position: {walker.get_current_position()}, Character: '{walker.get_current_character()}'")
print()
# Walk backward
print("Walking backward 1 step...")
walker.walk_backward(1)
print(f"Position: {walker.get_current_position()}, Character: '{walker.get_current_character()}'")
print()
# Get character at specific position
print("Getting character at position 7...")
char_at_7 = walker.get_character_at(7)
print(f"Character at position 7: '{char_at_7}'")
print()
# Replace character
print("Replacing character at position 7 with 'X'...")
walker.replace_character_at(7, 'X')
print(f"Updated string: '{walker.get_string()}'")
print()
# Go to specific position
print("Going to position 10...")
walker.go_to_position(10)
print(f"Position: {walker.get_current_position()}, Character: '{walker.get_current_character()}'")
print()
# Replace current character
print("Replacing current character with 'Y'...")
walker.replace_current_character('Y')
print(f"Updated string: '{walker.get_string()}'")
print()
# Go to start and end
print("Going to start...")
walker.go_to_start()
print(f"Position: {walker.get_current_position()}, Character: '{walker.get_current_character()}'")
print("Going to end...")
walker.go_to_end()
print(f"Position: {walker.get_current_position()}, Character: '{walker.get_current_character()}'")
print()
print("=== Final State ===")
print(walker)