15
\$\begingroup\$

So I have the situation where I have one boolean value a and, if that one is true and b<c I want it to end up as true, if a is false and b>c I want it to resolve as true as well. If b=c I want it to be always false, no matter the value b and c are both numbers. My best approach so far is a?b<c:b>c or maybe b^c^a^b<c. Is there any shorter approach? I can’t stop thinking about it, and want confirmation this is the shortest possible way or find the solution...

\$\endgroup\$
4
  • \$\begingroup\$ if a is true, does it matter what is output if b>c? same question for a being false and b<c \$\endgroup\$ Commented Jun 24, 2022 at 4:39
  • 3
    \$\begingroup\$ Are b and c guaranteed to be integers? \$\endgroup\$ Commented Jun 24, 2022 at 6:35
  • 2
    \$\begingroup\$ @Arnauld yes they are guaranteed integers \$\endgroup\$ Commented Jun 24, 2022 at 17:52
  • 1
    \$\begingroup\$ @thejonymyster no it doesn’t matter \$\endgroup\$ Commented Jun 25, 2022 at 6:17

2 Answers 2

26
\$\begingroup\$

Yes, it can be shorter!

c-b<a^a beats a?b<c:b>c by 2 bytes. My tests show that it works for any integer, including negatives. The downside to this formula is that a is repeated twice, which may be unideal depending on your situation.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Very clever. This works for integers because 1) if a is false, c-b<a^a is true <=> c-b<a is true <=> b>c; 2) if a is true, c-b<a^a is true <=> c-b<a is false <=> c-b<1 is false <=> c-b>=1 is true <=> b<c. But it sometimes fails for non-integers. \$\endgroup\$ Commented Jun 24, 2022 at 6:39
  • \$\begingroup\$ I think c<b+a^a also works, again for integers. \$\endgroup\$ Commented Jun 24, 2022 at 7:34
  • \$\begingroup\$ What is precedence order for c-b<a^a? Is it ((c - b) < a) ^ a? \$\endgroup\$ Commented Jun 26, 2022 at 16:04
  • 1
    \$\begingroup\$ @EricDuminil Yes, you are correct \$\endgroup\$ Commented Jul 2, 2022 at 17:59
11
\$\begingroup\$

I don't think it can be any shorter, at least in the most general case

See dingledooper's 7-bytes solution that works for integers.

If the target expression were a?b<c:b>=c (note the change on the second comparison), the two conditions are exactly the opposite, and we can get away with comparing b and c only once with a^b>=c or !a^b<c.

But, the target is a?b<c:b>c, and since b<c and b>c cannot be inferred from each other, you either need to

  • mention b and c twice each, to carry out two separate comparisons, or
  • somehow use the three states (negative, zero, positive) of b-c.

For the first method, you already have the optimal answer. For the second, the best I can get is (c-b)*(a-.5)>0, 14 bytes (TIO).

Also note that b^c^a^b<c doesn't quite work.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Javascript numbers are floating point, so have 4 states (negative, zero, positive, NaN). Similarly, the relation between a pair of numbers can be above, below, equal, or unordered (one or both NaN). It can still be interesting to golf the case of finite numbers, though, so you can exclude NaN inputs, and the case of inf-inf giving NaN. \$\endgroup\$ Commented Jun 24, 2022 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.