-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce_string.py
More file actions
29 lines (22 loc) · 988 Bytes
/
reduce_string.py
File metadata and controls
29 lines (22 loc) · 988 Bytes
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
def reduce_string(string):
"""Whenever an 'e' shows up consecutively in a string, reduce it to just once instance of the letter.
>>> reduce_string('askfheeeelkjeeeemahee')
'askfhelkjemahe'
"""
# when checking e, need to also peek at next item
# if that item is also an e, we need to get rid of it
# problem becomes trying to index out
# do i need to solve this recursively?
valid_string = ''
for i in range(len(string)):
# in the case that the letter IS an e, we check that the letter before is not an e before appending
if string[i] != "e" or (i > 0 and string[i-1]) != "e":
valid_string += string[i]
# else:
# # in the case that the letter IS an e, we check that the letter before is not an e before appending
# if string[i-1] != "e":
# valid_string.append(string[i])
return valid_string
if __name__ == "__main__":
import doctest
doctest.testmod()