I have input of a string containing a single number (like: \$3\$) or a range (like: \$1-5\$). Sample input, all together, looks like: "1-5,3,15-16"
, and sample output for that input looks like "1,2,3,4,5,15"
. Output doesn't need to be sorted.
I built something to parse this, but it's ugly. How can I improve this?
from itertools import chain
def giveRange(numString:str):
z=numString.split("-")
if(len(z)==1):
return [int(z[0])]
elif(len(z)==2):
return list(range(int(z[0]),int(z[1])+1))
else:
raise IndexError("TOO MANY VALS!")
def unpackNums(numString:str):
rList=[]
rList.extend(set(chain(*map(giveRange,numString.split(",")))))
return rList
unpackNums("1-2,30-50,1-10")