| Bytes | Lang | Time | Link |
|---|---|---|---|
| 079 | Javascript ES 2015 | 151125T093001Z | Fabrizio |
| 074 | O | 151125T073524Z | jado |
| 080 | Mathematica | 151125T083846Z | alephalp |
| 076 | Python 3 | 151125T080649Z | xnor |
| nan | TeaScript | 151125T064413Z | Downgoat |
| 019 | CJam | 151125T071806Z | aditsu q |
| 075 | Julia | 151125T070416Z | Alex A. |
| 041 | CJam | 151125T064743Z | GamrCorp |
| 125 | Python 2 | 151125T064457Z | user4594 |
| 012 | Minkolang 0.13 | 151125T064230Z | El'e |
Javascript (ES 2015), 86 83 79 bytes
f=r=>{z="";r.replace(/(\d+).(\d+)/g,(m,n,x)=>{for(;n<=x;)z+=n+++" "});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
}
});
alert(z) // alert the output
}
f("(10-13, 11-15, 0-3, 9-9)"); // Output: 10 11 12 13 11 12 13 14 15 0 1 2 3 9
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.
Try it online or RUN EVERY TEST CASE AT THE SAME TIME
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
Mathematica, 80 bytes
Join@@BlockMap[Range@@FromDigits/@#&,StringCases[#,DigitCharacter..],2]~Row~" "&
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.
TeaScript, 18 + 1 = 19 bytes 20
+1 byte for "Inputs are arrays / lists?" checkbox
Such abuse...
xße`r(${ls`-`}¬)`)
I've never abused JavaScript's casting more in my life.
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(#e`r(${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
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
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
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
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()))))
I'm certain this could be golfed more...
Minkolang 0.13, 12 bytes
nd1+?.n~Lr$N
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