# Return each number from a group of numbers

## The challenge

The program must return all numbers included into a group (comma and hyphen separated sequence) of numbers.

## Rules

• s is the sequence string;
• all numbers included in s are positive;
• numbers will always increase;
• numbers will never repeat
• when you answer, show the output for s="1,3-5,9,16,18-23"

## Examples

input(s)    outputs
-----------------
1           1
1,2         1,2
1-4         1,2,3,4
1-4,6       1,2,3,4,6
1-4,8-11    1,2,3,4,8,9,10,11


Good luck. =)

• Will we ever have input sequences that are not constantly increasing, for example: 4-9,1-2 or 1-3,9-6? – Matt Oct 3 '12 at 17:16
• Or overlapping? Does the output have to be sorted and not contain duplicates? – Peter Taylor Oct 3 '12 at 17:24
• @Gareth Yes, this is a code-golf, then please vote-up for the shortest answer. Matt and Peter, I edited the question, please check it. Thanks! – BernaMariano Oct 3 '12 at 17:33
• Does it have to be a full program, and is there a restriction on the format of the output? – Brad Gilbert b2gills Nov 28 '15 at 19:21
• duplicate? – ev3commander Nov 28 '15 at 21:53

## GolfScript (24 chars)

','/{~.,!{~)),>~}*}%','*


E.g.

# R, 44 bytes

-=seq;eval(parse(t=c("c(",scan(,""),")")))


Try it online!

Redefine - to mean seq (i.e. :), surround the input with c() and evaluate the corresponding expression.

# K, 47

","/:,/${x+!1+y-x}.'2#'a,'a:"I"$'"-"\:'","\:0:0


Test case

k)","/:,/${x+!1+y-x}.'2#'a,'a:"I"$'"-"\:'","\:0:0
1,3-5,9,16,18-23
"1,3,4,5,9,16,18,19,20,21,22,23"

• ","/:$,/{{x+!1+y-x}. 2#"J"$"-"\:x}'","\:0:0 for 43 bytes – streetster Jul 19 '19 at 21:45

# Jelly, 9 bytes

⁾-ryṣ”,VF


Try it online!

   y         Replace
⁾-r          hyphens with the letter r,
ṣ”,      split on commas,
V     evaluate every element,
F    and flatten.


The range dyad r takes two arguments on either side of it and produces an inclusive range between them.

## J, 53434139 38 characters

;(}.[:i.1+])/&.>".'- ,;'charsub 1!:1[1


Takes input from the keyboard:

   ;(}.[:i.1+])/&.>".'- ,;'charsub 1!:1[1
1-4,8-11
1 2 3 4 8 9 10 11


Output for the requested test case:

   ;(}.[:i.1+])/&.>".'- ,;'charsub 1!:1[1
1,3-5,9,16,18-23
1 3 4 5 9 16 18 19 20 21 22 23


# Hassium, 173 Bytes

This was pretty long and might not be competing since there is a trailing , at the end.

 func main(){p="1,2,3,5-8".split(",")for(c=0;c<p.length;c++){e=p[c]if(e.contains("-")){p=e.split("-")for(x=p[0].toInt();x<=p[1].toInt()print(x++ +",")){}}else print(e+",")}}


Run online and see expanded here

$_=<>;s/^/say join',',/;s/-/../g;eval  • - when you answer, show the output for s="1,3-5,9,16,18-23", thanks – BernaMariano Oct 3 '12 at 17:40 • How about say join",",eval<>=~s/-/../gr at 29 bytes – Brad Gilbert b2gills Nov 28 '15 at 19:15 ## Python 2.7, 147 138 Bytes z,f=input().split(','),[] for i in z: x=i.split('-') if len(x)>1:f+=range(int(x[0]),int(x[1])+1) else:f+=[int(x[0])] print str(f)[1:-1]  Usage: >>>python nums.py "1,3-5,9,16,18-23" 1, 3, 4, 5, 9, 16, 18, 19, 20, 21, 22, 23  Not the best program... • Welcome to PPCG. I think you can make your answer shorter by using 1 space for indents. – intrepidcoder Nov 26 '15 at 2:42 • Thanks @intrepidcoder, I didn't know that you could use single space indents. – Alex Nov 27 '15 at 1:26 ## MATLAB, 47 bytes disp(eval(['[',strrep(input(''),'-',':'),']']))  This snippet reads a string input from the command window, replaces '-' by ':', adds square brackets to the string and then evaluates it, so that the input will be expanded to a full array of numbers. Example input: '1,3-5,9,16,18-23'  Example output: 1 3 4 5 9 16 18 19 20 21 22 23  I believe this output is allowed, as the challenge only say that all numbers in a group should be displayed. • comma-separated output would be nicer, though i can se a 5-spaces-separated pattern, thats cool for me :) – BernaMariano Nov 27 '15 at 14:05 ## Perl 6, 36 bytes $_=get;say join ',',EVAL S:g/\-/../

1,3,4,5,9,16,18,19,20,21,22,23


## PowerShell, 79 71 bytes

('('+($args[0]-replace'-','..'-replace',','),(')+')'|iex|%{$_})-join','


Try it online!

The inner part changes "1,5-9,12" into a "(1),(5..9),(12)" format that PowerShell understands, then executes that with iex, which creates an array of arrays. Then iterate through each inner array, then finally join all outer array elements together

Borrows code from my "Help Me Manage My Time" answer

### Usage

PS C:\Tools\Scripts\golfing> .\return-each-number-from-a-group-of-numbers.ps1 '1,3-5,9,16,18-23'
1,3,4,5,9,16,18,19,20,21,22,23


-8 bytes thanks to Veskah

# K (oK), 40 31 bytes

Solution

,/{{x+!1+y-x}. 2#.:'"-"\x}'","\


Try it online!

Explanation:

Managed more golfing whilst adding the explanation...

,/{{x+!1+y-x}. 2#.:'"-"\x}'","\ / the solution
","\ / split input on ","
{                      }'     / apply lambda to each
"-"\x       / split x on "-"
.:'            / value (.:) each (')
2#               / 2 take (dupe if only 1 element)
{        }.                  / diadic lambda, 2 args x and y
y-x                    / y subtract x
!                         / range 0..n
,/                              / flatten


## Clojure, 110 bytes

#(clojure.string/join","(for[s(.split %",")[a b][(map read-string(.split s"-"))]r(if b(range a(inc b))[a])]r))


Dealing with strings isn't much fun :(

# Python 2, 112 bytes

L=[]
for s in input().split(','):
if'-'in s:a,b=map(int,s.split('-'));L+=range(a,b+1)
else:L+=[int(s)]
print L


Try it online!

# Ruby-p, 33 bytes

gsub(/(\d+)-(\d+)/){[*$1..$2]*?,}


Try it online!

# Japt, 12 bytes

q, c@OvXr-'ò


Try it

• Can you replace c@ with £? – Oliver Jul 19 '19 at 20:07
• @Oliver, as it's an old challenge that doesn't specify its I/O format, I erred on the side of caution, taking input as a comma delimited string and outputting as a flattened array. Normally, though, yes, I'd have specified input as an array of strings, output as a multi-dimensional array and just used £ in place of the first 5 bytes. – Shaggy Jul 19 '19 at 20:49

# Python 2, 90 bytes

lambda s:sum((range(u[0],u[-1]+1)for u in[map(int,t.split('-'))for t in s.split(',')]),[])


Try it online!