22
\$\begingroup\$

Given a list with number, output the ranges like this:

Input: [0, 5, 0] would become [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0].

This is mapping a range through the array, so we first have to create the range [0, 5], which is [0, 1, 2, 3, 4, 5]. After that, we use the 5 to create the range [5, 0]. Appended at our previous range, this gives us:

[0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]

Let's observe a test case with two same digits next to each other:

[3, 5, 5, 3], ranges:
[3, 5] = 3, 4, 5
[5, 5] = 5 (actually [5, 5] due to overlapping)
[5, 3] = 5, 4, 3

So this would give us [3, 4, 5, 5, 4, 3].

Some other test cases:

[1, 9] > [1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, -10] > [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
[3, 0, 0, -3] > [3, 2, 1, 0, 0, -1, -2, -3]
[1, 3, 5, 7, 5, 3, 1, -1, -3] > [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3]

Input will always have at least 2 integers.

Shortest answer wins!

asked Mar 5, 2016 at 14:16
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Related. Related. \$\endgroup\$ Commented Mar 5, 2016 at 14:22
  • 1
    \$\begingroup\$ In what way are input and output related? What constitutes a valid input? \$\endgroup\$ Commented Mar 5, 2016 at 14:31

20 Answers 20

21
\$\begingroup\$

05AB1E, 1 byte

Ÿ

Try it online!

How it works

It's a built-in.

answered Mar 5, 2016 at 14:54
\$\endgroup\$
5
  • 21
    \$\begingroup\$ Do you have a dictionary of all built-ins in all esolangs in your head, or what? ;) \$\endgroup\$ Commented Mar 5, 2016 at 17:51
  • 10
    \$\begingroup\$ Why does it even have a built-in for this? \$\endgroup\$ Commented Mar 5, 2016 at 20:24
  • \$\begingroup\$ There should be a compilation of all 0byte and 1byte (maybe even 2byte) programs that do stuff. \$\endgroup\$ Commented Mar 6, 2016 at 15:56
  • 3
    \$\begingroup\$ @Neil It's basically an inclusive range function, it's really not that spectacular. \$\endgroup\$ Commented Mar 6, 2016 at 19:53
  • \$\begingroup\$ I challenge you with this question: codegolf.stackexchange.com/questions/240385/… :-) \$\endgroup\$ Commented Dec 31, 2021 at 9:31
5
\$\begingroup\$

Javascript, (削除) 99 (削除ここまで) (削除) 95 (削除ここまで) 93 bytes

(削除) 4 (削除ここまで) 6 bytes off thanks @Neil.

a=>a.reduce((x,y)=>x.concat(b.map?b=y:[...Array(y<b?b-y:y-b||1)].map(_=>b+=y<b?-1:y>b)),b=[])

f=
a=>a.reduce(
 (x,y)=>
 x.concat(
 b.map?b=y
 :[...Array(y<b?b-y:y-b||1)]
 .map(_=>b+=y<b?-1:y>b)
 )
 ,b=[])
G.addEventListener('click',_=>O.innerHTML=f(JSON.parse(I.value)));
<input id=I value="[3,5,5,3]"><button id=G>Go</button><pre id=O>

answered Mar 5, 2016 at 17:35
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Save 3 bytes by using y<b?b-y:y-b||1. Save another byte by using y>b||y-b&&-1. \$\endgroup\$ Commented Mar 5, 2016 at 20:15
  • \$\begingroup\$ @Neil. Good ones!! Thanks :) \$\endgroup\$ Commented Mar 5, 2016 at 20:24
  • 1
    \$\begingroup\$ Actually y<b?-1:y>b is better still. \$\endgroup\$ Commented Mar 5, 2016 at 23:45
5
\$\begingroup\$

JavaScript (SpiderMonkey 30+), (削除) 81 (削除ここまで) 76 bytes

([n,...a])=>[n,...[for(i of a)for(j of Array(i<n?n-i:i-n||1))n+=i<n?-1:i>n]]

Tested in Firefox 44. Uses ES6's awesome argument destructuring capabilities and ES7's array comprehensions (which have sadly been removed from the ES7 spec).

