32
\$\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\$
  • 9
    \$\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 at 16:22

35 Answers 35

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\$
  • 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\$ – attinat Jan 18 at 5:24
  • \$\begingroup\$ -11 bytes \$\endgroup\$ – Nahuel Fouilleul Jan 24 at 11:29
  • \$\begingroup\$ 38 bytes \$\endgroup\$ – Nahuel Fouilleul Jan 24 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\$
  • \$\begingroup\$ :| it's longer than the other (older) one \$\endgroup\$ – ASCII-only Jan 28 at 4:34
  • \$\begingroup\$ also. better link \$\endgroup\$ – ASCII-only Jan 28 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\$

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.