-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce_string.py
More file actions
37 lines (32 loc) · 894 Bytes
/
reduce_string.py
File metadata and controls
37 lines (32 loc) · 894 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
30
31
32
33
34
35
36
37
import fileinput
def reduce(text):
reduced = ""
prev = text[0]
count = 1
same = False
for i in range(1, len(text)):
if i == len(text) - 1:
if prev == text[i]:
count += 1
if count % 2 != 0:
reduced += text[i]
elif same == True:
if count % 2 != 0:
reduced += prev
reduced += text[i]
if text[i] != prev:
if same == True:
if count % 2 != 0:
reduced += prev
same = False
count = 1
else:
reduced += prev
elif text[i] == prev:
count += 1
same = True
prev = text[i]
return "Empty String" if len(reduced) == 0 else reduced
for line in fileinput.input():
pass
print(reduce(line))