answered Mar 5, 2016 at 18:05
\$\endgroup\$
3
  • \$\begingroup\$ Doesn't work on [3, 0, 0, -3]. I fixed the RangeError and saved 10 bytes but it still doesn't work: ([n,...a],z=[n])=>z.concat([for(i of a)for(j of[...Array((r=n<i)?i-n-1:n-i-1),0])i=r?++n:--n]) \$\endgroup\$ Commented Mar 5, 2016 at 20:09
  • \$\begingroup\$ Sorry, I meant ([n,...a])=>[n].concat([for(i of a)for(j of[...Array((r=n<i)?i-n:n-i)])i=r?++n:--n]) of course. \$\endgroup\$ Commented Mar 5, 2016 at 20:26
  • \$\begingroup\$ @Neil Fixed, with a bunch more golfed off in the process \$\endgroup\$ Commented Mar 6, 2016 at 1:27
4
\$\begingroup\$

JavaScript (ES6) 66 (削除) 72 (削除ここまで)

A recursive function that repeatedly adds values inside the array to fill the gaps between near numbers

f=l=>l.some((x,i)=>(z=l[i-1]-x)*z>1&&l.splice(i,0,x+z/2|0))?f(l):l

Test

f=l=>l.some((x,i)=>(z=l[i-1]-x)*z>1&&l.splice(i,0,x+z/2|0))?f(l):l
console.log=x=>O.textContent+=x+'\n'
;[[1,9],[10,-10],[3,0,0,-3],[1, 3, 5, 7, 5, 3, 1, -1, -3]]
.forEach(t=>console.log(t+' -> ' +f(t)))
<pre id=O></pre>

answered Mar 6, 2016 at 22:09
\$\endgroup\$
3
\$\begingroup\$

C, 120 + 12 = 132 bytes

i,j,k;f(a,n)int*a;{a[0]--;for(i=0;i<n-1;i++)for(k=0,j=a[i]-a[i+1]?a[i]:a[i]-1;j-a[i+1];)printf("%i ",j+=a[i+1]>j?1:-1);}

Example call:

f(a,sizeof(a)/4); // I've added 12 bytes because of ",sizeof(a)/4"

Test live on ideone.

answered Mar 5, 2016 at 17:38
\$\endgroup\$
3
\$\begingroup\$

Python 2, 77 bytes

lambda n:n[0:1]+sum([range(x,y,2*(y>x)-1)[1:]+[y]for(x,y)in zip(n,n[1:])],[])

Try it online

Thanks to Neil, DenkerAffe, and Erwan for pointing out improvements that I missed

