美文网首页
CodeWars打卡(05)

CodeWars打卡(05)

作者: 影醉阏轩窗 | 来源:发表于2018-07-01 01:57 被阅读0次

Details:

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_order1(iterable):
    if len(list(iterable))==0:
        return []
    return [list(iterable)[0]] + [value for key,value in enumerate(list(iterable)) if (key>0 and (iterable[key-1] != value))]

第一名代码:

def is_isogram(string):
    return len(string) == len(set(string.lower()))

第四名代码:

unique_in_order = lambda l: [z for i, z in enumerate(l) if i == 0 or l[i - 1] != z]
  • 总结:这道题难度系数六级,自己写的代码比第一名好多了,但是和第四名的代码差一点,和我的思路一样,但是他用了一个or和lambda把我的问题简化了很多.
  • 天外有天,人外有人,刚开始我还以为我肯定能排进前多少名~~

相关文章

网友评论

      本文标题:CodeWars打卡(05)

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