List numbers within given ranges [duplicate]

Goal:

Your goal is to print out numbers within a given range (Also including given values).

Input Format:

The input will look like: (int-int, int-int, ...)

... meaning the user can add as many int-int as they'd like.

Other Info:

• You will only have to handle non-negative integer numbers.
• The first integer in int-int will always be smaller, or the same, then the next integer in int-int
• The input will always have at least two int-int
• Each int-int will followed by a comma and a space, except for the last one.
• This is meaning the shortest answer wins!

Examples / Test Cases:

• Example 1:
• Input: (10-13, 11-15, 0-3)
• Output: 10 11 12 13 11 12 13 14 15 0 1 2 3

• Example 2:

• Input: (1-5, 1-15)
• Output: 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

• Example 3:

• Input: (1-2, 1-3, 1-3, 1-2)
• Output: 1 2 1 2 3 1 2 3 1 2

• Example 4:

• Input: (4-6, 0-1, 9-9)
• Output: 4 5 6 0 1 9
• Will the input have all the ( and the ,  or can we take an array of n-n for the input? – Downgoat Nov 25 '15 at 6:33
• @Downgoat It will have all the other (, , , ) too. – Alien G Nov 25 '15 at 6:34
• Will the first integer always be less than the second or can it be reversed and still be valid? e.g. 11-10 would give 11 10 I'd recommend adding a test case for that and for when the integers are the same (e.g. 10-10 -- though I'd imagine this would give 10) – cole Nov 25 '15 at 6:43
• @Cole Great question(s), yes the first integer will always be smaller and in the case of the numbers being the same it should just print that number (8-8 => 8). – Alien G Nov 25 '15 at 6:49
• Unfortunate that this was closed. It isn't a complete duplicate, as the other one requires comma-separated output. I have a cute answer (valid?) in ngn/apl: Instead of a program/function to do the work, I redefine minus to give the desired output: -←{(⊃~⍨/⍳¨⍺,1+↑⍵),1↓⍵}. Now (10-13, 11-15, 0-3) evaluates to 10 11 12 13 11 12 13 14 15 0 1 2 3! – Adám Nov 25 '15 at 17:34

CJam, 19

qS/{'.,Ser~),>~}%S*


Martin Büttner killed 2 bytes again, thanks :)
Try it online

Explanation:

q       read input
S/      split by space
{…}%    transform each string (containing a pair of numbers and some other chars)
'.,   enumerate all characters smaller than '.'
Ser   replace them all with a space in the string
~     evaluate the resulting string, pushing the 2 numbers on the stack
let's call them A and B
),    create an array [0 1 … B]
>     slice it from A: [A … B]
~     dump it onto the stack
S*      join the resulting array with spaces


Minkolang 0.13, 12 bytes

nd1+?.n~Lr$N  Try it here. Explanation n Take number from input (-1 if input is devoid of numbers) d1+ Duplicate and add 1 ?. If this is 0, stop. n~ Get number from input and negate it (because of the '-') L Pops b,a and pushes a,a+1,a+2,...,b-1,b r Reverses list$N     Outputs whole stack as numbers


Julia, 75 bytes

s->print((j=join)(map(i->j(i," "),eval(parse(replace(s,"-",":"))))," "))


This is an anonymous function that accepts a string and prints to STDOUT. To call it, give it a name, e.g. f=s->....

Ungolfed:

function f(s::AbstractString)
# Replace dashes with colons in the input
c = replace(s, "-", ":")

# Parse as a tuple of UnitRange objects
r = eval(parse(c))

# Join each range with spaces
m = map(i -> join(i, " "), r)

# Join into one string and print
print(join(m, " "))
end


Python 3, 76 bytes

l=eval(input().replace(*'-,'))
while l:a,b,*l=l;print(*range(a,b+1),end=' ')


The input is evaluated as a tuple by replacing each - with ,

(10-13, 11-15, 0-3) --> (10,13, 11,15, 0,3)


Then, the first two elements of the list are repeatedly lopped off and the numbers between them are printed.

Saved 2 bytes thanks to xsot.

• '-',',' can be shortened to *'-,'. – xsot Nov 25 '15 at 8:19
• @xsot Thanks. I wonder if there's something to be done with replace('-',',1+') to get the ranges right... – xnor Nov 25 '15 at 8:24

Javascript (ES 2015), 86 83 bytes

f=r=>{z="";r.replace(/(?:(\d+).(\d+))/g,(m,n,x)=>{do{z+=n+" "}while(n++<x)});alert(z)}