answered Mar 5, 2016 at 17:16
\$\endgroup\$
5
  • \$\begingroup\$ Surely the +1 is unnecessary? \$\endgroup\$ Commented Mar 5, 2016 at 20:21
  • \$\begingroup\$ why not go with lambda n:n[0:1]+sum([range(x,y,[1,-1][y+1<x])[1:]+[y]for(x,y)in zip(n,n[1:])],[])? saves some bytes. \$\endgroup\$ Commented Mar 5, 2016 at 22:46
  • \$\begingroup\$ I was very tired while writing this :) Answer first, improve later. \$\endgroup\$ Commented Mar 5, 2016 at 23:43
  • \$\begingroup\$ i think you can replace [1,-1][y+1<x] by 2*(y>x)-1 (also i don't understand why you use y<=x and not simply y<x ) \$\endgroup\$ Commented Mar 9, 2016 at 9:04
  • \$\begingroup\$ n[0:1] is equivalent to n[:1]. \$\endgroup\$ Commented Oct 7, 2017 at 16:18
3
\$\begingroup\$

Perl, 47 bytes

Includes +3 for -p (code contains $' so space and - count too)

Give the list of numbers on STDIN:

fluctuating.pl <<< "3 5 5 3"

fluctuating.pl:

#!/usr/bin/perl -p
($n=$&+($'<=>$&))-$'&&s/\G/$n / while/\S+ /g

The temporary variable and all these parenthesis feel suboptimal...

answered Sep 7, 2016 at 15:56
\$\endgroup\$
2
  • \$\begingroup\$ It looks like you posted the wrong answer : it doesn't seem to be working and that $' you mentioned isn't in the code... \$\endgroup\$ Commented Sep 7, 2016 at 20:33
  • \$\begingroup\$ @Dada: Yes, again pasted an old untested version of the code instead of the fixed one. Thanks and fixed \$\endgroup\$ Commented Sep 7, 2016 at 22:47
2
\$\begingroup\$

Haskell, (削除) 63 (削除ここまで) 55 bytes

g(a:b:r)=[a|a==b]++[a..b-1]++[a,a-1..b+1]++g(b:r)
g x=x

Usage example: g [3,5,5,3] -> [3,4,5,5,4,3].

It's a modification of my answer to a related challenge. Again, the main work is done by concatenating the list from a upwards to b-1 and from a downwards to b+1 (where one list will be empty) and a recursive call. To handle the a==b case where both lists are empty, we prepend [a|a==b] which evaluates to [a] if a==b and [] otherwise.

answered Mar 5, 2016 at 17:48
\$\endgroup\$
2
\$\begingroup\$

R, (削除) 86 (削除ここまで) (削除) 82 (削除ここまで) 75 bytes

function(x)rep((y<-rle(unlist(Map(seq,head(x,-1),x[-1]))))$v,pmax(1,y$l-1))

saved 4 bytes using rep not rep.int (code golf not performance!) saved another 7 bytes by using built-in partial matching when using $ (and collapsing function definition to 1 line

answered Mar 6, 2016 at 22:26
\$\endgroup\$
1
  • \$\begingroup\$ I think (y=...) rather than (y<-...) is also valid, and one byte less. \$\endgroup\$ Commented Oct 6, 2017 at 14:51
2
\$\begingroup\$

Ruby, (削除) 116 (削除ここまで) 82 bytes

->n{o,*m=n;o=[o];m.zip(n).map{|t,u|o+=[[u],[*u+1..t],[*t..u-1].reverse][t<=>u]};o}

My first ever golf.

Edit: Thanks manatwork for the awesome suggestions.

answered Mar 7, 2016 at 17:00
\$\endgroup\$
2
  • \$\begingroup\$ No need to assign to variable, anonymous proc is enough; no need to put parenthesis around formal parameter; taking out the array's first element is shorter with parallel assignment and splat; map's code block can take the array as multiple parameters: ->n{o,*m=n;o=[o];m.zip(n).map{|t,u|o+=u==t ?[u]:(u<t ?[*u+1..t]:[*t..u-1].reverse)};o}. Otherwise nice first golf. \$\endgroup\$ Commented Mar 8, 2016 at 7:17
  • \$\begingroup\$ Picking from a 3 element array by spaceship operator is shorter than 2 ternary operators: [[u],[*u+1..t],[*t..u-1].reverse][t<=>u]. \$\endgroup\$ Commented Mar 8, 2016 at 7:40
2
\$\begingroup\$

Japt, 12 bytes

Saved 16 bytes thanks to @ETHproductions!

ä!õ ËsE©DÊ>1

Test it online

answered May 18, 2018 at 20:05
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 94 bytes

I'm not super happy with this right now, I'll probably take another shot later

{reduce {|@^a[0..*-2],|@^b},map {@_[0]!= @_[1]??(@_[0]...@_[1])!!(@_[0],@_[1])},.rotor(2=>-1)}
answered Mar 6, 2016 at 0:39
\$\endgroup\$
1
\$\begingroup\$

PHP 5.4, 86 bytes

This is meant to be used as an included file, that returns the result.

The values are passed as commandline parameters.

<?for($i=1;$i<$argc-1;$R=array_merge($R?:[],range($argv[$i++],$argv[$i++])));return$R;

Not exactly pretty or anything, but does the job.

answered Mar 6, 2016 at 21:03
\$\endgroup\$
1
\$\begingroup\$

Python 3, 76 bytes

First attempt at a Python answer. The basic idea is to repeatedly identify pairs in the sequence where the difference is larger than a step and insert one (and only one) additional element to complete the sequence in the right direction. Repeat until all differences between consecutive elements are between +1 and -1.

d=diff
while any(d(x)**2>1):i=argmax(d(x)**2);x[:i+1]+=[x[i]+sign(d(x)[i])]

Try it online!

answered Oct 9, 2017 at 11:40
\$\endgroup\$
0
\$\begingroup\$

Lua, 156 Bytes

A function that takes an array in parameter and return the extended array.

function f(t)r={}for i=2,#t
do x,y=t[i-1],t[i]r[#r+1]=x==y and x or nil
z=x>y and-1or 1
x=x==r[#r]and x+z or x
for j=x,y,z
do r[#r+1]=j end end
return r end

Ungolfed and explanations

function f(t)
 r={} -- Initialise an empty array
 for i=2,#t -- Iterate over the parameter array
 do
 x,y=t[i-1],t[i] -- x and y are shorter names than t[i-1]
 r[#r+1]= -- when there's a range like [5,5]
 x==y and x or nil -- put this number once in the array
 z=x>y and-1or 1 -- determine the step value
 x= x==r[#r] -- prevent repeating the last value of r
 and x+z or x -- by incrementing/decrementing x
 for j=x,y,z -- iterate from t[i-1] to t[i] by step z (-1 or 1)
 do
 r[#r+1]=j -- put j into the array r
 end
 end
 return r -- return the extended array
end

For ease of use, you can use the following function to print the array returned by f().

function printArray(t)
 print("["..table.concat(t,",").."]")
end

When testing this submission, you can call it like:

printArray(f( {0,5,0,3,4,4,7,3,-3} ))
> [0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,4,5,6,7,6,5,4,3,2,1,0,-1,-2,-3]
answered Mar 7, 2016 at 10:27
\$\endgroup\$
0
\$\begingroup\$

Mathcad, 62 "bytes"

enter image description here

As Mathcad uses a 2D "whiteboard" and special operators (eg, summation operator, integral operator), and saves in an XML format, an actual worksheet may contain several hundred (or more) characters. For the purposes of Code Golf, I've taken a Mathcad "byte count" to be the number of characters or operators that the user must enter to create the worksheet.

Converting the function definition to a straight program, and replacing the variable lst with a single character name, gives a total of 62 "bytes". With the function, using a single character rather than the full name, this increases to 65 "bytes" for the definition and a further 4 "bytes" for each call (assuming that creation of the list itself isn't included in the overall byte count (Using Mathcad's built-in tables is another way of inputting the list).

answered Mar 7, 2016 at 18:06
\$\endgroup\$
0
\$\begingroup\$

PHP, 144 bytes

function f($r){$m=[];for($i=0;++$i<count($r);){$d=array_pop($m);$n=$r[$i];$p=$r[$i-1];$m=array_merge($m,$p==$n?[$p,$n]:range($p,$n));}return$m;}
Exploded view
function f($r) {
 $m = [];
 for ($i=0; ++$i < count($r); ) {
 $d = array_pop($m);
 $n = $r[$i];
 $p = $r[$i-1];
 $m = array_merge($m, $p==$n ? [$p,$n]
 : range($p,$n));
 }
 return $m;
}
Input / function call
f([ bound1, bound2, bound3, ... ]);
Output
[int, int, int, int, ...]

It's messy and chunky, and I'll try to optimize it later. It creates a range() from each pair of adjacent value pairs, then stitches them together (after poping off the end of the previous cumulative Array).

answered Mar 7, 2016 at 18:07
\$\endgroup\$
0
\$\begingroup\$

Perl6, 21

.join is short for $_.join

say EVAL .join: "..."

Test (rakudo)

perl6 -MMONKEY-SEE-NO-EVAL -e'say EVAL @*ARGS.join: "..."' 1 3 5 7 5 3 1 -1 -3

Output

(1 2 3 4 5 6 7 6 5 4 3 2 1 0 -1 -2 -3)
answered Mar 8, 2016 at 20:24
\$\endgroup\$
0
\$\begingroup\$

Jelly, 10 bytes

,r=?2\Ṗ;\/

Try it online!

answered Oct 7, 2017 at 10:00
\$\endgroup\$
-1
\$\begingroup\$

R, 74 bytes

Another R solution

function(x){b=x[1];z=c();for(a in x[-1]){z=c(z,c(b:(a-sign(a-b))));b=a};z}

Try it online!

answered Oct 7, 2017 at 15:33
\$\endgroup\$
1
  • \$\begingroup\$ this doesn't quite work as the last value seems to be missing... \$\endgroup\$ Commented May 17, 2018 at 20:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.