美文网首页
002. Codewars 之 Unique In Order

002. Codewars 之 Unique In Order

作者: 伟健Wiken | 来源:发表于2017-03-01 12:23 被阅读0次

    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):
        pass
    

    Solution:

    def unique_in_order(iterable):
        pre_item = None
        result = []
        for item in iterable:
            if item != pre_item:
                result.append(item)
                pre_item = item
        return result
    

    相关文章

      网友评论

          本文标题:002. Codewars 之 Unique In Order

          本文链接:https://www.haomeiwen.com/subject/wmvqgttx.html