The first, the last, and everything between
up vote
29
down vote
favorite
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]
code-golf
|
show 6 more comments
up vote
29
down vote
favorite
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]
code-golf
I guess we can't take the inputs in pre-ordered order?
– Kevin Cruijssen
2 days ago
@KevinCruijssen, no, the output order depends on the input order
– TFeld
2 days ago
@StewieGriffin, the output order has to be the same as the input
– TFeld
2 days ago
Is this output format acceptable? Note the newline
– Luis Mendo
2 days ago
2
@KevinCruijssen Any reasonable I/O is acceptable.
– TFeld
2 days ago
|
show 6 more comments
up vote
29
down vote
favorite
up vote
29
down vote
favorite
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]
code-golf
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]
code-golf
code-golf
edited yesterday
asked 2 days ago
TFeld
13.3k21039
13.3k21039
I guess we can't take the inputs in pre-ordered order?
– Kevin Cruijssen
2 days ago
@KevinCruijssen, no, the output order depends on the input order
– TFeld
2 days ago
@StewieGriffin, the output order has to be the same as the input
– TFeld
2 days ago
Is this output format acceptable? Note the newline
– Luis Mendo
2 days ago
2
@KevinCruijssen Any reasonable I/O is acceptable.
– TFeld
2 days ago
|
show 6 more comments
I guess we can't take the inputs in pre-ordered order?
– Kevin Cruijssen
2 days ago
@KevinCruijssen, no, the output order depends on the input order
– TFeld
2 days ago
@StewieGriffin, the output order has to be the same as the input
– TFeld
2 days ago
Is this output format acceptable? Note the newline
– Luis Mendo
2 days ago
2
@KevinCruijssen Any reasonable I/O is acceptable.
– TFeld
2 days ago
I guess we can't take the inputs in pre-ordered order?
– Kevin Cruijssen
2 days ago
I guess we can't take the inputs in pre-ordered order?
– Kevin Cruijssen
2 days ago
@KevinCruijssen, no, the output order depends on the input order
– TFeld
2 days ago
@KevinCruijssen, no, the output order depends on the input order
– TFeld
2 days ago
@StewieGriffin, the output order has to be the same as the input
– TFeld
2 days ago
@StewieGriffin, the output order has to be the same as the input
– TFeld
2 days ago
Is this output format acceptable? Note the newline
– Luis Mendo
2 days ago
Is this output format acceptable? Note the newline
– Luis Mendo
2 days ago
2
2
@KevinCruijssen Any reasonable I/O is acceptable.
– TFeld
2 days ago
@KevinCruijssen Any reasonable I/O is acceptable.
– TFeld
2 days ago
|
show 6 more comments
49 Answers
49
active
oldest
votes
1 2
next
up vote
13
down vote
R, 39 33 30 bytes
c(a<-scan(),setdiff(a:a[2],a))
Try it online!
Thanks for saved bytes to user2390246 and J.Doe.
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
You can abuse the fact the:operator uses the first element of both args for 30 bytes
– J.Doe
2 days ago
add a comment |
up vote
11
down vote
05AB1E, 4 bytes
Ÿ¦¨«
Try it online!
Explanation
Ÿ # inclusive range [a ... b]
¦¨ # remove the first and last element
« # append to input
add a comment |
up vote
10
down vote
Python 3, 52 48 47 42 bytes
lambda a,b:[a,b,*range(a,b,(a<b)*2-1)[1:]]
Try it online!
Combined former implementations.
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You can remove the space ator-1to save a byte.
– Kevin Cruijssen
2 days ago
add a comment |
up vote
9
down vote
Python 2 (Cython), 36 35 bytes
lambda x:x+range(*x,-cmp(*x)|1)[1:]
Thanks to @nwellnhof for golfing off 1 byte!
Try it online!
Python 2, 37 bytes
lambda x:x+range(*x+[-cmp(*x)|1])[1:]
Thanks to @JonasAusevicius for the port to CPython!
Try it online!
2
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution
– Jonas Ausevicius
2 days ago
add a comment |
up vote
8
down vote
Perl 6, 26 22 bytes
{|@_,|[...^](@_).skip}
Try it online!
Explanation
{ }
|@_, # Slip args a,b into result
[...^](@_) # Reduce args a,b with ...^ operator, same as a...^b
.skip # Skip first element
| # Slip into result
add a comment |
up vote
7
down vote
Python 2, 40 bytes
lambda x,y:[x,y]+range(x,y,-(y<x)|1)[1:]
Try it online!
Really like-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?
– ElPedro
2 days ago
2
@ElPedro Basically,y<xchecks ifyis strictly less thanx, and returnsTrueif it is,Falseotherwise. After that, unary-is applied to it, which convertsTrueto-1andFalseto0. The last step is to bitwise OR this number with1. This obviously leaves1(0b1) unaffected, and also leaves-1(-0b1) unaffected (the sign bit of-1is set, so it's kept as such). However, it does convert0to1, so thatrangedoesn't complain about me using astepof0.
– Erik the Outgolfer
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
add a comment |
up vote
6
down vote
Python 3, 64 62 51 bytes
lambda a,b:[a,b]+[*range(a+1,b)]+[*range(a-1,b,-1)]
Try it online!
Python 2, 58 45 bytes
lambda a,b:[a,b]+range(a+1,b)+range(a-1,b,-1)
Try it online!
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Because an empty list is falsey, you can remove thea<=b andfrom both answers
– TFeld
2 days ago
You could also use+instead ofor
– TFeld
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
add a comment |
up vote
6
down vote
Python 2, 47 41 bytes
lambda a,b:[a,b]+range(a,b,(a<b)*2-1)[1:]
Try it online!
Here's mine, now that a lot of other Python answers have been posted
-6 bytes, thanks to G B
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
2
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
add a comment |
up vote
6
down vote
Japt, 8 bytes
cUr!õ kU
Try it here
:Implicit input of array U
c :Concatenate
Ur : Reduce U by
!õ : Inclusive range
kU : Remove all elements in original U
add a comment |
up vote
5
down vote
JavaScript (ES6), 51 bytes
Takes input as (a)(b).
a=>g=(b,c=b)=>(b+=b<a|-(b>a))-a?[...g(b,c),b]:[a,c]
Try it online!
Commented
a => // main function, taking a
g = ( // g = recursive function
b, // taking b
c = b // we save a backup of the original value of b into c
) => //
(b += // add to b:
b < a | // +1 if b is less than a
-(b > a) // -1 if b is greater than a
) // (or 0 if b = a)
- a ? // if the updated value of b is not equal to a:
[ // generate a new array:
...g(b, c), // prepend all values generated by a recursive call
b // append the current value of b
] //
: // else:
[a, c] // stop recursion and return the first 2 values: a and c
add a comment |
up vote
5
down vote
Java 10, 109 108 104 102 93 62 bytes
Using a space-delimited String:
b->a->{var r=a+" "+b;for(;a<b?++a<b:--a>b;)r+=" "+a;return r;}
Try it online.
Using a List:
b->a->{var r=new java.util.Stack();for(r.add(a),r.add(b);a<b?++a<b:--a>b;)r.add(a);return r;}
Try it online.
(a<b?++a<b:--a>b can be ++a<b||(a-=2)>b for the same byte-count: Try it online for the String or Try it online for the List.)
Old (109 108 104 102 101 bytes) answer using an array:
a->b->{int s=a<b?1:-1,i=a!=b?(b-a)*s+1:2,r=new int[i];for(r[0]=a,r[1]=b;i>2;)r[--i]=b-=s;return r;}
-7 bytes thanks to @nwellnhof.
Try it online.
Explanation:
a->b->{ // Method with 2 int parameters & int-array return-type
int s= // Step integer, starting at:
a<b?1 // 1 if the first input is smaller than the second
:-1; // -1 otherwise
i= // Array-index integer, starting at:
a!=b? // If the inputs aren't equal:
(b-a)*s+1 // Set it to the absolute difference + 1
: // Else:
2, // Set it to 2
r=new int[i]; // Result-array of that size
for(r[0]=a, // Fill the first value with the first input
r[1]=b; // And the second value with the second input
i>2;) // Loop `i` downwards in the range [`i`,2):
r[--i]= // Decrease `i` by 1 first with `--i`
// Set the `i`'th array-value to:
b-=s; // If the step integer is 1: decrease `b` by 1
// If the step integer is -1: increase `b` by 1
// And set the array-value to this modified `b`
return r;} // Return the result-array
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
@Οurous It's indeed too verbose:a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;}(130 bytes)
– Kevin Cruijssen
2 days ago
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
1
@Neyt Ah, fixed. My initial version with the array below didn't usevar, which is why I usually put those at 8, and the ones that does usevaras 10 (and the ones usingString.repeatas 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.
– Kevin Cruijssen
yesterday
add a comment |
up vote
4
down vote
Haskell, 34 bytes
a#b=a:b:[a+1..b-1]++[a-1,a-2..b+1]
Try it online!
This does not work. GHC interpretsb-1asb $ (-1). Useb- 1instead.
– Mark Neu
3 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
Oh, sorry! I hadNegativeLiteralson.
– Mark Neu
2 hours ago
add a comment |
up vote
4
down vote
Jelly, 4 bytes
,œ|r
Try it online!
How it works
,œ|r Main link. Left argument: a. Right argument: b
, Pair; yield [a, b].
r Range; yield [a, ..., b].
œ| Perform multiset union.
add a comment |
up vote
4
down vote
J, 26 bytes
,,[|.@]^:(>{.)<.+1}.i.@|@-
Try it online!
Explanation:
A dyadic verb (takes left and right argument)
- subtracts the arguments
|@ and finds the absolute value
i.@ and makes a list 0..absolute difference
1}. drops the fist element
+ adds to the entire list
<. the smaller of the arguments
|.@] reverses the list
^: only if
[ the left argument
(>{.) is greater than the first item of the list
, appends the list to
, the right argument appended to the left one
1
,,[:}.@}:<.+i.@-@(+*)@-for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum*). i feel like this could get down under 20 but i'm tired.
– Jonah
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
add a comment |
up vote
4
down vote
Octave, 45 bytes
@(a,b)[a b linspace(a,b,(t=abs(a-b))+1)(2:t)]
Try it online!
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
add a comment |
up vote
3
down vote
Batch, 107 bytes
@echo %1
@echo %2
@for %%s in (1 -1)do @for /l %%i in (%1,%%s,%2)do @if %1 neq %%i if %%i neq %2 echo %%i
Takes input as command-line arguments. Explanation:
@echo %1
@echo %2
Output the two integers.
@for %%s in (1 -1)do
Try both ascending and descending ranges.
@for /l %%i in (%1,%%s,%2)do
Loop over the inclusive range.
@if %1 neq %%i if %%i neq %2
Exclude the two integers.
echo %%i
Output the current value.
add a comment |
up vote
3
down vote
Pyth, 5 bytes
+QtrF
Input is a two-element list, [input 1, input 2]. Try it online here, or verify all the test cases at once here.
+QtrFQ Implicit: Q=eval(input())
Trailing Q inferred
rFQ Generate range [input 1 - input 2)
t Discard first element
+Q Prepend Q
UsingFinstead of.*on 2-element lists is a brilliant trick that I will absolutely be using from here on.
– hakr14
2 days ago
add a comment |
up vote
3
down vote
Red, 75 bytes
func[a b][s: sign? d: b - a prin[a b]loop absolute d - s[prin[""a: a + s]]]
Try it online!
add a comment |
up vote
3
down vote
Clean, 49 bytes
import StdEnv
@a b=init[a,b:tl[a,a+sign(b-a)..b]]
Try it online!
add a comment |
up vote
3
down vote
Python 2, 52 47 41 bytes
lambda i,j:[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-5 with thanks to @JoKing
-6 by slicing the first element from the range (idea stolen from and with credit to @TFeld)
Non-lambda version...
Python 2, 51 49 47 bytes
i,j=input();print[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-2 with thanks to @JoKing
add a comment |
up vote
3
down vote
APL (Dyalog Classic), 29 bytes
{⍺,⍵,(⌽⍣(⍺>⍵))(⍺⌊⍵)+¯1↓⍳|⍺-⍵}
Try it online!
A port of my J solution
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
add a comment |
up vote
3
down vote
J, 13 bytes
,,<.+i.@-~-.=
Try it online!
i.@-~ range [0 .. |difference|-1], reverse if the difference is positive
-.= remove the zero (either "=" is 0 or there’s nothing to remove)
<.+ to each element add the smaller of the args
,, prepend args
Nice solution! I totally forgot abouti.with negative argument.
– Galen Ivanov
2 days ago
this is gorgeous!
– Jonah
8 hours ago
add a comment |
up vote
3
down vote
TI-BASIC, 35 34 bytes
-1 byte from Misha Lavrov
Prompt A,B
Disp A,B
cos(π(A>B
For(I,A+Ans,B-Ans,Ans
Disp I
End
2
And one more byte by replacing1-2(A>Bwithcos(π(A>B.
– Misha Lavrov
2 days ago
@MishaLavrovseq(wouldn't work for inputs whereAandBare the same, unfortunately :(
– kamoroso94
2 days ago
True - also, I left out an argument ofseq(, so I'm no longer convinced it even is smaller. Still, thecos(trick should help.
– Misha Lavrov
2 days ago
add a comment |
up vote
2
down vote
Charcoal, 15 bytes
IE²NI…⊕θηI⮌…⊕ηθ
Try it online! Link is to verbose version of code. Explanation:
IE²N
Print the inputs on separate lines.
I…⊕θη
Print the ascending range, if any.
I⮌…⊕ηθ
Print the reverse ascending reverse range, if any.
add a comment |
up vote
2
down vote
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!
>= to >add a comment |
up vote
2
down vote
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!
add a comment |
up vote
2
down vote
Ruby, 33 40 bytes
->a,b{[a,b]+[*a..b,*a.downto(b)][1..-2]}
Try it online!
Temporary fix, trying to find a better idea
3
For[4,4]this gives only one[4]
– Kirill L.
2 days ago
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
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; }
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
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
– ouflak
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
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.
– ElPedro
2 days ago
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
|
show 1 more comment
1 2
next
49 Answers
49
active
oldest
votes
49 Answers
49
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
up vote
13
down vote
R, 39 33 30 bytes
c(a<-scan(),setdiff(a:a[2],a))
Try it online!
Thanks for saved bytes to user2390246 and J.Doe.
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
You can abuse the fact the:operator uses the first element of both args for 30 bytes
– J.Doe
2 days ago
add a comment |
up vote
13
down vote
R, 39 33 30 bytes
c(a<-scan(),setdiff(a:a[2],a))
Try it online!
Thanks for saved bytes to user2390246 and J.Doe.
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
You can abuse the fact the:operator uses the first element of both args for 30 bytes
– J.Doe
2 days ago
add a comment |
up vote
13
down vote
up vote
13
down vote
R, 39 33 30 bytes
c(a<-scan(),setdiff(a:a[2],a))
Try it online!
Thanks for saved bytes to user2390246 and J.Doe.
R, 39 33 30 bytes
c(a<-scan(),setdiff(a:a[2],a))
Try it online!
Thanks for saved bytes to user2390246 and J.Doe.
edited 2 days ago
answered 2 days ago
Kirill L.
3,1661118
3,1661118
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
You can abuse the fact the:operator uses the first element of both args for 30 bytes
– J.Doe
2 days ago
add a comment |
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
You can abuse the fact the:operator uses the first element of both args for 30 bytes
– J.Doe
2 days ago
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
You could save a few bytes by taking the input as a vector rather than as two separate integers.
– user2390246
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
Yeah, that's reasonable, and it actually then becomes even shorter as a full program rather than function.
– Kirill L.
2 days ago
You can abuse the fact the
: operator uses the first element of both args for 30 bytes– J.Doe
2 days ago
You can abuse the fact the
: operator uses the first element of both args for 30 bytes– J.Doe
2 days ago
add a comment |
up vote
11
down vote
05AB1E, 4 bytes
Ÿ¦¨«
Try it online!
Explanation
Ÿ # inclusive range [a ... b]
¦¨ # remove the first and last element
« # append to input
add a comment |
up vote
11
down vote
05AB1E, 4 bytes
Ÿ¦¨«
Try it online!
Explanation
Ÿ # inclusive range [a ... b]
¦¨ # remove the first and last element
« # append to input
add a comment |
up vote
11
down vote
up vote
11
down vote
05AB1E, 4 bytes
Ÿ¦¨«
Try it online!
Explanation
Ÿ # inclusive range [a ... b]
¦¨ # remove the first and last element
« # append to input
05AB1E, 4 bytes
Ÿ¦¨«
Try it online!
Explanation
Ÿ # inclusive range [a ... b]
¦¨ # remove the first and last element
« # append to input
answered 2 days ago
Emigna
44.6k432136
44.6k432136
add a comment |
add a comment |
up vote
10
down vote
Python 3, 52 48 47 42 bytes
lambda a,b:[a,b,*range(a,b,(a<b)*2-1)[1:]]
Try it online!
Combined former implementations.
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You can remove the space ator-1to save a byte.
– Kevin Cruijssen
2 days ago
add a comment |
up vote
10
down vote
Python 3, 52 48 47 42 bytes
lambda a,b:[a,b,*range(a,b,(a<b)*2-1)[1:]]
Try it online!
Combined former implementations.
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You can remove the space ator-1to save a byte.
– Kevin Cruijssen
2 days ago
add a comment |
up vote
10
down vote
up vote
10
down vote
Python 3, 52 48 47 42 bytes
lambda a,b:[a,b,*range(a,b,(a<b)*2-1)[1:]]
Try it online!
Combined former implementations.
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Python 3, 52 48 47 42 bytes
lambda a,b:[a,b,*range(a,b,(a<b)*2-1)[1:]]
Try it online!
Combined former implementations.
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
cobaltp
1416
1416
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
cobaltp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
You can remove the space ator-1to save a byte.
– Kevin Cruijssen
2 days ago
add a comment |
2
You can remove the space ator-1to save a byte.
– Kevin Cruijssen
2 days ago
2
2
You can remove the space at
or-1 to save a byte.– Kevin Cruijssen
2 days ago
You can remove the space at
or-1 to save a byte.– Kevin Cruijssen
2 days ago
add a comment |
up vote
9
down vote
Python 2 (Cython), 36 35 bytes
lambda x:x+range(*x,-cmp(*x)|1)[1:]
Thanks to @nwellnhof for golfing off 1 byte!
Try it online!
Python 2, 37 bytes
lambda x:x+range(*x+[-cmp(*x)|1])[1:]
Thanks to @JonasAusevicius for the port to CPython!
Try it online!
2
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution
– Jonas Ausevicius
2 days ago
add a comment |
up vote
9
down vote
Python 2 (Cython), 36 35 bytes
lambda x:x+range(*x,-cmp(*x)|1)[1:]
Thanks to @nwellnhof for golfing off 1 byte!
Try it online!
Python 2, 37 bytes
lambda x:x+range(*x+[-cmp(*x)|1])[1:]
Thanks to @JonasAusevicius for the port to CPython!
Try it online!
2
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution
– Jonas Ausevicius
2 days ago
add a comment |
up vote
9
down vote
up vote
9
down vote
Python 2 (Cython), 36 35 bytes
lambda x:x+range(*x,-cmp(*x)|1)[1:]
Thanks to @nwellnhof for golfing off 1 byte!
Try it online!
Python 2, 37 bytes
lambda x:x+range(*x+[-cmp(*x)|1])[1:]
Thanks to @JonasAusevicius for the port to CPython!
Try it online!
Python 2 (Cython), 36 35 bytes
lambda x:x+range(*x,-cmp(*x)|1)[1:]
Thanks to @nwellnhof for golfing off 1 byte!
Try it online!
Python 2, 37 bytes
lambda x:x+range(*x+[-cmp(*x)|1])[1:]
Thanks to @JonasAusevicius for the port to CPython!
Try it online!
edited 2 days ago
answered 2 days ago
Dennis♦
183k32293727
183k32293727
2
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution
– Jonas Ausevicius
2 days ago
add a comment |
2
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution
– Jonas Ausevicius
2 days ago
2
2
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:
lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution– Jonas Ausevicius
2 days ago
This can be applied to standard Python 2 at 37 bytes, making it the shortest answer yet:
lambda x:x+range(*x+[-cmp(*x)|1])[1:]. Nice solution– Jonas Ausevicius
2 days ago
add a comment |
up vote
8
down vote
Perl 6, 26 22 bytes
{|@_,|[...^](@_).skip}
Try it online!
Explanation
{ }
|@_, # Slip args a,b into result
[...^](@_) # Reduce args a,b with ...^ operator, same as a...^b
.skip # Skip first element
| # Slip into result
add a comment |
up vote
8
down vote
Perl 6, 26 22 bytes
{|@_,|[...^](@_).skip}
Try it online!
Explanation
{ }
|@_, # Slip args a,b into result
[...^](@_) # Reduce args a,b with ...^ operator, same as a...^b
.skip # Skip first element
| # Slip into result
add a comment |
up vote
8
down vote
up vote
8
down vote
Perl 6, 26 22 bytes
{|@_,|[...^](@_).skip}
Try it online!
Explanation
{ }
|@_, # Slip args a,b into result
[...^](@_) # Reduce args a,b with ...^ operator, same as a...^b
.skip # Skip first element
| # Slip into result
Perl 6, 26 22 bytes
{|@_,|[...^](@_).skip}
Try it online!
Explanation
{ }
|@_, # Slip args a,b into result
[...^](@_) # Reduce args a,b with ...^ operator, same as a...^b
.skip # Skip first element
| # Slip into result
edited 2 days ago
answered 2 days ago
nwellnhof
5,568921
5,568921
add a comment |
add a comment |
up vote
7
down vote
Python 2, 40 bytes
lambda x,y:[x,y]+range(x,y,-(y<x)|1)[1:]
Try it online!
Really like-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?
– ElPedro
2 days ago
2
@ElPedro Basically,y<xchecks ifyis strictly less thanx, and returnsTrueif it is,Falseotherwise. After that, unary-is applied to it, which convertsTrueto-1andFalseto0. The last step is to bitwise OR this number with1. This obviously leaves1(0b1) unaffected, and also leaves-1(-0b1) unaffected (the sign bit of-1is set, so it's kept as such). However, it does convert0to1, so thatrangedoesn't complain about me using astepof0.
– Erik the Outgolfer
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
add a comment |
up vote
7
down vote
Python 2, 40 bytes
lambda x,y:[x,y]+range(x,y,-(y<x)|1)[1:]
Try it online!
Really like-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?
– ElPedro
2 days ago
2
@ElPedro Basically,y<xchecks ifyis strictly less thanx, and returnsTrueif it is,Falseotherwise. After that, unary-is applied to it, which convertsTrueto-1andFalseto0. The last step is to bitwise OR this number with1. This obviously leaves1(0b1) unaffected, and also leaves-1(-0b1) unaffected (the sign bit of-1is set, so it's kept as such). However, it does convert0to1, so thatrangedoesn't complain about me using astepof0.
– Erik the Outgolfer
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
add a comment |
up vote
7
down vote
up vote
7
down vote
Python 2, 40 bytes
lambda x,y:[x,y]+range(x,y,-(y<x)|1)[1:]
Try it online!
Python 2, 40 bytes
lambda x,y:[x,y]+range(x,y,-(y<x)|1)[1:]
Try it online!
answered 2 days ago
Erik the Outgolfer
30.2k428100
30.2k428100
Really like-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?
– ElPedro
2 days ago
2
@ElPedro Basically,y<xchecks ifyis strictly less thanx, and returnsTrueif it is,Falseotherwise. After that, unary-is applied to it, which convertsTrueto-1andFalseto0. The last step is to bitwise OR this number with1. This obviously leaves1(0b1) unaffected, and also leaves-1(-0b1) unaffected (the sign bit of-1is set, so it's kept as such). However, it does convert0to1, so thatrangedoesn't complain about me using astepof0.
– Erik the Outgolfer
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
add a comment |
Really like-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?
– ElPedro
2 days ago
2
@ElPedro Basically,y<xchecks ifyis strictly less thanx, and returnsTrueif it is,Falseotherwise. After that, unary-is applied to it, which convertsTrueto-1andFalseto0. The last step is to bitwise OR this number with1. This obviously leaves1(0b1) unaffected, and also leaves-1(-0b1) unaffected (the sign bit of-1is set, so it's kept as such). However, it does convert0to1, so thatrangedoesn't complain about me using astepof0.
– Erik the Outgolfer
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
Really like
-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?– ElPedro
2 days ago
Really like
-(y<x)|1. very cool but I can't work out why it works! Any chance you can explain it?– ElPedro
2 days ago
2
2
@ElPedro Basically,
y<x checks if y is strictly less than x, and returns True if it is, False otherwise. After that, unary - is applied to it, which converts True to -1 and False to 0. The last step is to bitwise OR this number with 1. This obviously leaves 1 (0b1) unaffected, and also leaves -1 (-0b1) unaffected (the sign bit of -1 is set, so it's kept as such). However, it does convert 0 to 1, so that range doesn't complain about me using a step of 0.– Erik the Outgolfer
2 days ago
@ElPedro Basically,
y<x checks if y is strictly less than x, and returns True if it is, False otherwise. After that, unary - is applied to it, which converts True to -1 and False to 0. The last step is to bitwise OR this number with 1. This obviously leaves 1 (0b1) unaffected, and also leaves -1 (-0b1) unaffected (the sign bit of -1 is set, so it's kept as such). However, it does convert 0 to 1, so that range doesn't complain about me using a step of 0.– Erik the Outgolfer
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
That is seriously cool and very clever. If I could upvote twice I would. Many thanks for the explanation.
– ElPedro
2 days ago
add a comment |
up vote
6
down vote
Python 3, 64 62 51 bytes
lambda a,b:[a,b]+[*range(a+1,b)]+[*range(a-1,b,-1)]
Try it online!
Python 2, 58 45 bytes
lambda a,b:[a,b]+range(a+1,b)+range(a-1,b,-1)
Try it online!
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Because an empty list is falsey, you can remove thea<=b andfrom both answers
– TFeld
2 days ago
You could also use+instead ofor
– TFeld
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
add a comment |
up vote
6
down vote
Python 3, 64 62 51 bytes
lambda a,b:[a,b]+[*range(a+1,b)]+[*range(a-1,b,-1)]
Try it online!
Python 2, 58 45 bytes
lambda a,b:[a,b]+range(a+1,b)+range(a-1,b,-1)
Try it online!
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Because an empty list is falsey, you can remove thea<=b andfrom both answers
– TFeld
2 days ago
You could also use+instead ofor
– TFeld
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
add a comment |
up vote
6
down vote
up vote
6
down vote
Python 3, 64 62 51 bytes
lambda a,b:[a,b]+[*range(a+1,b)]+[*range(a-1,b,-1)]
Try it online!
Python 2, 58 45 bytes
lambda a,b:[a,b]+range(a+1,b)+range(a-1,b,-1)
Try it online!
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Python 3, 64 62 51 bytes
lambda a,b:[a,b]+[*range(a+1,b)]+[*range(a-1,b,-1)]
Try it online!
Python 2, 58 45 bytes
lambda a,b:[a,b]+range(a+1,b)+range(a-1,b,-1)
Try it online!
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
Jonas Ausevicius
613
613
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Jonas Ausevicius is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Because an empty list is falsey, you can remove thea<=b andfrom both answers
– TFeld
2 days ago
You could also use+instead ofor
– TFeld
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
add a comment |
2
Because an empty list is falsey, you can remove thea<=b andfrom both answers
– TFeld
2 days ago
You could also use+instead ofor
– TFeld
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
2
2
Because an empty list is falsey, you can remove the
a<=b and from both answers– TFeld
2 days ago
Because an empty list is falsey, you can remove the
a<=b and from both answers– TFeld
2 days ago
You could also use
+ instead of or – TFeld
2 days ago
You could also use
+ instead of or – TFeld
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
@TFeld thank you
– Jonas Ausevicius
2 days ago
add a comment |
up vote
6
down vote
Python 2, 47 41 bytes
lambda a,b:[a,b]+range(a,b,(a<b)*2-1)[1:]
Try it online!
Here's mine, now that a lot of other Python answers have been posted
-6 bytes, thanks to G B
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
2
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
add a comment |
up vote
6
down vote
Python 2, 47 41 bytes
lambda a,b:[a,b]+range(a,b,(a<b)*2-1)[1:]
Try it online!
Here's mine, now that a lot of other Python answers have been posted
-6 bytes, thanks to G B
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
2
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
add a comment |
up vote
6
down vote
up vote
6
down vote
Python 2, 47 41 bytes
lambda a,b:[a,b]+range(a,b,(a<b)*2-1)[1:]
Try it online!
Here's mine, now that a lot of other Python answers have been posted
-6 bytes, thanks to G B
Python 2, 47 41 bytes
lambda a,b:[a,b]+range(a,b,(a<b)*2-1)[1:]
Try it online!
Here's mine, now that a lot of other Python answers have been posted
-6 bytes, thanks to G B
edited 2 days ago
answered 2 days ago
TFeld
13.3k21039
13.3k21039
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
2
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
add a comment |
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
2
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
Taking advantage of the empty range when it's invalid is a smart way to deal with forward or backwards lists. I could see that being very useful and is a nice trick to know exists.
– akozi
2 days ago
2
2
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
41 bytes using a single range: range(a,b,(a<b)*2-1)
– G B
2 days ago
add a comment |
up vote
6
down vote
Japt, 8 bytes
cUr!õ kU
Try it here
:Implicit input of array U
c :Concatenate
Ur : Reduce U by
!õ : Inclusive range
kU : Remove all elements in original U
add a comment |
up vote
6
down vote
Japt, 8 bytes
cUr!õ kU
Try it here
:Implicit input of array U
c :Concatenate
Ur : Reduce U by
!õ : Inclusive range
kU : Remove all elements in original U
add a comment |
up vote
6
down vote
up vote
6
down vote
Japt, 8 bytes
cUr!õ kU
Try it here
:Implicit input of array U
c :Concatenate
Ur : Reduce U by
!õ : Inclusive range
kU : Remove all elements in original U
Japt, 8 bytes
cUr!õ kU
Try it here
:Implicit input of array U
c :Concatenate
Ur : Reduce U by
!õ : Inclusive range
kU : Remove all elements in original U
edited 2 days ago
answered 2 days ago
Shaggy
17.8k21663
17.8k21663
add a comment |
add a comment |
up vote
5
down vote
JavaScript (ES6), 51 bytes
Takes input as (a)(b).
a=>g=(b,c=b)=>(b+=b<a|-(b>a))-a?[...g(b,c),b]:[a,c]
Try it online!
Commented
a => // main function, taking a
g = ( // g = recursive function
b, // taking b
c = b // we save a backup of the original value of b into c
) => //
(b += // add to b:
b < a | // +1 if b is less than a
-(b > a) // -1 if b is greater than a
) // (or 0 if b = a)
- a ? // if the updated value of b is not equal to a:
[ // generate a new array:
...g(b, c), // prepend all values generated by a recursive call
b // append the current value of b
] //
: // else:
[a, c] // stop recursion and return the first 2 values: a and c
add a comment |
up vote
5
down vote
JavaScript (ES6), 51 bytes
Takes input as (a)(b).
a=>g=(b,c=b)=>(b+=b<a|-(b>a))-a?[...g(b,c),b]:[a,c]
Try it online!
Commented
a => // main function, taking a
g = ( // g = recursive function
b, // taking b
c = b // we save a backup of the original value of b into c
) => //
(b += // add to b:
b < a | // +1 if b is less than a
-(b > a) // -1 if b is greater than a
) // (or 0 if b = a)
- a ? // if the updated value of b is not equal to a:
[ // generate a new array:
...g(b, c), // prepend all values generated by a recursive call
b // append the current value of b
] //
: // else:
[a, c] // stop recursion and return the first 2 values: a and c
add a comment |
up vote
5
down vote
up vote
5
down vote
JavaScript (ES6), 51 bytes
Takes input as (a)(b).
a=>g=(b,c=b)=>(b+=b<a|-(b>a))-a?[...g(b,c),b]:[a,c]
Try it online!
Commented
a => // main function, taking a
g = ( // g = recursive function
b, // taking b
c = b // we save a backup of the original value of b into c
) => //
(b += // add to b:
b < a | // +1 if b is less than a
-(b > a) // -1 if b is greater than a
) // (or 0 if b = a)
- a ? // if the updated value of b is not equal to a:
[ // generate a new array:
...g(b, c), // prepend all values generated by a recursive call
b // append the current value of b
] //
: // else:
[a, c] // stop recursion and return the first 2 values: a and c
JavaScript (ES6), 51 bytes
Takes input as (a)(b).
a=>g=(b,c=b)=>(b+=b<a|-(b>a))-a?[...g(b,c),b]:[a,c]
Try it online!
Commented
a => // main function, taking a
g = ( // g = recursive function
b, // taking b
c = b // we save a backup of the original value of b into c
) => //
(b += // add to b:
b < a | // +1 if b is less than a
-(b > a) // -1 if b is greater than a
) // (or 0 if b = a)
- a ? // if the updated value of b is not equal to a:
[ // generate a new array:
...g(b, c), // prepend all values generated by a recursive call
b // append the current value of b
] //
: // else:
[a, c] // stop recursion and return the first 2 values: a and c
edited yesterday
answered 2 days ago
Arnauld
68k584288
68k584288
add a comment |
add a comment |
up vote
5
down vote
Java 10, 109 108 104 102 93 62 bytes
Using a space-delimited String:
b->a->{var r=a+" "+b;for(;a<b?++a<b:--a>b;)r+=" "+a;return r;}
Try it online.
Using a List:
b->a->{var r=new java.util.Stack();for(r.add(a),r.add(b);a<b?++a<b:--a>b;)r.add(a);return r;}
Try it online.
(a<b?++a<b:--a>b can be ++a<b||(a-=2)>b for the same byte-count: Try it online for the String or Try it online for the List.)
Old (109 108 104 102 101 bytes) answer using an array:
a->b->{int s=a<b?1:-1,i=a!=b?(b-a)*s+1:2,r=new int[i];for(r[0]=a,r[1]=b;i>2;)r[--i]=b-=s;return r;}
-7 bytes thanks to @nwellnhof.
Try it online.
Explanation:
a->b->{ // Method with 2 int parameters & int-array return-type
int s= // Step integer, starting at:
a<b?1 // 1 if the first input is smaller than the second
:-1; // -1 otherwise
i= // Array-index integer, starting at:
a!=b? // If the inputs aren't equal:
(b-a)*s+1 // Set it to the absolute difference + 1
: // Else:
2, // Set it to 2
r=new int[i]; // Result-array of that size
for(r[0]=a, // Fill the first value with the first input
r[1]=b; // And the second value with the second input
i>2;) // Loop `i` downwards in the range [`i`,2):
r[--i]= // Decrease `i` by 1 first with `--i`
// Set the `i`'th array-value to:
b-=s; // If the step integer is 1: decrease `b` by 1
// If the step integer is -1: increase `b` by 1
// And set the array-value to this modified `b`
return r;} // Return the result-array
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
@Οurous It's indeed too verbose:a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;}(130 bytes)
– Kevin Cruijssen
2 days ago
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
1
@Neyt Ah, fixed. My initial version with the array below didn't usevar, which is why I usually put those at 8, and the ones that does usevaras 10 (and the ones usingString.repeatas 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.
– Kevin Cruijssen
yesterday
add a comment |
up vote
5
down vote
Java 10, 109 108 104 102 93 62 bytes
Using a space-delimited String:
b->a->{var r=a+" "+b;for(;a<b?++a<b:--a>b;)r+=" "+a;return r;}
Try it online.
Using a List:
b->a->{var r=new java.util.Stack();for(r.add(a),r.add(b);a<b?++a<b:--a>b;)r.add(a);return r;}
Try it online.
(a<b?++a<b:--a>b can be ++a<b||(a-=2)>b for the same byte-count: Try it online for the String or Try it online for the List.)
Old (109 108 104 102 101 bytes) answer using an array:
a->b->{int s=a<b?1:-1,i=a!=b?(b-a)*s+1:2,r=new int[i];for(r[0]=a,r[1]=b;i>2;)r[--i]=b-=s;return r;}
-7 bytes thanks to @nwellnhof.
Try it online.
Explanation:
a->b->{ // Method with 2 int parameters & int-array return-type
int s= // Step integer, starting at:
a<b?1 // 1 if the first input is smaller than the second
:-1; // -1 otherwise
i= // Array-index integer, starting at:
a!=b? // If the inputs aren't equal:
(b-a)*s+1 // Set it to the absolute difference + 1
: // Else:
2, // Set it to 2
r=new int[i]; // Result-array of that size
for(r[0]=a, // Fill the first value with the first input
r[1]=b; // And the second value with the second input
i>2;) // Loop `i` downwards in the range [`i`,2):
r[--i]= // Decrease `i` by 1 first with `--i`
// Set the `i`'th array-value to:
b-=s; // If the step integer is 1: decrease `b` by 1
// If the step integer is -1: increase `b` by 1
// And set the array-value to this modified `b`
return r;} // Return the result-array
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
@Οurous It's indeed too verbose:a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;}(130 bytes)
– Kevin Cruijssen
2 days ago
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
1
@Neyt Ah, fixed. My initial version with the array below didn't usevar, which is why I usually put those at 8, and the ones that does usevaras 10 (and the ones usingString.repeatas 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.
– Kevin Cruijssen
yesterday
add a comment |
up vote
5
down vote
up vote
5
down vote
Java 10, 109 108 104 102 93 62 bytes
Using a space-delimited String:
b->a->{var r=a+" "+b;for(;a<b?++a<b:--a>b;)r+=" "+a;return r;}
Try it online.
Using a List:
b->a->{var r=new java.util.Stack();for(r.add(a),r.add(b);a<b?++a<b:--a>b;)r.add(a);return r;}
Try it online.
(a<b?++a<b:--a>b can be ++a<b||(a-=2)>b for the same byte-count: Try it online for the String or Try it online for the List.)
Old (109 108 104 102 101 bytes) answer using an array:
a->b->{int s=a<b?1:-1,i=a!=b?(b-a)*s+1:2,r=new int[i];for(r[0]=a,r[1]=b;i>2;)r[--i]=b-=s;return r;}
-7 bytes thanks to @nwellnhof.
Try it online.
Explanation:
a->b->{ // Method with 2 int parameters & int-array return-type
int s= // Step integer, starting at:
a<b?1 // 1 if the first input is smaller than the second
:-1; // -1 otherwise
i= // Array-index integer, starting at:
a!=b? // If the inputs aren't equal:
(b-a)*s+1 // Set it to the absolute difference + 1
: // Else:
2, // Set it to 2
r=new int[i]; // Result-array of that size
for(r[0]=a, // Fill the first value with the first input
r[1]=b; // And the second value with the second input
i>2;) // Loop `i` downwards in the range [`i`,2):
r[--i]= // Decrease `i` by 1 first with `--i`
// Set the `i`'th array-value to:
b-=s; // If the step integer is 1: decrease `b` by 1
// If the step integer is -1: increase `b` by 1
// And set the array-value to this modified `b`
return r;} // Return the result-array
Java 10, 109 108 104 102 93 62 bytes
Using a space-delimited String:
b->a->{var r=a+" "+b;for(;a<b?++a<b:--a>b;)r+=" "+a;return r;}
Try it online.
Using a List:
b->a->{var r=new java.util.Stack();for(r.add(a),r.add(b);a<b?++a<b:--a>b;)r.add(a);return r;}
Try it online.
(a<b?++a<b:--a>b can be ++a<b||(a-=2)>b for the same byte-count: Try it online for the String or Try it online for the List.)
Old (109 108 104 102 101 bytes) answer using an array:
a->b->{int s=a<b?1:-1,i=a!=b?(b-a)*s+1:2,r=new int[i];for(r[0]=a,r[1]=b;i>2;)r[--i]=b-=s;return r;}
-7 bytes thanks to @nwellnhof.
Try it online.
Explanation:
a->b->{ // Method with 2 int parameters & int-array return-type
int s= // Step integer, starting at:
a<b?1 // 1 if the first input is smaller than the second
:-1; // -1 otherwise
i= // Array-index integer, starting at:
a!=b? // If the inputs aren't equal:
(b-a)*s+1 // Set it to the absolute difference + 1
: // Else:
2, // Set it to 2
r=new int[i]; // Result-array of that size
for(r[0]=a, // Fill the first value with the first input
r[1]=b; // And the second value with the second input
i>2;) // Loop `i` downwards in the range [`i`,2):
r[--i]= // Decrease `i` by 1 first with `--i`
// Set the `i`'th array-value to:
b-=s; // If the step integer is 1: decrease `b` by 1
// If the step integer is -1: increase `b` by 1
// And set the array-value to this modified `b`
return r;} // Return the result-array
edited yesterday
answered 2 days ago
Kevin Cruijssen
33.2k554177
33.2k554177
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
@Οurous It's indeed too verbose:a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;}(130 bytes)
– Kevin Cruijssen
2 days ago
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
1
@Neyt Ah, fixed. My initial version with the array below didn't usevar, which is why I usually put those at 8, and the ones that does usevaras 10 (and the ones usingString.repeatas 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.
– Kevin Cruijssen
yesterday
add a comment |
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
@Οurous It's indeed too verbose:a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;}(130 bytes)
– Kevin Cruijssen
2 days ago
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
1
@Neyt Ah, fixed. My initial version with the array below didn't usevar, which is why I usually put those at 8, and the ones that does usevaras 10 (and the ones usingString.repeatas 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.
– Kevin Cruijssen
yesterday
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
Isn't there anything in Java's standard library for making ranges of integers? Or it is just too verbose to use?
– Οurous
2 days ago
@Οurous It's indeed too verbose:
a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;} (130 bytes)– Kevin Cruijssen
2 days ago
@Οurous It's indeed too verbose:
a->b->{var L=java.util.stream.IntStream.range(a,b).boxed().collect(java.util.Collectors.toList());L.add(0,b);L.add(0,a);return L;} (130 bytes)– Kevin Cruijssen
2 days ago
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
Is it Java 8 or Java 10 ? Because of "var" ^^'
– Neyt
yesterday
1
1
@Neyt Ah, fixed. My initial version with the array below didn't use
var, which is why I usually put those at 8, and the ones that does use var as 10 (and the ones using String.repeat as 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.– Kevin Cruijssen
yesterday
@Neyt Ah, fixed. My initial version with the array below didn't use
var, which is why I usually put those at 8, and the ones that does use var as 10 (and the ones using String.repeat as 11). :) Forgot to update it after adding the List and String answers, should be corrected now. Thanks.– Kevin Cruijssen
yesterday
add a comment |
up vote
4
down vote
Haskell, 34 bytes
a#b=a:b:[a+1..b-1]++[a-1,a-2..b+1]
Try it online!
This does not work. GHC interpretsb-1asb $ (-1). Useb- 1instead.
– Mark Neu
3 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
Oh, sorry! I hadNegativeLiteralson.
– Mark Neu
2 hours ago
add a comment |
up vote
4
down vote
Haskell, 34 bytes
a#b=a:b:[a+1..b-1]++[a-1,a-2..b+1]
Try it online!
This does not work. GHC interpretsb-1asb $ (-1). Useb- 1instead.
– Mark Neu
3 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
Oh, sorry! I hadNegativeLiteralson.
– Mark Neu
2 hours ago
add a comment |
up vote
4
down vote
up vote
4
down vote
Haskell, 34 bytes
a#b=a:b:[a+1..b-1]++[a-1,a-2..b+1]
Try it online!
Haskell, 34 bytes
a#b=a:b:[a+1..b-1]++[a-1,a-2..b+1]
Try it online!
answered 2 days ago
nimi
30.5k31985
30.5k31985
This does not work. GHC interpretsb-1asb $ (-1). Useb- 1instead.
– Mark Neu
3 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
Oh, sorry! I hadNegativeLiteralson.
– Mark Neu
2 hours ago
add a comment |
This does not work. GHC interpretsb-1asb $ (-1). Useb- 1instead.
– Mark Neu
3 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
Oh, sorry! I hadNegativeLiteralson.
– Mark Neu
2 hours ago
This does not work. GHC interprets
b-1 as b $ (-1). Use b- 1 instead.– Mark Neu
3 hours ago
This does not work. GHC interprets
b-1 as b $ (-1). Use b- 1 instead.– Mark Neu
3 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
@MarkNeu: it does work. See TIO link.
– nimi
2 hours ago
Oh, sorry! I had
NegativeLiterals on.– Mark Neu
2 hours ago
Oh, sorry! I had
NegativeLiterals on.– Mark Neu
2 hours ago
add a comment |
up vote
4
down vote
Jelly, 4 bytes
,œ|r
Try it online!
How it works
,œ|r Main link. Left argument: a. Right argument: b
, Pair; yield [a, b].
r Range; yield [a, ..., b].
œ| Perform multiset union.
add a comment |
up vote
4
down vote
Jelly, 4 bytes
,œ|r
Try it online!
How it works
,œ|r Main link. Left argument: a. Right argument: b
, Pair; yield [a, b].
r Range; yield [a, ..., b].
œ| Perform multiset union.
add a comment |
up vote
4
down vote
up vote
4
down vote
Jelly, 4 bytes
,œ|r
Try it online!
How it works
,œ|r Main link. Left argument: a. Right argument: b
, Pair; yield [a, b].
r Range; yield [a, ..., b].
œ| Perform multiset union.
Jelly, 4 bytes
,œ|r
Try it online!
How it works
,œ|r Main link. Left argument: a. Right argument: b
, Pair; yield [a, b].
r Range; yield [a, ..., b].
œ| Perform multiset union.
answered 2 days ago
Dennis♦
183k32293727
183k32293727
add a comment |
add a comment |
up vote
4
down vote
J, 26 bytes
,,[|.@]^:(>{.)<.+1}.i.@|@-
Try it online!
Explanation:
A dyadic verb (takes left and right argument)
- subtracts the arguments
|@ and finds the absolute value
i.@ and makes a list 0..absolute difference
1}. drops the fist element
+ adds to the entire list
<. the smaller of the arguments
|.@] reverses the list
^: only if
[ the left argument
(>{.) is greater than the first item of the list
, appends the list to
, the right argument appended to the left one
1
,,[:}.@}:<.+i.@-@(+*)@-for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum*). i feel like this could get down under 20 but i'm tired.
– Jonah
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
add a comment |
up vote
4
down vote
J, 26 bytes
,,[|.@]^:(>{.)<.+1}.i.@|@-
Try it online!
Explanation:
A dyadic verb (takes left and right argument)
- subtracts the arguments
|@ and finds the absolute value
i.@ and makes a list 0..absolute difference
1}. drops the fist element
+ adds to the entire list
<. the smaller of the arguments
|.@] reverses the list
^: only if
[ the left argument
(>{.) is greater than the first item of the list
, appends the list to
, the right argument appended to the left one
1
,,[:}.@}:<.+i.@-@(+*)@-for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum*). i feel like this could get down under 20 but i'm tired.
– Jonah
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
add a comment |
up vote
4
down vote
up vote
4
down vote
J, 26 bytes
,,[|.@]^:(>{.)<.+1}.i.@|@-
Try it online!
Explanation:
A dyadic verb (takes left and right argument)
- subtracts the arguments
|@ and finds the absolute value
i.@ and makes a list 0..absolute difference
1}. drops the fist element
+ adds to the entire list
<. the smaller of the arguments
|.@] reverses the list
^: only if
[ the left argument
(>{.) is greater than the first item of the list
, appends the list to
, the right argument appended to the left one
J, 26 bytes
,,[|.@]^:(>{.)<.+1}.i.@|@-
Try it online!
Explanation:
A dyadic verb (takes left and right argument)
- subtracts the arguments
|@ and finds the absolute value
i.@ and makes a list 0..absolute difference
1}. drops the fist element
+ adds to the entire list
<. the smaller of the arguments
|.@] reverses the list
^: only if
[ the left argument
(>{.) is greater than the first item of the list
, appends the list to
, the right argument appended to the left one
edited 2 days ago
answered 2 days ago
Galen Ivanov
5,62211031
5,62211031
1
,,[:}.@}:<.+i.@-@(+*)@-for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum*). i feel like this could get down under 20 but i'm tired.
– Jonah
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
add a comment |
1
,,[:}.@}:<.+i.@-@(+*)@-for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum*). i feel like this could get down under 20 but i'm tired.
– Jonah
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
1
1
,,[:}.@}:<.+i.@-@(+*)@- for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum *). i feel like this could get down under 20 but i'm tired.– Jonah
16 hours ago
,,[:}.@}:<.+i.@-@(+*)@- for 23 bytes and no special casing on relative argument ordering (rather: it's hidden inside the signum *). i feel like this could get down under 20 but i'm tired.– Jonah
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
@Jonah Thank you! Btw FrownyFrog's solution is way better than mine, so I 'm not going to golf it any further.
– Galen Ivanov
16 hours ago
add a comment |
up vote
4
down vote
Octave, 45 bytes
@(a,b)[a b linspace(a,b,(t=abs(a-b))+1)(2:t)]
Try it online!
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
add a comment |
up vote
4
down vote
Octave, 45 bytes
@(a,b)[a b linspace(a,b,(t=abs(a-b))+1)(2:t)]
Try it online!
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
add a comment |
up vote
4
down vote
up vote
4
down vote
Octave, 45 bytes
@(a,b)[a b linspace(a,b,(t=abs(a-b))+1)(2:t)]
Try it online!
Octave, 45 bytes
@(a,b)[a b linspace(a,b,(t=abs(a-b))+1)(2:t)]
Try it online!
edited 2 days ago
answered 2 days ago
Luis Mendo
73.5k885287
73.5k885287
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
add a comment |
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
IF the first is larger than the second, the range must be descending
– TFeld
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
Oh man, I can't read
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
I ended up changing the language
– Luis Mendo
2 days ago
add a comment |
up vote
3
down vote
Batch, 107 bytes
@echo %1
@echo %2
@for %%s in (1 -1)do @for /l %%i in (%1,%%s,%2)do @if %1 neq %%i if %%i neq %2 echo %%i
Takes input as command-line arguments. Explanation:
@echo %1
@echo %2
Output the two integers.
@for %%s in (1 -1)do
Try both ascending and descending ranges.
@for /l %%i in (%1,%%s,%2)do
Loop over the inclusive range.
@if %1 neq %%i if %%i neq %2
Exclude the two integers.
echo %%i
Output the current value.
add a comment |
up vote
3
down vote
Batch, 107 bytes
@echo %1
@echo %2
@for %%s in (1 -1)do @for /l %%i in (%1,%%s,%2)do @if %1 neq %%i if %%i neq %2 echo %%i
Takes input as command-line arguments. Explanation:
@echo %1
@echo %2
Output the two integers.
@for %%s in (1 -1)do
Try both ascending and descending ranges.
@for /l %%i in (%1,%%s,%2)do
Loop over the inclusive range.
@if %1 neq %%i if %%i neq %2
Exclude the two integers.
echo %%i
Output the current value.
add a comment |
up vote
3
down vote
up vote
3
down vote
Batch, 107 bytes
@echo %1
@echo %2
@for %%s in (1 -1)do @for /l %%i in (%1,%%s,%2)do @if %1 neq %%i if %%i neq %2 echo %%i
Takes input as command-line arguments. Explanation:
@echo %1
@echo %2
Output the two integers.
@for %%s in (1 -1)do
Try both ascending and descending ranges.
@for /l %%i in (%1,%%s,%2)do
Loop over the inclusive range.
@if %1 neq %%i if %%i neq %2
Exclude the two integers.
echo %%i
Output the current value.
Batch, 107 bytes
@echo %1
@echo %2
@for %%s in (1 -1)do @for /l %%i in (%1,%%s,%2)do @if %1 neq %%i if %%i neq %2 echo %%i
Takes input as command-line arguments. Explanation:
@echo %1
@echo %2
Output the two integers.
@for %%s in (1 -1)do
Try both ascending and descending ranges.
@for /l %%i in (%1,%%s,%2)do
Loop over the inclusive range.
@if %1 neq %%i if %%i neq %2
Exclude the two integers.
echo %%i
Output the current value.
answered 2 days ago
Neil
77.5k744174
77.5k744174
add a comment |
add a comment |
up vote
3
down vote
Pyth, 5 bytes
+QtrF
Input is a two-element list, [input 1, input 2]. Try it online here, or verify all the test cases at once here.
+QtrFQ Implicit: Q=eval(input())
Trailing Q inferred
rFQ Generate range [input 1 - input 2)
t Discard first element
+Q Prepend Q
UsingFinstead of.*on 2-element lists is a brilliant trick that I will absolutely be using from here on.
– hakr14
2 days ago
add a comment |
up vote
3
down vote
Pyth, 5 bytes
+QtrF
Input is a two-element list, [input 1, input 2]. Try it online here, or verify all the test cases at once here.
+QtrFQ Implicit: Q=eval(input())
Trailing Q inferred
rFQ Generate range [input 1 - input 2)
t Discard first element
+Q Prepend Q
UsingFinstead of.*on 2-element lists is a brilliant trick that I will absolutely be using from here on.
– hakr14
2 days ago
add a comment |
up vote
3
down vote
up vote
3
down vote
Pyth, 5 bytes
+QtrF
Input is a two-element list, [input 1, input 2]. Try it online here, or verify all the test cases at once here.
+QtrFQ Implicit: Q=eval(input())
Trailing Q inferred
rFQ Generate range [input 1 - input 2)
t Discard first element
+Q Prepend Q
Pyth, 5 bytes
+QtrF
Input is a two-element list, [input 1, input 2]. Try it online here, or verify all the test cases at once here.
+QtrFQ Implicit: Q=eval(input())
Trailing Q inferred
rFQ Generate range [input 1 - input 2)
t Discard first element
+Q Prepend Q
answered 2 days ago
Sok
3,309722
3,309722
UsingFinstead of.*on 2-element lists is a brilliant trick that I will absolutely be using from here on.
– hakr14
2 days ago
add a comment |
UsingFinstead of.*on 2-element lists is a brilliant trick that I will absolutely be using from here on.
– hakr14
2 days ago
Using
F instead of .* on 2-element lists is a brilliant trick that I will absolutely be using from here on.– hakr14
2 days ago
Using
F instead of .* on 2-element lists is a brilliant trick that I will absolutely be using from here on.– hakr14
2 days ago
add a comment |
up vote
3
down vote
Red, 75 bytes
func[a b][s: sign? d: b - a prin[a b]loop absolute d - s[prin[""a: a + s]]]
Try it online!
add a comment |
up vote
3
down vote
Red, 75 bytes
func[a b][s: sign? d: b - a prin[a b]loop absolute d - s[prin[""a: a + s]]]
Try it online!
add a comment |
up vote
3
down vote
up vote
3
down vote
Red, 75 bytes
func[a b][s: sign? d: b - a prin[a b]loop absolute d - s[prin[""a: a + s]]]
Try it online!
Red, 75 bytes
func[a b][s: sign? d: b - a prin[a b]loop absolute d - s[prin[""a: a + s]]]
Try it online!
answered 2 days ago
Galen Ivanov
5,62211031
5,62211031
add a comment |
add a comment |
up vote
3
down vote
Clean, 49 bytes
import StdEnv
@a b=init[a,b:tl[a,a+sign(b-a)..b]]
Try it online!
add a comment |
up vote
3
down vote
Clean, 49 bytes
import StdEnv
@a b=init[a,b:tl[a,a+sign(b-a)..b]]
Try it online!
add a comment |
up vote
3
down vote
up vote
3
down vote
Clean, 49 bytes
import StdEnv
@a b=init[a,b:tl[a,a+sign(b-a)..b]]
Try it online!
Clean, 49 bytes
import StdEnv
@a b=init[a,b:tl[a,a+sign(b-a)..b]]
Try it online!
answered 2 days ago
Οurous
5,76311031
5,76311031
add a comment |
add a comment |
up vote
3
down vote
Python 2, 52 47 41 bytes
lambda i,j:[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-5 with thanks to @JoKing
-6 by slicing the first element from the range (idea stolen from and with credit to @TFeld)
Non-lambda version...
Python 2, 51 49 47 bytes
i,j=input();print[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-2 with thanks to @JoKing
add a comment |
up vote
3
down vote
Python 2, 52 47 41 bytes
lambda i,j:[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-5 with thanks to @JoKing
-6 by slicing the first element from the range (idea stolen from and with credit to @TFeld)
Non-lambda version...
Python 2, 51 49 47 bytes
i,j=input();print[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-2 with thanks to @JoKing
add a comment |
up vote
3
down vote
up vote
3
down vote
Python 2, 52 47 41 bytes
lambda i,j:[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-5 with thanks to @JoKing
-6 by slicing the first element from the range (idea stolen from and with credit to @TFeld)
Non-lambda version...
Python 2, 51 49 47 bytes
i,j=input();print[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-2 with thanks to @JoKing
Python 2, 52 47 41 bytes
lambda i,j:[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-5 with thanks to @JoKing
-6 by slicing the first element from the range (idea stolen from and with credit to @TFeld)
Non-lambda version...
Python 2, 51 49 47 bytes
i,j=input();print[i,j]+range(i,j,(i<j)*2-1)[1:]
Try it online!
-2 with thanks to @JoKing
edited 2 days ago
answered 2 days ago
ElPedro
3,4031023
3,4031023
add a comment |
add a comment |
up vote
3
down vote
APL (Dyalog Classic), 29 bytes
{⍺,⍵,(⌽⍣(⍺>⍵))(⍺⌊⍵)+¯1↓⍳|⍺-⍵}
Try it online!
A port of my J solution
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
add a comment |
up vote
3
down vote
APL (Dyalog Classic), 29 bytes
{⍺,⍵,(⌽⍣(⍺>⍵))(⍺⌊⍵)+¯1↓⍳|⍺-⍵}
Try it online!
A port of my J solution
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
add a comment |
up vote
3
down vote
up vote
3
down vote
APL (Dyalog Classic), 29 bytes
{⍺,⍵,(⌽⍣(⍺>⍵))(⍺⌊⍵)+¯1↓⍳|⍺-⍵}
Try it online!
A port of my J solution
APL (Dyalog Classic), 29 bytes
{⍺,⍵,(⌽⍣(⍺>⍵))(⍺⌊⍵)+¯1↓⍳|⍺-⍵}
Try it online!
A port of my J solution
answered 2 days ago
Galen Ivanov
5,62211031
5,62211031
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
add a comment |
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
Wow, I'm surprised this is so long for a seemingly simple task.
– Quintec
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
@Quintec Probably it can be golfed, or maybe another algorithm will result in much shorter solution.
– Galen Ivanov
yesterday
add a comment |
up vote
3
down vote
J, 13 bytes
,,<.+i.@-~-.=
Try it online!
i.@-~ range [0 .. |difference|-1], reverse if the difference is positive
-.= remove the zero (either "=" is 0 or there’s nothing to remove)
<.+ to each element add the smaller of the args
,, prepend args
Nice solution! I totally forgot abouti.with negative argument.
– Galen Ivanov
2 days ago
this is gorgeous!
– Jonah
8 hours ago
add a comment |
up vote
3
down vote
J, 13 bytes
,,<.+i.@-~-.=
Try it online!
i.@-~ range [0 .. |difference|-1], reverse if the difference is positive
-.= remove the zero (either "=" is 0 or there’s nothing to remove)
<.+ to each element add the smaller of the args
,, prepend args
Nice solution! I totally forgot abouti.with negative argument.
– Galen Ivanov
2 days ago
this is gorgeous!
– Jonah
8 hours ago
add a comment |
up vote
3
down vote
up vote
3
down vote
J, 13 bytes
,,<.+i.@-~-.=
Try it online!
i.@-~ range [0 .. |difference|-1], reverse if the difference is positive
-.= remove the zero (either "=" is 0 or there’s nothing to remove)
<.+ to each element add the smaller of the args
,, prepend args
J, 13 bytes
,,<.+i.@-~-.=
Try it online!
i.@-~ range [0 .. |difference|-1], reverse if the difference is positive
-.= remove the zero (either "=" is 0 or there’s nothing to remove)
<.+ to each element add the smaller of the args
,, prepend args
answered 2 days ago
FrownyFrog
2,2371518
2,2371518
Nice solution! I totally forgot abouti.with negative argument.
– Galen Ivanov
2 days ago
this is gorgeous!
– Jonah
8 hours ago
add a comment |
Nice solution! I totally forgot abouti.with negative argument.
– Galen Ivanov
2 days ago
this is gorgeous!
– Jonah
8 hours ago
Nice solution! I totally forgot about
i. with negative argument.– Galen Ivanov
2 days ago
Nice solution! I totally forgot about
i. with negative argument.– Galen Ivanov
2 days ago
this is gorgeous!
– Jonah
8 hours ago
this is gorgeous!
– Jonah
8 hours ago
add a comment |
up vote
3
down vote
TI-BASIC, 35 34 bytes
-1 byte from Misha Lavrov
Prompt A,B
Disp A,B
cos(π(A>B
For(I,A+Ans,B-Ans,Ans
Disp I
End
2
And one more byte by replacing1-2(A>Bwithcos(π(A>B.
– Misha Lavrov
2 days ago
@MishaLavrovseq(wouldn't work for inputs whereAandBare the same, unfortunately :(
– kamoroso94
2 days ago
True - also, I left out an argument ofseq(, so I'm no longer convinced it even is smaller. Still, thecos(trick should help.
– Misha Lavrov
2 days ago
add a comment |
up vote
3
down vote
TI-BASIC, 35 34 bytes
-1 byte from Misha Lavrov
Prompt A,B
Disp A,B
cos(π(A>B
For(I,A+Ans,B-Ans,Ans
Disp I
End
2
And one more byte by replacing1-2(A>Bwithcos(π(A>B.
– Misha Lavrov
2 days ago
@MishaLavrovseq(wouldn't work for inputs whereAandBare the same, unfortunately :(
– kamoroso94
2 days ago
True - also, I left out an argument ofseq(, so I'm no longer convinced it even is smaller. Still, thecos(trick should help.
– Misha Lavrov
2 days ago
add a comment |
up vote
3
down vote
up vote
3
down vote
TI-BASIC, 35 34 bytes
-1 byte from Misha Lavrov
Prompt A,B
Disp A,B
cos(π(A>B
For(I,A+Ans,B-Ans,Ans
Disp I
End
TI-BASIC, 35 34 bytes
-1 byte from Misha Lavrov
Prompt A,B
Disp A,B
cos(π(A>B
For(I,A+Ans,B-Ans,Ans
Disp I
End
edited 2 days ago
answered 2 days ago
kamoroso94
65149
65149
2
And one more byte by replacing1-2(A>Bwithcos(π(A>B.
– Misha Lavrov
2 days ago
@MishaLavrovseq(wouldn't work for inputs whereAandBare the same, unfortunately :(
– kamoroso94
2 days ago
True - also, I left out an argument ofseq(, so I'm no longer convinced it even is smaller. Still, thecos(trick should help.
– Misha Lavrov
2 days ago
add a comment |
2
And one more byte by replacing1-2(A>Bwithcos(π(A>B.
– Misha Lavrov
2 days ago
@MishaLavrovseq(wouldn't work for inputs whereAandBare the same, unfortunately :(
– kamoroso94
2 days ago
True - also, I left out an argument ofseq(, so I'm no longer convinced it even is smaller. Still, thecos(trick should help.
– Misha Lavrov
2 days ago
2
2
And one more byte by replacing
1-2(A>B with cos(π(A>B.– Misha Lavrov
2 days ago
And one more byte by replacing
1-2(A>B with cos(π(A>B.– Misha Lavrov
2 days ago
@MishaLavrov
seq( wouldn't work for inputs where A and B are the same, unfortunately :(– kamoroso94
2 days ago
@MishaLavrov
seq( wouldn't work for inputs where A and B are the same, unfortunately :(– kamoroso94
2 days ago
True - also, I left out an argument of
seq(, so I'm no longer convinced it even is smaller. Still, the cos( trick should help.– Misha Lavrov
2 days ago
True - also, I left out an argument of
seq(, so I'm no longer convinced it even is smaller. Still, the cos( trick should help.– Misha Lavrov
2 days ago
add a comment |
up vote
2
down vote
Charcoal, 15 bytes
IE²NI…⊕θηI⮌…⊕ηθ
Try it online! Link is to verbose version of code. Explanation:
IE²N
Print the inputs on separate lines.
I…⊕θη
Print the ascending range, if any.
I⮌…⊕ηθ
Print the reverse ascending reverse range, if any.
add a comment |
up vote
2
down vote
Charcoal, 15 bytes
IE²NI…⊕θηI⮌…⊕ηθ
Try it online! Link is to verbose version of code. Explanation:
IE²N
Print the inputs on separate lines.
I…⊕θη
Print the ascending range, if any.
I⮌…⊕ηθ
Print the reverse ascending reverse range, if any.
add a comment |
up vote
2
down vote
up vote
2
down vote
Charcoal, 15 bytes
IE²NI…⊕θηI⮌…⊕ηθ
Try it online! Link is to verbose version of code. Explanation:
IE²N
Print the inputs on separate lines.
I…⊕θη
Print the ascending range, if any.
I⮌…⊕ηθ
Print the reverse ascending reverse range, if any.
Charcoal, 15 bytes
IE²NI…⊕θηI⮌…⊕ηθ
Try it online! Link is to verbose version of code. Explanation:
IE²N
Print the inputs on separate lines.
I…⊕θη
Print the ascending range, if any.
I⮌…⊕ηθ
Print the reverse ascending reverse range, if any.
answered 2 days ago
Neil
77.5k744174
77.5k744174
add a comment |
add a comment |
up vote
2
down vote
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!
>= to >add a comment |
up vote
2
down vote
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!
>= to >add a comment |
up vote
2
down vote
up vote
2
down vote
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!
>= to >
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!
>= to >answered 2 days ago
Elcan
27115
27115
add a comment |
add a comment |
up vote
2
down vote
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!
add a comment |
up vote
2
down vote
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!
add a comment |
up vote
2
down vote
up vote
2
down vote
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!
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!
edited 2 days ago
answered 2 days ago
steenbergh
6,77411739
6,77411739
add a comment |
add a comment |
up vote
2
down vote
Ruby, 33 40 bytes
->a,b{[a,b]+[*a..b,*a.downto(b)][1..-2]}
Try it online!
Temporary fix, trying to find a better idea
3
For[4,4]this gives only one[4]
– Kirill L.
2 days ago
add a comment |
up vote
2
down vote
Ruby, 33 40 bytes
->a,b{[a,b]+[*a..b,*a.downto(b)][1..-2]}
Try it online!
Temporary fix, trying to find a better idea
3
For[4,4]this gives only one[4]
– Kirill L.
2 days ago
add a comment |
up vote
2
down vote
up vote
2
down vote
Ruby, 33 40 bytes
->a,b{[a,b]+[*a..b,*a.downto(b)][1..-2]}
Try it online!
Temporary fix, trying to find a better idea
Ruby, 33 40 bytes
->a,b{[a,b]+[*a..b,*a.downto(b)][1..-2]}
Try it online!
Temporary fix, trying to find a better idea
edited 2 days ago
answered 2 days ago
G B
7,4261327
7,4261327
3
For[4,4]this gives only one[4]
– Kirill L.
2 days ago
add a comment |
3
For[4,4]this gives only one[4]
– Kirill L.
2 days ago
3
3
For
[4,4] this gives only one [4]– Kirill L.
2 days ago
For
[4,4] this gives only one [4]– Kirill L.
2 days ago
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
up vote
2
down vote
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.
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.
edited 2 days ago
answered 2 days ago
nwellnhof
5,568921
5,568921
add a comment |
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
up vote
2
down vote
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.
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.
answered 2 days ago
mazzy
1,655312
1,655312
add a comment |
add a comment |
up vote
2
down vote
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; }
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
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
– ouflak
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
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.
– ElPedro
2 days ago
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
|
show 1 more comment
up vote
2
down vote
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; }
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
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
– ouflak
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
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.
– ElPedro
2 days ago
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
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; }
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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; }
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
Marco Tulio Avila Cerón
213
213
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Marco Tulio Avila Cerón is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
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
– ouflak
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
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.
– ElPedro
2 days ago
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
|
show 1 more comment
1
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
– ouflak
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
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.
– ElPedro
2 days ago
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
1
1
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
– ouflak
2 days ago
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
– ouflak
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Ok, learning about hte Code golf, I will do better with time
– Marco Tulio Avila Cerón
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
Welcome to PPCG! Good to have you with us :)
– Shaggy
2 days ago
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.
– ElPedro
2 days ago
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.
– ElPedro
2 days ago
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
How do I compile this? Simply putting it in a main class doesn't seem to work.
– Dennis♦
yesterday
|
show 1 more comment
1 2
next
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175485%2fthe-first-the-last-and-everything-between%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I guess we can't take the inputs in pre-ordered order?
– Kevin Cruijssen
2 days ago
@KevinCruijssen, no, the output order depends on the input order
– TFeld
2 days ago
@StewieGriffin, the output order has to be the same as the input
– TFeld
2 days ago
Is this output format acceptable? Note the newline
– Luis Mendo
2 days ago
2
@KevinCruijssen Any reasonable I/O is acceptable.
– TFeld
2 days ago