45
\$\begingroup\$

Given two integers, A and B, output A if A-B (A minus B) is in A-B (A to B), otherwise output B.

"A minus B" is standard subtraction.

"A to B" is the range of integers starting at A and ending at B, including both A and B. For example:

1 to 4: 1, 2, 3, 4
-2 to 5: -2, -1, 0, 1, 2, 3, 4, 5
3 to -1: 3, 2, 1, 0, -1
7 to 7: 7

The shortest code in bytes wins.

Test Cases

A B Output
1 4 4
-2 5 5
3 -1 -1
7 7 7
90 30 90
90 -30 -30
-90 30 30
-90 -30 -90
-2 -2 -2
-2 -1 -2
-2 0 -2
-2 1 1
-2 2 2
-1 -2 -2
-1 -1 -1
-1 0 -1
-1 1 1
-1 2 2
0 -2 -2
0 -1 -1
0 0 0
0 1 1
0 2 2
1 -2 -2
1 -1 -1
1 0 1
1 1 1
1 2 2
2 -2 -2
2 -1 -1
2 0 2
2 1 2
2 2 2

Original Chat-Mini-Challenge

\$\endgroup\$
0

37 Answers 37

1
2
0
\$\begingroup\$

Kotlin, 43 bytes

{a,b->if(a-b in a..b||a-b in b..a)a else b}

This is the one time that I am grateful that kotlin ranges are inclusive.

You could alternatively do it using xnor's algorithm with 28 bytes:

{a,b->if(2*b*b>a*b)b else a}

Test Suite

\$\endgroup\$
0
0
\$\begingroup\$

MATL, 13 bytes

tPdGSl&)&:mo)

Try it at MATL Online

Explanation

    % Implicitly grab input as a 2-element array
t   % Duplicate the input
Pd  % Flip the order and compute the difference
GS  % Grab the input again and sort it
l&) % Break the two elements of the array into two stack elements
&:  % Create the array from the smaller to the larger element
m   % Check for membership (returns FALSE or TRUE)
o   % Convert FALSE to 0 and TRUE to 1
)   % Use this as a modular index into the initial array
    % Implicitly display the result
\$\endgroup\$
0
\$\begingroup\$

Japt, 14 bytes

òV bU-V ¨0?U:V

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ohm, 25 bytes

IIGÉ┼┘-E;?┼¿┘;,

Expanation:

IIGÉ┼┘-E;?┼¿┘;,
II              ■Inputs twice
  G             ■Range a..b
   É            ■any {
    ┼┘-E        ■  firstInput-secondInput==element
        ;       ■}
         ? ¿ ;  ■if {
          ┼     ■  firstinput
           ¿    ■}else{
            ┘   ■  secondinput
             ;  ■}
              , ■Print
\$\endgroup\$
0
\$\begingroup\$

Google Sheets, 49 bytes

-2 bytes because Google Sheets automatically closes brackets

=MINUS(A1,A2
=IF(AND(GTE(A1,A3),LTE(A2,A3)),A1,A2

Inputs are in the cells A1 and A2. Assumes cell A3 is selected after input is recieved.

The newline isn't actually a newline, it's just pressing enter.

The code is pretty readable, so I doubt an explanation is needed.

\$\endgroup\$
0
\$\begingroup\$

Octave, 37 34 bytes

@(a)a(2-ismember(-diff(a),a:a(2)))

This is an anonymous function that takes the input values as an array of two elements: [-90, 30].

Breakdown:

@(a)a(                           )  % Function that takes a variable a as input
                                    % and returns the x'th element (dependent on what's
                                    % inside the parentheses.
                 -diff(a)           % Takes the negative difference between the two

                          a:a(2)))  % A vector: -3 -2 -1 0 1 2 ...
        ismember(-diff(a),a:a(2)))  % Checks if the negative difference (a-b)
                                       % is part of the range. Returns 1 for true
    a(2-ismember(-diff(a),a:a(2)))  % Return the 2-ismember() element of the input
@(a)a(2-ismember(-diff(a),a:a(2)))  % Everything put together

a(1):a(2) is the same as a:a(2) since Octave automatically assumes you want the first element. That saved an additional two bytes.

I could save one more byte by taking the input in the different order [b, a], but I figured it was cleaner this way.

\$\endgroup\$
0
\$\begingroup\$

Ly, 39 bytes

nn&s<l>>lRy![plfR11$]p<-s>l~[<<pu;]<<u;

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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