002. Codewars 之 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]
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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,457评论 0 23
  • 永远别低估,减肥能给自己容貌上带来多大的改变,鼻梁变高根本不是什么稀奇的事,被肉坠的下垂的嘴角减完肥后会上扬,双眼...
    向日葵孔小姐阅读 4,826评论 9 10
  • 晨起,鞋子踢踏着台阶,蘸着暴雨后的积水,划拉上班的节奏。 一阵清凉的空气自面门起,梳过头发的缝隙把他们拢在脑后。原...
    妖精也会说人话阅读 2,047评论 1 2
  • 在这里,Alice对于安东尼就相当于他自己,他性格比较内向,朋友比较少,知心的就小樱一个。他在寻找Alice的途中...
    Lee0117阅读 2,823评论 0 0
  • 听说kafka是个神器。好想学! kafka是啥? 分布式消息队列 排队用的。 在分布式系统中,消息队列(mess...
    老吴学技术阅读 5,385评论 3 6