Skip to content

Instantly share code, notes, and snippets.

@kkt-ee
Created January 4, 2020 17:04
Show Gist options
  • Save kkt-ee/5432802b76332d9e19716c691a613361 to your computer and use it in GitHub Desktop.
Save kkt-ee/5432802b76332d9e19716c691a613361 to your computer and use it in GitHub Desktop.
"""
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
"""
def unique_in_order(iterable):
ll = []
for x in iterable:
if len(ll) == 0:
ll.append(x)
if ll[-1] != x:
ll.append(x)
return ll
iterable='AAAABBBCCDAABBB'#), ['A','B','C','D','A','B'])
print(unique_in_order(iterable))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment