Skip to content

Instantly share code, notes, and snippets.

@arcolife
Created February 17, 2017 11:30
Show Gist options
  • Select an option

  • Save arcolife/90a33f8c01ea87355ed12f6e635a9262 to your computer and use it in GitHub Desktop.

Select an option

Save arcolife/90a33f8c01ea87355ed12f6e635a9262 to your computer and use it in GitHub Desktop.
#!/bin/env python3
# aaabbcd -> abcd
# https://groups.google.com/forum/#!forum/dsa_sg
x = list('aaabbcd')
tmp = ''
for i in range(0,len(x),2):
# import pdb; pdb.set_trace()
if i+1 < len(x):
if x[i]==x[i+1]:
if x[i] not in tmp:
tmp+=x[i]
else:
if x[i] not in tmp:
tmp+=x[i]
if x[i+1] not in tmp:
tmp+=x[i+1]
else:
if x[i] not in tmp:
tmp+=x[i]
print(tmp)
@arcolife
Copy link
Author

arcolife commented Feb 17, 2017

#!/bin/env python3

# aaabbcd -> abcd
# https://groups.google.com/forum/#!forum/dsa_sg

def eliminate(x):
    x = list(x)
    tmp = ''
    for i in range(0,len(x),2):
        if (tmp and tmp[-1]!=x[i]) or not tmp:
            tmp+=x[i]        
        if i+1 < len(x):
            if x[i]!=x[i+1]:
                tmp+=x[i+1]
    return tmp

assert eliminate('aaabbcd') == 'abcd'
assert eliminate('aaabbacd') == 'abacd'
assert eliminate('aaabbaacd') == 'abacd'
assert eliminate('aaaaaa') == 'a'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment