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):
is_in = ''
res = []
for char in iterable:
if char != is_in:
is_in = char
res.append(char)
return res
def unique_in_order(iterable):
r = []
for x in iterable:
x in r[-1:] or r.append(x)
return r
print(unique_in_order('AAAABBBCCDAABBB'))
Training on Unique In Order | Codewars
No comments :
Post a Comment