-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrings.py
More file actions
208 lines (171 loc) · 7.21 KB
/
Copy pathstrings.py
File metadata and controls
208 lines (171 loc) · 7.21 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
"""
String Exercises - Complete the functions below
Run this file with: python topic.py
Or work interactively with: python3 -i topic.py
"""
# Exercise 1: Basic String Operations
def get_length(text):
"""Return the length of a string."""
# Your code here
pass
def to_upper_case(text):
"""Convert a string to uppercase."""
# Your code here
pass
def to_lower_case(text):
"""Convert a string to lowercase."""
# Your code here
pass
# Exercise 2: String Analysis
def count_vowels(text):
"""Count vowels (a, e, i, o, u) in a string (case insensitive)."""
# Your code here
pass
def count_words(text):
"""Count words in a string (separated by spaces)."""
# Your code here
pass
def is_palindrome(text):
"""Check if a string reads the same forwards and backwards (ignore case and spaces)."""
# Your code here
pass
# Exercise 3: String Searching
def find_substring(text, substring):
"""Return the index of the first occurrence of substring, -1 if not found."""
# Your code here
pass
def count_occurrences(text, substring):
"""Count how many times substring appears in text."""
# Your code here
pass
def starts_with_prefix(text, prefix):
"""Check if text starts with prefix."""
# Your code here
pass
# Exercise 4: String Modification
def remove_spaces(text):
"""Remove all spaces from a string."""
# Your code here
pass
def replace_char(text, old_char, new_char):
"""Replace all occurrences of old_char with new_char."""
# Your code here
pass
def reverse_string(text):
"""Return the string reversed."""
# Your code here
pass
# Exercise 5: String Formatting
def format_name(first, last):
"""Create name in 'Last, First' format."""
# Your code here
pass
def create_email(username, domain):
"""Create an email address: username@domain."""
# Your code here
pass
def format_phone(area_code, exchange, number):
"""Format phone as '(area_code) exchange-number'."""
# Your code here
pass
# Exercise 6: String Validation
def is_valid_email(email):
"""Check basic email format (contains @ and .)."""
# Your code here
pass
def is_all_digits(text):
"""Check if string contains only digits."""
# Your code here
pass
def is_strong_password(password):
"""Check if password has uppercase, lowercase, and digits."""
# Your code here
pass
# Exercise 7: String Parsing
def extract_numbers(text):
"""Extract all numbers from a string and return them as a list of integers."""
# Your code here
pass
def split_csv_line(line):
"""Split a CSV line into a list of values."""
# Your code here
pass
def parse_name(full_name):
"""Split 'First Last' into a tuple (first, last)."""
# Your code here
pass
# Exercise 8: String Lists
def join_with_comma(string_list):
"""Join strings with commas."""
# Your code here
pass
def longest_string(string_list):
"""Find the longest string in a list."""
# Your code here
pass
def filter_by_length(string_list, min_length):
"""Filter strings by minimum length."""
# Your code here
pass
# Exercise 9: Advanced String Processing
def title_case(text):
"""Convert to title case (capitalize each word)."""
# Your code here
pass
def remove_duplicates(text):
"""Remove duplicate characters while preserving order."""
# Your code here
pass
def compress_string(text):
"""Compress 'aabbbcccc' to 'a2b3c4'."""
# Your code here
pass
def run_tests():
"""Test all the functions to verify they work correctly."""
print("Testing Basic String Operations:")
print(f"Length of 'hello': {get_length('hello')}") # Should print 5
print(f"'hello' uppercase: {to_upper_case('hello')}") # Should print HELLO
print(f"'WORLD' lowercase: {to_lower_case('WORLD')}") # Should print world
print("\nTesting String Analysis:")
print(f"Vowels in 'hello world': {count_vowels('hello world')}") # Should print 3
print(f"Words in 'hello world': {count_words('hello world')}") # Should print 2
print(f"Is 'racecar' palindrome: {is_palindrome('racecar')}") # Should print True
print(f"Is 'hello' palindrome: {is_palindrome('hello')}") # Should print False
print("\nTesting String Searching:")
print(f"'ell' in 'hello': {find_substring('hello', 'ell')}") # Should print 1
print(f"'xyz' in 'hello': {find_substring('hello', 'xyz')}") # Should print -1
print(f"Count 'll' in 'hello': {count_occurrences('hello', 'll')}") # Should print 1
print(f"'hello' starts with 'he': {starts_with_prefix('hello', 'he')}") # Should print True
print("\nTesting String Modification:")
print(f"Remove spaces from 'he llo wo rld': {remove_spaces('he llo wo rld')}") # Should print helloworld
print(f"Replace 'l' with 'x' in 'hello': {replace_char('hello', 'l', 'x')}") # Should print hexxo
print(f"Reverse 'hello': {reverse_string('hello')}") # Should print olleh
print("\nTesting String Formatting:")
print(f"Format name: {format_name('John', 'Doe')}") # Should print Doe, John
print(f"Create email: {create_email('john', 'example.com')}") # Should print john@example.com
print(f"Format phone: {format_phone('555', '123', '4567')}") # Should print (555) 123-4567
print("\nTesting String Validation:")
print(f"Valid email 'test@email.com': {is_valid_email('test@email.com')}") # Should print True
print(f"Valid email 'invalid': {is_valid_email('invalid')}") # Should print False
print(f"All digits '12345': {is_all_digits('12345')}") # Should print True
print(f"All digits 'abc123': {is_all_digits('abc123')}") # Should print False
print(f"Strong password 'Pass123': {is_strong_password('Pass123')}") # Should print True
print("\nTesting String Parsing:")
print(f"Extract numbers: {extract_numbers('I have 5 cats and 3 dogs')}") # Should print [5, 3]
print(f"Split CSV: {split_csv_line('apple,banana,cherry')}") # Should print ['apple', 'banana', 'cherry']
print(f"Parse name: {parse_name('John Doe')}") # Should print ('John', 'Doe')
print("\nTesting String Lists:")
print(f"Join with comma: {join_with_comma(['apple', 'banana'])}") # Should print apple,banana
print(f"Longest string: {longest_string(['hi', 'hello', 'hey'])}") # Should print hello
print(f"Filter by length 4: {filter_by_length(['hi', 'hello', 'hey'], 4)}") # Should print ['hello']
print("\nTesting Advanced Processing:")
print(f"Title case: {title_case('hello world')}") # Should print Hello World
print(f"Remove duplicates: {remove_duplicates('aabbcc')}") # Should print abc
print(f"Compress string: {compress_string('aabbbcccc')}") # Should print a2b3c4
if __name__ == "__main__":
print("Python String Exercises")
print("=" * 40)
print("Complete the functions above, then run this file to test them.")
print("Uncomment the line below when you're ready to test:")
print()
# run_tests() # Uncomment this line to run tests