37
\$\begingroup\$

Write shortest possible code that will return true if the two given integer values are equal or their sum or absolute difference is 5.

Example test cases:

4 1 => True
10 10 => True
1 3 => False
6 2 => False
1 6 => True
-256 -251 => True
6 1 => True
-5 5 => False

The shortest I could come up with in python2 is 56 characters long:

x=input();y=input();print all([x-y,x+y-5,abs(x-y)-5])<1

-9, thanks @ElPedro. It takes input in format x,y:

x,y=input();print all([x-y,x+y-5,abs(x-y)-5])<1
\$\endgroup\$
1
  • 11
    \$\begingroup\$ welcome to PPCG! This is a good first challenge -- the challenge is clearly defined, it has ample test cases, and uses our default I/O! If you stick around for a while and keep thinking up interesting challenges, I would recommend using The Sandbox to get feedback before posting them to this site. I hope you enjoy the time you spend here! \$\endgroup\$
    – Giuseppe
    Jan 16 '19 at 16:22

41 Answers 41

1
2
1
\$\begingroup\$

Common Lisp, 48 bytes

(lambda(a b)(find 5(list(abs(- b a))a(+ a b)b)))
\$\endgroup\$
1
\$\begingroup\$

Pyth, 12 bytes

}5[sQaFQ+4l{

Try it online!

}5[sQaFQ+4l{
}5[sQaFQ+4l{Q) // Implicit input and closed brace
  [          ) // Create an array of the following:
   sQ          //   Sum of input
     aFQ       //   Absolute difference of input
        +4l{Q  //   Length of unique values in input, + 4
}5             // Is '5' in the array? 
\$\endgroup\$
1
+50
\$\begingroup\$

Vyxal, 11 10 bytes

=£₌+ĸW5c¥∨

Try it Online!

Credits

  • Saved 1 byte thanks to @Lyxal by employing the use of an absolute difference built-in
\$\endgroup\$
1
1
\$\begingroup\$

Pyth, 15 bytes

|!!/,a.*QsQ5q.*

Try it online!

Not super impressed, especially having to use double negation !! to cast a number into true... I'm sure there's a better way.

|!!/,a.*QsQ5q.*
   /,      5     Count '5's in: 
     a.*Q        Absolute value of (A, B)
         sQ      Sum of (A, B)
 !!              Cast to truthy
|                OR:
            q.*  A==B
\$\endgroup\$
1
0
\$\begingroup\$

Perl 5, 51 bytes

Pretty simple really, uses the an flags for input. Outputs 0 for false, 1 for true. Not gonna lie, I don't know if bitwise OR is appropriate here, but is does work for all the test cases, so that's nice.

($a,$b)=@F;print($a==$b|$a+$b==5|$a-$b==5|$b-$a==5)

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ bitwise OR is equivalent to logical OR when operating on one-bit values. In general, if passed a truthy arguments bitwise OR will return truthy, and if passed two falsy arguments bitwise OR will return 0. \$\endgroup\$
    – att
    Jan 18 '19 at 5:24
  • \$\begingroup\$ -11 bytes \$\endgroup\$ Jan 24 '19 at 11:29
  • 1
    \$\begingroup\$ 38 bytes \$\endgroup\$ Jan 24 '19 at 11:36
0
\$\begingroup\$

Julia 1.0, 38 bytes

f(a,b)=((a+b-5)*(a-b)*(abs(a-b)-5))==0

Try it online!

Of course, it's probably not the shortest one

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

APL(NARS) 19 chars, 38 bytes

{∨/(⍺=⍵),5∊∣⍺+⍵,-⍵}

  f←{∨/(⍺=⍵),5∊∣⍺+⍵,-⍵}
  4 f 1
1
  1 f 3
0
  6 f 2
0
  1 f 6
1
  ¯256 f ¯251
1
  6 f 1
1
  ¯5 f 5
0
\$\endgroup\$
0
\$\begingroup\$

Ruby (>=1.9), 23 38 bytes

->(a,b){a==b or a+b==5 or(a-b).abs==5}

Try it online!

It's an anonymous function, so the TIO has some extra code to take input and return the output. Put the two numbers on separate lines.

Returns true if they are equal or add to 5, or if their absolute difference is 5, and false if not.

\$\endgroup\$
2
  • \$\begingroup\$ :| it's longer than the other (older) one \$\endgroup\$
    – ASCII-only
    Jan 28 '19 at 4:34
  • \$\begingroup\$ also. better link \$\endgroup\$
    – ASCII-only
    Jan 28 '19 at 4:35
0
\$\begingroup\$

Runic Enchantments, 27 bytes

i::i::}3s=?!@-'|A5:}=?!@+=@

Try it online!

Outputs 0 (exactly one zero) for false and any other output (literally whatever is left on the stack) for true.

New interpreter build, updated answer, saved 3 bytes (previous revision miscounted and said 4).

Explanation:

i::i::                         Read two inputs, duplicating each twice.
      }                        Rotate stack right
       3s                      Rotate the top 3 items to the right
         =? @                  Compare equal, if so, dump stack
           !                   If not, skip next instruction
             -'|A              Subtract, absolute value, (j)
                 5:}           Push two 5s, rotate one to the bottom
                    =? @       Compare 5 with (j), if equal, dump stack
                      ! +=     If not, add the two inputs, compare with remaining 5
                          @    Dump stack (will be 1 for true and 0 for false)

Essentially it reads both inputs 3 times and arranges the stack so that each operation works on a copy of each input (or a literal 5). As the only way to dump a 0 is if all three comparisons are false, simply dumping the rest of the stack avoids having to do anything other than vomiting the entire stack to output and terminating, all posibilities of which are valid truthy values.

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

Haskell, 30 Bytes

a?b=elem 5[a-b+5,abs(a-b),a+b]
\$\endgroup\$
0
\$\begingroup\$

Desmos, 90 bytes

A pretty straightfoward solution, could definitely by golfed further:

f(a,b)=\left\{a+b=5:0,1\right\}\left\{\left|a-b\right|=5:0,1\right\}\left\{a=b:0,1\right\}

Try It On Desmos!

0 is true, and 1 is false.

\$\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.