Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Last active June 12, 2020 21:52
Show Gist options
  • Save pyrofolium/44c4bf95a6cc08c8bde78cf659226c1a to your computer and use it in GitHub Desktop.
Save pyrofolium/44c4bf95a6cc08c8bde78cf659226c1a to your computer and use it in GitHub Desktop.
# string processing
# remove sequential duplicate characters from a string
# "aaabbccccccfffdde" -> "abcfde"
#"abcdefghijklmnop"
# b = num of boundaries
# n * log(n)
def remove_repeated(x: str) -> str:
if len(x) <= 1:
return x
else:
if x[0] == x[1]:
return remove_repeated(x[0] + x[2:])
else:
return x[0] + remove_repeated(x[1:])
def remove_repeated2(x: str) -> str:
acc = []
for char in x:
if len(acc) == 0 or acc[-1] != char:
acc.append(char)
return "".join(acc)
# def addNumbers(a,b):
# sum = a + b
# return sum
# num1 = int(input())
# num2 = int(input())
# print("The sum is", addNumbers(num1, num2))
print(remove_repeated("aaabbccccccafffdde"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment