def myfunc(intList,k):
if len(intList)==1:
return str(intList[0])
else:
if intList[1]-intList[0]==1:
if k==0:
k=+1
return str(intList[0])+'-'+str(myfunc(intList[1:],k))
if k==1:
return str(myfunc(intList[1:],k))
else:
k=0
return str(intList[0])+','+str(myfunc(intList[1:],k))
print(myfunc([1,2,3,4,7,9,11,13,14,15,16,17,19],0))
print(myfunc([5,6,15,16],0))
1-4,7,9,11,13-17,19
5-6,15-16
def fl(l):
if len(l) > 2:
return '%s-%s' % (l[0], l[-1])
elif len(l) == 2:
return '%s, %s' % (l[0], l[-1])
else:
return '%s' % l[0]
def cc(l):
r = []
b = []
for i in l:
if len(b) > 0:
if i - b[-1] == 1:
b.append(i)
else:
r.append(fl(b))
b = [i]
else:
b.append(i)
else:
r.append(fl(b))
return ', '.join(r)
def getstr(l):
nl = [l[0],]
for i in range(1, len(l)):
if l[i] - nl[-1] > 1 and nl[-2] != '-':
nl.append('-')
nl.append(l[i])
return nl
l = [1,2,3,4,7,9,11,13,14,15,16,17,19]
nl = getstr(l)
print(','.join(nl).replace(',-,', '-'))
def closeInt(rS, rE):
if rE - rS == 1:
return ", " + str(rE)
if rE - rS > 1:
return "-" + str(rE)
return ""
def shortify(intlist):
result = str(intlist[0])
rangeStart = intlist[0]
rangeEnd = intlist[0]
for i in range(1, len(intlist)):
if intlist[i] == intlist[i-1] + 1:
rangeEnd += 1
else:
result += closeInt(rangeStart, rangeEnd)
result += ", " + str(intlist[i])
rangeStart = intlist[i]
rangeEnd = intlist[i]
result += closeInt(rangeStart, intlist[-1])
return result
print(shortify([1]))
print(shortify([2,3]))
print(shortify([2,4]))
print(shortify([4,5,6]))
print(shortify([1,2,3,4,7,9,11,13,14,15,16,17,19]))
print(shortify([1,2,4,7,8,11,12,13,14,15,16,18,19]))
print(shortify([1,2,3,4,7,9,11,13,14,15,17,18,19]))
from itertools import groupby
def drop_inner(nums):
for a, b, c in zip([0, 0] + nums, [0] + nums + [0], nums + [0, 0]):
if b:
yield 0 if a and c and (a + 1 == b == c - 1) else b
def shorten(nums):
return ','.join(
str(x) if x else '-' for x, _ in groupby(drop_inner(nums))
).replace(',-,', '-')