# The first, the last, and everything between

Given two integers, output the two integers, and then the range between them (excluding both).

The order of the range must be the same as the input.

### Examples:

 Input        Output
0,  5   ->   [0, 5, 1, 2, 3, 4]
-3,  8   ->   [-3, 8, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
4,  4   ->   [4, 4]
4,  5   ->   [4, 5]
8,  2   ->   [8, 2, 7, 6, 5, 4, 3]
-2, -7   ->   [-2, -7, -3, -4, -5, -6]

• I guess we can't take the inputs in pre-ordered order? Commented Nov 8, 2018 at 9:55
• Is this output format acceptable? Note the newline Commented Nov 8, 2018 at 10:48
• @LuisMendo I'll allow it :) Commented Nov 8, 2018 at 10:55
• I assume a space or newline delimited string is also allowed as output, instead of an actual array/list/stream? Commented Nov 8, 2018 at 13:06
• @KevinCruijssen Any reasonable I/O is acceptable. Commented Nov 8, 2018 at 13:08

# Dart, 85 84 bytes

f(a,b)=>[a,b]+((a-b).abs()>1?List.generate((a-b).abs()-1,(i)=>(a>b?-i-1:i+1)+a):[]);


Try it online!

• -1 by going from >= to >

# QBASIC, 39 53 bytes

INPUT a,b
?a
?b
FOR q=a+1TO b-1 STEP SGN(b-a)
?q
NEXT


Added the STEP parameter to account for a>b, and that uses the SGN() function to get a -1 or a +1 as increment. This however breaks the REPL because the SGN() function isn't implemented there...

Try it (the old answer) online!

# C (gcc), 65 bytes

f(a,b){for(printf("%d %d",a,b);a<b?++a<b:--a>b;)printf(" %d",a);}


Try it online!

Not very exciting. The loop increment is borrowed from an early version of Kevin Cruijssen's Java answer.

# Powershell, 41 bytes

param($a,$b)$a;$b;$a..$b|?{$_-notin$a,$b}  Less golfed test script: $f = {

param($a,$b)
$a # push$a to a pipe
$b # push$b to a pipe
$a..$b|?{         # push to pipe all integers from $a to$b
$_-notin$a,$b # ...except$a and $b itself } } @( ,( 0, 5 , 0, 5, 1, 2, 3, 4) ,(-3, 8 , -3, 8, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7) ,( 4, 4 , 4, 4) ,( 4, 5 , 4, 5) ,( 8, 2 , 8, 2, 7, 6, 5, 4, 3) ,(-2, -7 , -2, -7, -3, -4, -5, -6) ) | % {$a,$b,$expected = $_$result = &$f$a $b "$("$result"-eq"$expected"): $result" }  Output: True: 0 5 1 2 3 4 True: -3 8 -2 -1 0 1 2 3 4 5 6 7 True: 4 4 True: 4 5 True: 8 2 7 6 5 4 3 True: -2 -7 -3 -4 -5 -6  Explanation: The basic concept of Powershell is the pipe. Pipe is an array. All results that push into the pipe fall into the array. So we should just push the values into the pipe in the correct order. # Java, 739, 555 bytes  public static List<Integer> firstLastAndEverythingBetween(final int a, final int b) { if (a == b) { return addAB(a, b); } final List<Integer> result = addAB(a, b); int initial = getInitial(a, b); for (int n = 1; n < Math.abs(b - a); n++) { result.add(initial); if (b > a) { initial++; } else { initial--; } } return result; } private static int getInitial(int a, int b) { return (b > a) ? (a + 1) : (a - 1); } private static List<Integer> addAB(int a, int b) { final List<Integer> result = new ArrayList<>(); result.add(a); result.add(b); return result; }  • This reminds me of my first post on here (in LUA). You should really make an effort to atleast remove the white space. I'm no Java programmer, but even I can see some quicky areas for golfing. Consider taking a look at this before the negative votes start piling up. Also, there is a templated style for posting code with the language name and number of bytes as a heading. I'd incorporate that. Here is a link to an online byte counter mothereff.in/byte-counter Commented Nov 8, 2018 at 17:00 • Ok, learning about hte Code golf, I will do better with time Commented Nov 8, 2018 at 17:02 • Welcome to PPCG! Good to have you with us :) Commented Nov 8, 2018 at 17:27 • Good first effort. You'll soon learn the tricks. First one is to delete all of the unneeded spaces and shorten all variable and function names to one letter. Just the kind of things you wouldn't do in "real life" (hopefully!). Welcome on board. Commented Nov 8, 2018 at 20:38 • How do I compile this? Simply putting it in a main class doesn't seem to work. Commented Nov 9, 2018 at 2:02 # Python 3, 53 48 bytes -5 bytes thanks to @mypetlion. lambda a,b:[a,b,*range(a+1-2*(a>b),b,1-2*(a>b))]  Try it online! • Save 5 bytes with starred expression to unpack the range object lambda a,b:[a,b,*range(a+1-2*(a>b),b,1-2*(a>b))] Commented Nov 8, 2018 at 19:01 # APL(NARS), 14 chars, 28 bytes {⍺⍵,¯1↓1↓⍺..⍵}  test  f←{⍺⍵,¯1↓1↓⍺..⍵} ¯3 f 8 ¯3 8 ¯2 ¯1 0 1 2 3 4 5 6 7 0 f 5 0 5 1 2 3 4  # C (clang), 60 bytes f(a,b,c)int*c;{for(*c++=a,*c++=b;(a<b?++a<b:--a>b);*c++=a);}  Try it online! • Can ditch the parens in the middle argument of for() Commented Nov 17, 2018 at 18:21 ## C++, 143 140 bytes -3 bytes thanks to Zacharý #define V std::vector<int #include<vector> V>f(int a,int b){V>v{a,b};int c=2*(b>a)-1;for(int i=a+c;a!=b&&b!=i;i+=c)v.push_back(i);return v;}  How to use : In the main function : std::cout << f(0, 5) << '\n'; std::cout << f(-3, 8) << '\n'; std::cout << f(4, 4) << '\n'; std::cout << f(4, 5) << '\n'; std::cout << f(8, 2) << '\n'; std::cout << f(-2, -7) << '\n';  also add : template<typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { os << "[ "; for (const auto& a : v) { os << a << ' '; } return os << ']'; }  • You can chance the auto to int, as well as using a #define for std::vector<int> Commented Nov 10, 2018 at 2:39 • #define V std::vector<int, and replace V f with V>f and V v with V>v for -1 byte. Commented Nov 12, 2018 at 15:26 • Suggest c=b>a?1:-1 instead of c=2*(b>a)-1 and a-b&&b-i instead of a!=b&&b!=i Commented Nov 20, 2018 at 17:20 # Haskell, 18 34 bytes as function, 46 52 as an IO action, 56 62 as program x!y=x:y:[x+1..y-1]++[x-1,x-2..y+1]  or, as an IO action to take input, there are these options (all the same length): main=(\x y->x:y:[x+1..y-1]++[x-1,x-2..y+1])<$>readLn<*>readLn
--
x!y=x:y:[x+1..y-1]++[x-1,x-2..y+1]
main=(!)<$>readLn<*>readLn -- f x y=x:y:[x+1..y-1]++[x-1,x-2..y+1] main=f<$>readLn<*>readLn


or, as an IO action taking input and printing the result:

main=((\x y->x:y:[x+1..y-1]++[x-1,x-2..y+1])<$>readLn<*>readLn)>>=print -- x!y=x:y:[x+1..y-1]++[x- 1,x-2..y+1] main=((!)<$>readLn<*>readLn)>>=print
--
f x y=x:y:[x+1..y-1]++[x-1,x-2..y+1]
main=(f<$>readLn<*>readLn)>>=print  The last one is also available on Try It Online! (You can try it online there) Please remember that TIO requires the input be entered beforehand in the "Input" box. De-golfed: enumerate' x y = x : y : [x + 1 .. x - 1] ++ [x - 1 , x - 2 .. y + 1] main = do x <- readLn y <- readLn print (enumerate' x y)  Note: : is an operator to prefix an element to a list. Note: a!b will not work if -XBangBatterns is enabled. Use a#b instead. Note: -XNegativeLiterals will break this. Insert a space between x- and 1. This will parse as x + (negate 1) instead of x$ (-1).

• I feel obligated to point out that your shortest solution is essentially the same as nimi's. Commented Nov 11, 2018 at 1:54
• Oh, sorry. Should I remove this? Commented Nov 11, 2018 at 8:16
• You don't need to. According to this consensus on Meta, duplicate answers are fine. Commented Nov 11, 2018 at 10:40

# C# (Visual C# Interactive Compiler), 86 bytes

x=>y=>{var l=new List<int>{x,y};for(int s=x>y?-1:1;x!=y&(x+=s)!=y;)l.Add(x);return l;}


Try it online!

-1 thanks @auhmaan!

• You can save a byte by replacing the && with a single &; Also, since your answer isn't for C# (Visual C# Interactive Compiler) you need to add to your byte count the usings - which is System.Collections.Generic for this solution, if I'm not mistaken. Commented Nov 12, 2018 at 17:21
• Thanks for the tip - I've updated my answer. I kind of like this compiler better :)
– dana
Commented Nov 12, 2018 at 17:31
• It also lowers your byte count a lot Commented Nov 12, 2018 at 17:53

## JavaScript, 75 bytes

(a,b,n=-~(a>b?a-b:b-a)-2)=>[a,b,...[...Array(n<2?0:n)].map(_=>a<b?++a:--a)]


Try it online!

• 71 bytes Commented Jul 25, 2019 at 20:15

# Husk, 5 bytes

+¹ht…


Try it online!

Thanks to Dominic Van Essen for the fix.

• I'm afraid I think you'll need to use 5 bytes to pass the [4,4] test case... Commented Jan 24, 2021 at 13:03

# Tcl, 85 bytes

proc R a\ b {puts $a\n$b
if $a>$b {lassign $a\$b b a}
while {[incr a]<$b} {puts$a}}


Try it online!

# JavaScript (ES6), 97 bytes

a=>b=>[a,b,...((c,d)=>c==d?[]:(s=(m=Math).min(a,b),Array(m.abs(a-b)-1).fill().map(n=>++s)))(a,b)]


Can probably be improved!!

# Python 3, 57 bytes

def m(a,b):s=(1,-1)[a>b];return[a,b]+list(range(a+s,b,s))


Try it online!

• Same approach down to 49 bytes: def m(a,b):s=1-2*(a>b);print(a,b,*range(a+s,b,s)) Commented Nov 8, 2018 at 19:04

JAVA 147 bytes

f(int a, int b){System.out.print(a+""+b);if(a<b){for(int i=a+1; i<b;i++){System.out.print(i);}}else{for(int i=a-1;i>b;i--){System.out.print(i);}}}


# Racket, 68 bytes

(define(f a b)(let([s(if(> a b)-1 1)])(list* a b(range(+ a s)b s))))


Try it online!

The same as TheGreatGeek's Clojure solution

# perl -M5.010 -alE, 58 bytes

@_=($F[0]..$F[1],reverse$F[1]..$F[0]);say"$_ @_[1..$#_-1]"


This reads two integers (on a single line, separated by whitespace) from STDIN and writes the result to STDOUT.

# Bash, 62 bytes

a=($(seq$1 $2|tr$'\n' ' '))
echo $@${a[@]:1:((${#a[@]}-2))}  ## explanation seq does we want but produces lines, tr turns the lines into space-delimited output, the outer parens make that into an array. $@ is the original input, and the rest of it slices the 0-indexed array from 1 and takes the next "array len - 2" chars.

Try it online!

(NOTE: not all test cases work on TIO for reasons i don't understand, but they do work on my local machine)

# C# (.NET Core), 99 bytes

a=>b=>new[]{a,b}.Concat(b>a?Enumerable.Range(a+1,b+~a):Enumerable.Range(b+1,a!=b?a+~b:0).Reverse())


Try it online!

First (and probably last) time golfing in C#.

# F# (Mono), 68 bytes

let f x y=
let s=if x<y then 1 else-1
Seq.append[x;y][x+s..s..y-s]


Try it online!

# C# .NET Core, 56 441 bytes

Here is my corrected solution without predefined input variables and without string joining.

(a,b)=>n(a,b).c(k(a-b)>1?b>a?r(a+1,b-a-1):r(b+1,a-b-1).i():n());


Try it online!

n(a,b)               => returns new array with a and b in it
c(                 => concat the following content
k(a-b)>1         => when absolute value of a-b greater then one
b>a            => when b bigger a
r(a,b-a)     => range from a to b-a
r(b,a-b).i() => else range from b to a-b reversed
n()            => else empty array


I've created and used the following declarations. When I include those and the minimum code required to compile in the byte count I get 640 bytes.

public static class Extensions{
public static IEnumerable<T> c<T>(this IEnumerable<T> a, IEnumerable<T> b) => a.Concat(b);
public static IEnumerable<T> i<T>(this IEnumerable<T> a) => a.Reverse();
}

public static Func<int, int> k = Math.Abs;
public static Func<int, int, IEnumerable<int>> r = Enumerable.Range;
public static int[] n(params int[] a) => a;


using System.Linq;using System.Collections.Generic;using System;public static class E{public static IEnumerable<T> c<T>(this IEnumerable<T> a,IEnumerable<T> b)=>a.Concat(b);public static IEnumerable<T> i<T>(this IEnumerable<T> a)=>a.Reverse();}public class P{public static Func<int,int> k=Math.Abs;public static Func<int,int,IEnumerable<int>> r=Enumerable.Range;public static int[] n(params int[] a)=>a;public static void Main(string[] args){Console.WriteLine($"[ {string.Join(", ", f(int.Parse(args[0]), int.Parse(args[1])))} ]");}public static Func<int,int,IEnumerable<int>> f=(a,b)=>n(a,b).c(k(a-b)>1?b>a?r(a,b-a):r(b,a-b).i():n());}  • What exactly are your "tricks"? – kepe Commented Nov 8, 2018 at 18:23 • @FireCubez all the declarations which aren't in the body. Is that allowed? Commented Nov 8, 2018 at 18:35 • Which body? Take a look at the standard loopholes. If it isn't there then it's allowed – kepe Commented Nov 8, 2018 at 19:01 • @TFeld I've corrected it. It's just not that nice anymore^^ Commented Nov 9, 2018 at 8:41 • You have to include it in the bytecount – Jo King Commented Nov 9, 2018 at 9:56 # F#, 67 bytes let f x y=if(y-x<0)then[x;y]@List.rev[y+1..x-1]else[x;y]@[x+1..y-1]  Try it online! Appends a list containing x and y and a list iterating from x+1 to y-1 (if positive) or a list in reverse iteration order from y+1 to x-1 (if negative) # Perl 5-pl, 46 bytes / /;$_.=join$",'',$+1..$'-1,reverse$'+1..$-1  Try it online! # Arturo, 3028 27 bytes $[x,y][p:@[x,y]p++--x..y p]


Try it

$[x,y][ ; a function taking arguments x and y p: @[x,y] ; assign the list [x,y] to p p ++ ; append something to p --x..y p ; the range x to y without p ] ; end function, implicit return  # Zsh*, 33 bytes A=({$1..$2});<<<$@\ ;<<<\${A:1:-1}


Try it online!

(*) Using set -o err_return

# Thunno 2, 5 bytes

ẸIḣṫḥ


Attempt This Online!

#### Explanation

ẸIḣṫḥ  # Implicit input   ->  [8, 2]
Ẹ      # Dump onto stack  ->  8, 2
I     # Inclusive range  ->  [8,7,6,5,4,3,2]
ḣ    # Remove head      ->  [7,6,5,4,3,2]
ṫ   # Remove tail      ->  [7,6,5,4,3]
ḥ  # Concatenate      ->  [8,2,7,6,5,4,3]
# Implicit output


# Scala, 87 75 bytes

Golfed version. Try it online!

(a,b)=>a::b::(if(a>b)(a to b by-1).drop(1)else(a to b by 1).drop(1)).toList


Ungolfed version.

object Main {
def main(args: Array[String]): Unit = {
val f = (a: Int, b: Int) => {
a :: b :: (if (a > b) (a to b by -1).drop(1) else (a to b by 1).drop(1)).toList
}
println(f(0, 5)) // List(0, 5, 1, 2, 3, 4)
println(f(-3, 8)) // List(-3, 8, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7)
println(f(4, 4)) // List(4, 4)
println(f(4, 5)) // List(4, 5)
println(f(8, 2)) // List(8, 2, 7, 6, 5, 4, 3)
println(f(-2, -7)) // List(-2, -7, -3, -4, -5, -6)
}
}



# Vyxal, 4 bytes

ṡÞẇJ


Try it Online! (inputs are swapped)

ṡ    # Inclusive range
Þẇ  # Push [first, last] and rest
J # Concatenate