-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.py
More file actions
278 lines (248 loc) · 6.07 KB
/
Copy pathstrings.py
File metadata and controls
278 lines (248 loc) · 6.07 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Imperative programming on strings
def count(c: str, s: str) -> int:
""" Counts the number if occurences of the character c in s.
>>> count('a', 'banana')
3
"""
count = 0
i = 0
while i < len(s):
if s[i] == c:
count = count + 1
i = i + 1
return count
def member(c: str, s: str) -> bool:
""" Checks wether c appears in s.
>>> member('a', 'man')
True
>>> member('a', 'hut')
False
"""
member = False
i = 0
while i < len(s) and not member:
if s[i] == c:
member = True
i = i + 1
return member
def is_prefix(s1: str, s2: str) -> bool:
""" Checks wether s1 is a prefix if s2.
>>> is_prefix('ba', 'banana')
True
>>> is_prefix('bn', 'banana')
False
"""
if len(s1) > len(s2):
return False
else:
prefix = True
i = 0
while i < len(s1) and prefix:
if s1[i] != s2[i]:
prefix = False
i = i + 1
return prefix
def is_suffix(s1: str, s2: str) -> bool:
""" Checks wether s1 is a suffix of s2.
>>> is_suffix('cave', 'mancave')
True
>>> is_suffix('man', 'mancave')
False
"""
if len(s1) > len(s2):
return False
else:
i = -1
suffix = True
while i > -(len(s1) + 1) and suffix:
if s1[i] != s2[i]:
suffix = False
i = i - 1
return suffix
def contains(s1: str, s2: str) -> bool:
""" Checks if s2 can be obtained from s1 by deleting some characters from s1.
>>> contains('mann', 'man')
True
>>> contains('menn', 'man')
False
"""
i = 0
j = 0
while i < len(s2) and j < len(s1):
if s2[i] == s1[j]:
i = i + 1
j = j + 1
return i == len(s2)
def to_uppercase(s: str) -> str:
""" Converts s to uppercase.
>>> to_uppercase('hey')
'HEY'
"""
upper = ''
i = 0
while i < len(s):
if 97 <= ord(s[i]) <= 122:
upper = upper + chr(ord(s[i]) - 32)
else:
upper = upper + s[i]
i = i + 1
return upper
def to_lowercase(s: str) -> str:
""" Coverts s to lowercase.
>>> to_lowercase('HEY')
'hey'
"""
lower = ''
i = 0
while i < len(s):
if 65 <= ord(s[i]) <= 90:
lower = lower + chr(ord(s[i]) + 32)
else:
lower = lower + s[i]
i = i + 1
return lower
def toCamelCase(s: str) -> str:
""" Converts s to camel notation.
>>> toCamelCase('hey you')
'heyYou'
"""
camel_case = ''
i = 0
while i < len(s):
if s[i] == ' ':
if 97 <= ord(s[i+1]) <= 122:
camel_case = camel_case + chr(ord(s[i+1]) - 32)
else:
camel_case = camel_case + s[i + 1]
i = i + 2
else:
camel_case = camel_case + s[i]
i = i + 1
return camel_case
def first_position(c: str, s: str) -> int:
""" Returns the index of the first occurrence of c in s or -1 if c is not in s.
>>> first_position('q', 'man')
-1
>>> first_position('a','banana')
1
"""
found = False
i = 0
while i < len(s) and not found:
if s[i] == c:
found = True
i = i + 1
if i >= len(s):
return -1
else:
return i - 1
def last_position(c: str, s: str) -> int:
""" Returns the index of the last occurrence of c in s or -1 if c does not appear in s.
>>> last_position('q', 'man')
-1
>>> last_position('a', 'banana')
5
"""
found = False
i = len(s) - 1
while i >= 0 and not found:
if s[i] == c:
found = True
i = i - 1
if i < 0:
return -1
else:
return i + 1
def positions(c: str, s: str) -> list[int]:
""" Returns a list with the indices of the occurrences of c in s.
>>> positions('a', 'banana')
[1, 3, 5]
"""
positions = []
i = 0
while i < len(s):
if s[i] == c:
positions.append(i)
i = i + 1
return positions
def is_permutation(s1: str, s2: str) -> bool:
""" Checks if s1 and s2 contains excatly the same characters counting repetitions.
>>> is_permutation('abadcc', 'bcadac')
True
>>> is_permutation('abc', 'abcc')
False
"""
permutation = True
i = 0
if len(s1) != len(s2):
return False
else:
while i < len(s1) and permutation:
if count(s1[i], s1) != count(s1[i], s2):
permutation = False
i = i + 1
return permutation
def reverse(s: str) -> str:
""" Reverses s.
>>> reverse('abc')
'cba'
"""
reverse = ''
i = len(s) - 1
while i >= 0:
reverse = reverse + s[i]
i = i - 1
return reverse
def reverse_words(s: str) -> str:
""" Reverses the words in s while preserving their order.
>>> reverse_words('hey you')
'yeh uoy'
"""
words = []
word = ''
reverse_words = ''
i = 0
while i < len(s):
if s[i] == ' ':
words.append(word)
word = ''
else:
word = word + s[i]
i = i + 1
words.append(word)
j = 0
while j < len(words):
reverse_words = reverse_words + reverse(words[j]) + ' '
j = j + 1
return reverse_words[:-1]
def remove_vowels(s: str) -> str:
""" Removes the vowels in s.
>>> remove_vowels('abce')
'bc'
"""
not_vowels = ''
i = 0
while i < len(s):
if not member(s[i], 'aAeEiIoOuUyY'):
not_vowels = not_vowels + s[i]
i = i + 1
return not_vowels
def respace(s: str, n: int) -> str:
""" Removes all spaces from s and the adds a space after every n characters.
>>> respace('abcdefg', 3)
'abc def g'
"""
words = ''
i = 0
while i < len(s):
if s[i] != ' ':
words = words + s[i]
i = i + 1
respace = ''
j = 0
while j < len(words):
respace = respace + words[j]
if (j + 1) % n == 0:
respace = respace + ' '
j = j + 1
return respace