Edit 1: saved 3 bytes changing the body of the inner function into for(;n<=x;)z+=n+++" " (and I feel good when the horizontal scrollbar of my snippet at last disappears :) )

f=r=>{z="";r.replace(/(?:(\d+).(\d+))/g,(m,n,x)=>{for(;n<=x;)z+=n+++" "});alert(z)}


Ungolfed

f=r=>{                                       // define a function with 'r' arg

z="";                                      // initialize a variable to collect
// the output

r.replace(/(?:(\d+).(\d+))/g,(m,n,x)=>{    // for each range
// where (m=range, n=min, x=max)
for(; n<=x; ) {                          // until min <= max
z += n++ + " "                         // add to z the value and a space
}
});

}


f("(10-13, 11-15, 0-3, 9-9)");  // Output: 10 11 12 13 11 12 13 14 15 0 1 2 3 9


Python 2, 125 bytes

import re
print' '.join(map(lambda x:' '.join([str(y)for y in x]),eval(re.sub('(\d+)-(\d+)',r'range(\1,\2+1)',raw_input()))))


Try it online

I'm certain this could be golfed more...

CJam, 41 bytes (too long!)

Definitely can be golfed, but its a start. Test it online here.

l'(/e_')/e_',/{['-' er~]2,.+:,W%:-}%e_' *

l                                           e# Take string input
'(/e_                                      e# Remove '( characters
')/e_                                 e# Remove ') characters
',/                              e# Split on commas
{                   }%        e# Map over array...
['-' er~]                       e# Convert A-B to [A B]
2,.+                   e# Convert [A B] to [A B+1]
:,W%               e# Convert [A B+1] to [[0,1,2, ... B][0, 1, 2, ... A]]
:-             e# Remove element 1 of the previous array from element 2
e_' *   e# Format nicely
e# CJam prints stack after execution

• A few tips: 1. To remove '(', do '(-; even better in this case: 1>; similarly, ')- or W< 2. To convert A-B to [A B], do '-/:~ 3. To convert [A B] to [A … B], do ~),> 4. Instead of e_ after the loop, you can do ~ at the end of the loop 5. Use S instead of '  – aditsu Nov 25 '15 at 7:36

TeaScript, 18 + 1 = 19 bytes 20

+1 byte for "Inputs are arrays / lists?" checkbox

Such abuse...

xßer(${ls-}¬))  I've never abused JavaScript's casting more in my life. Try it online Ungolfed && Explanation My favorite part is JavaScript arrays when cast to a string have commas auto-inserted. We abuse this so the array elements are inserted as numbers and as separate arguments. So 1-2, would become ['1','2'] but when string casted 1,2 and this can be put directly in a function. xm(#er(${ls-}+1))

// The checkbox makes the input like ['1-2','3-4']
xm(#                )   // Loop through input
e                 // Eval string...
r(        +1)     // range function...
${ls-} // Convert range -> args // See above for more details  Mathematica, 80 bytes Join@@BlockMap[Range@@FromDigits/@#&,StringCases[#,DigitCharacter..],2]~Row~" "&  O, 75 74 bytes I'(-')-", "/]{[[n'-/]{n#}d[$,.$.0=0={.@=0={;.@=0=}w;}{;;}?r]]{n{noTo}d}d}d  This makes logical sense to anyone who built the language (i.e. me). The logic behind this is intense and I'm just happy that I got it working. Explanation: I'(-')-", "/] Format inputs into array { Iterate over each input range [[n'-/]{n#}d Split ranges into sets of numbers [$,              Get the second number and get a range from 0 to it
.\$.0=0=          Is the first number 0?
{.@=0={;.@=0=}w;} True: Remove any unneeded numbers
{;;}?             False: Pop some random stuff that shouldn't have been on there to begin with
r]]              Reverse the range and close some array stuff or whatever
{n{noTo}d}d}     Format output
}d

• No idea what any of it does but good job. – Alien G Nov 25 '15 at 7:36
• Maybe would be better to specify the version too. As I see, your answers work only with the new C interpreter, but until you document it I will keep posting answers which work only with the old Java interpreter (like the 99 bottles of beer one). – manatwork Nov 25 '15 at 9:05
• @manatwork That answer actually works on the IDE. The C interpreter has most of the same features as the old one, and most old programs will work unless they do crazy stuff. – phase Nov 25 '15 at 9:08
• Hmm… Indeed. Maybe I tested only an older version of the code. But on thing is sure, was not the only time we didn't understood each other with the online interpreter. – manatwork Nov 25 '15 at 9:54