Skip to content

Latest commit

 

History

History
45 lines (28 loc) · 866 Bytes

Unique_In_Order.md

File metadata and controls

45 lines (28 loc) · 866 Bytes

CodeWars Python Solutions


Unique In Order

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]

Given Code

def unique_in_order(iterable):
    pass

Solution

def unique_in_order(iterable):
    chars = []
    for i in range(len(iterable)):
        if i == 0 or iterable[i] != iterable[i-1]:
            chars.append(iterable[i])
    return chars

See on CodeWars.com