【Codewars python 4kyu】: Range Extraction

问题描述:

A format for expressing an ordered list of integers is to use a comma separated list of either

  • individual integers
  • or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example ("12, 13, 15-17")

Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.

Example:

solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20])
# returns "-6,-3-1,3-5,7-11,14,15,17-20"

Courtesy of rosettacode.org

代码实现:

#codewars第十九题
def solution(args):
    my_list = []
    my_index = 0
    i = 0
    _length = len(args)
    if _length < 3: return args
    while i < _length:
        now_index = i
        j = i + 1
        while j <= _length:  # =_length去解决最后超出长度导致跳出循环  最后几个字符无法解决的情况
            if j != _length and args[j] == args[j-1] + 1:  # j != _length and 防止超出索引寻址
                j += 1
            else:
                if j >= (now_index + 3):
                    my_list.append(str(args[now_index])+'-'+str(args[j-1])) #追加到末尾
                    my_index += 1
                    i = j - 1
                    
                    break
                else:
                    my_list[my_index:] = args[i:j]  #切片赋值
                    my_index += (j - i)  #存储list的下一个存储下标
                    i = j - 1
                    break
        i += 1
    return ','.join(map(str,my_list))  #将list转换为字符串
solution([-6,-3,-2,-1,0,1,3,4,5,7,8,9,10,11,14,15,17,18,19,20])
#solution([-3,-2,-1,2,10,15,16,18,19,20])

#另解一
def solution(args):
    result = []
    tmp = []
    for i in args+[""]:
        if not tmp:
            tmp.append(i)
        elif i==tmp[-1]+1:
            tmp.append(i)
        else:
            if len(tmp)>=3:
                t = '{}-{}'.format(tmp[0],tmp[-1])
                result.append(t)
            else:
                result.extend(tmp)            
            tmp = []
            tmp.append(i)
    #print(','.join(map(str,result)))
    return ','.join(map(str,result))

#另解二
def solution(args):
    out = []
    beg = end = args[0]
    
    for n in args[1:] + [""]:        
        if n != end + 1:
            if end == beg:
                out.append( str(beg) )
            elif end == beg + 1:
                out.extend( [str(beg), str(end)] )
            else:
                out.append( str(beg) + "-" + str(end) )
            beg = n
        end = n
    
    return ",".join(out)
solution([-6,-3,-2,-1,0,1,3,4,5,7,8,9,10,11,14,15,17,18,19,20])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值