string exercise minor improvements

This commit is contained in:
anebz 2019-04-04 14:49:46 +02:00
parent 4520a81ce3
commit 4725533380
1 changed files with 8 additions and 10 deletions

View File

@ -6,25 +6,23 @@ import unittest
def reduce_string(s):
slen = len(s)
if slen < 2:
return "Empty String" if len(s) == 0 else s
return s if s else "Empty String"
i = 0
count = 0
count = False
while slen > 1 and i < slen - 1:
if s[i] == s[i+1]:
count += 1
count = True
if count % 2 == 1:
s = s.replace(s[i] * (count+1), '')
i -= count + 1
if i < -1:
i = -1
if count:
s = s.replace(s[i] * 2, '')
i = max(i-2, -1)
slen = len(s)
count = 0
count = False
i += 1
return "Empty String" if len(s) == 0 else s
return s if s else "Empty String"
class Test(unittest.TestCase):