4
\$\begingroup\$

This question already has an answer here:

Take an input list L containing positive integers, and insert the least amount of integers into the sequence, so that you make it a continuous one.

Example:

L = 4 2 6 1
Out: 4 3 2 3 4 5 6 5 4 3 2 1

The input and output formats are optional.

Test cases:

L = 1
Out: 1

L = 10 4
Out: 10 9 8 7 6 5 4

L = 13 13
Out: 13 13

L = 3 6
Out: 3 4 5 6

L = 4 4 1 1 4 4
Out: 4 4 3 2 1 1 2 3 4 4

This is ... You know the rules.

\$\endgroup\$

marked as duplicate by Martin Ender code-golf Oct 6 '17 at 9:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ I'm almost certain this is a duplicate but I can't find the challenge… \$\endgroup\$ – Fatalize Oct 6 '17 at 9:18
  • \$\begingroup\$ Related \$\endgroup\$ – Fatalize Oct 6 '17 at 9:20
  • \$\begingroup\$ I tried finding a similar one myself, but I couldn't find any. I thought it would make a nice simple challenge. My favorite language is a bit messy when it comes to creating ranges in both ascending and descending order at the same time. \$\endgroup\$ – CG. Oct 6 '17 at 9:21
  • \$\begingroup\$ @Fatalize I didn't find that one when I searched. I do feel this is different enough, but I don't know how strict the community is when it comes to similar challenges...? I guess there will be some answers that are easily portable though... \$\endgroup\$ – CG. Oct 6 '17 at 9:24
  • 1
    \$\begingroup\$ Test case: [4,4,1,1,4,4] \$\endgroup\$ – Zgarb Oct 6 '17 at 9:29
2
\$\begingroup\$

Builtin

Husk:

Try it online!

I bet this builtin exists in more than one language, so I made this a community wiki, just add in more languages where this is a builtin if you know them!

\$\endgroup\$
2
\$\begingroup\$

Brachylog, 11 bytes

⊆.s₂ᵇ-ᵐ~ṡᵐ∧

Try it online!

It's long and slow but at least it's declarative…

Explanation

⊆.        ∧       The Input is an ordered subset of the Output
  s₂ᵇ             Take all substring of 2 consecutive elements in the Output
     -ᵐ           Take the subtractions of all those substrings
       ~ṡᵐ        They must all be valid "ṡigns" (i.e 1, 0 or -1)
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 72 bytes

Another approach using ReplaceAll.

#//.{(p={a___,b_,c_,d___})/;b>c+1->(f={a,b,b-#,c,d}&)@1,p/;b<c-1->f@-1}&
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.