22
\$\begingroup\$

Task

Given two positive integers a, b and a Unicode mathematical inequality symbol c, determine if a c b is true.

You many take the character or its Unicode codepoint for the input c. You may output your language's truthy/falsy values, or two distinct values for true and false respectively.

Standard rules apply. The shortest code in bytes wins.

List of symbols to support

Symbol  |  Hex   |  Dec   |  Name
--------+--------+--------+-------------
<       |  003C  |  60    |  Less than
=       |  003D  |  61    |  Equal to
>       |  003E  |  62    |  Greater than
≠       |  2260  |  8800  |  Not equal to
≤       |  2264  |  8804  |  Less than or equal to
≥       |  2265  |  8805  |  Greater than or equal to
≮       |  226E  |  8814  |  Not less than
≯       |  226F  |  8815  |  Not greater than
≰       |  2270  |  8816  |  Neither less than nor equal to
≱       |  2271  |  8817  |  Neither greater than nor equal to

The last four symbols may look broken in Chrome. They are four symbols <>≤≥ with slash overstrike, indicating negation.

Truthy test cases

1 < 2
1 = 1
2 > 1
1 ≠ 2
2 ≠ 1
1 ≤ 1
1 ≤ 2
2 ≥ 1
1 ≥ 1
2 ≮ 1
1 ≮ 1
1 ≯ 1
1 ≯ 2
2 ≰ 1
1 ≱ 2

Falsy test cases

1 < 1
2 < 1
1 = 2
2 = 1
1 > 1
1 > 2
1 ≠ 1
2 ≤ 1
1 ≥ 2
1 ≮ 2
2 ≯ 1
1 ≰ 1
1 ≰ 2
2 ≱ 1
1 ≱ 1
\$\endgroup\$
  • \$\begingroup\$ Kind-of related: Evaluate a chain of inequalities \$\endgroup\$ – xnor Apr 16 at 14:03
  • 9
    \$\begingroup\$ I'm pleased to report that Mathematica does not have built-in support for the symbol . \$\endgroup\$ – Greg Martin Apr 17 at 7:36
  • 2
    \$\begingroup\$ There must be some language where the empty program will work here. \$\endgroup\$ – JDL Apr 17 at 8:14
  • 1
    \$\begingroup\$ @GregMartin It does support that symbol and detect that it is the NotGreaterEqual function, but it doesn't have a built-in meaning :( \$\endgroup\$ – my pronoun is monicareinstate Apr 17 at 12:54
  • 2
    \$\begingroup\$ This would be a lot more "fun" if it included complex numbers, NaN, or something else lacking a total order, so that ≮ wouldn't be equivalent to ≥. \$\endgroup\$ – Joseph Sible-Reinstate Monica Apr 18 at 20:35

11 Answers 11

20
\$\begingroup\$

JavaScript (ES6),  58 45  42 bytes

Saved 3 bytes thanks to @Neil

Expects the Unicode code point for \$c\$. Returns \$0\$ or \$1\$.

(a,c,b)=>'14353426'[c%61%9]>>(a>b?2:b>a)&1

Try it online!

How?

Each comparison character is assigned a 3-bit mask describing if it should be truthy for a > b, a < b or a == b.

 char. | code | meaning                           | > | < | = | mask
-------+------+-----------------------------------+---+---+---+------
   <   |   60 | Less than                         | 0 | 1 | 0 |  2
   =   |   61 | Equal to                          | 0 | 0 | 1 |  1
   >   |   62 | Greater than                      | 1 | 0 | 0 |  4
   ≠   | 8800 | Not equal to                      | 1 | 1 | 0 |  6
   ≤   | 8804 | Less than or equal to             | 0 | 1 | 1 |  3
   ≥   | 8805 | Greater than or equal to          | 1 | 0 | 1 |  5
   ≮   | 8814 | Not less than                     | 1 | 0 | 1 |  5
   ≯   | 8815 | Not greater than                  | 0 | 1 | 1 |  3
   ≰   | 8816 | Neither less than nor equal to    | 1 | 0 | 0 |  4
   ≱   | 8817 | Neither greater than nor equal to | 0 | 1 | 0 |  2

We store these masks in a 8-character lookup string whose index is computed by applying two consecutive modulos to the code point:

 code | mod 61 | mod 9 | mask
------+--------+-------+------
   60 |   60   |   6   |  2
   61 |    0   |   0   |  1
   62 |    1   |   1   |  4
 8800 |   16   |   7   |  6
 8804 |   20   |   2   |  3
 8805 |   21   |   3   |  5
 8814 |   30   |   3   |  5
 8815 |   31   |   4   |  3
 8816 |   32   |   5   |  4
 8817 |   33   |   6   |  2
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 42 bytes: '14353426'[c%61%9]>>(b<a?2:b>a)&1 \$\endgroup\$ – Neil Apr 16 at 12:54
  • \$\begingroup\$ @Neil This is precisely what I was trying to do, but I got bogged down with the lookup update. Thanks! \$\endgroup\$ – Arnauld Apr 16 at 13:01
10
\$\begingroup\$

Python 2, 45 bytes

lambda a,o,b:o%83*45%555%16%6+1>>cmp(a,b)+1&1

Try it online!

Improved based on @Arnauld's answer, make sure to upvote him!

The bitmask here is a different from @Arnauld's answer because bit 0 and 1 are swapped. As usual, the lookup table is replaced by some cool magic numbers.

Python 3, 51 48 47 bytes

lambda a,o,b:o%83*45%555%16%6+1>>(a>b)+(a>=b)&1

Try it online!


Python 2, 47 46 bytes

lambda a,o,b:(cmp(a,b)==1-o*6%43%7%3)^o*3%58%3

Try it online!

Every operation can be expressed by (cmp(a,b)==a)^b. For example, a<b iff (cmp(a,b)==-1)^0. We then use some dirty magic numbers to compress a and b.

Python 3, 51 49 48 bytes

lambda a,o,b:((a<b)+(a<=b)==o*6%43%7%3)^o*3%58%3

Try it online!

| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

05AB1E, 34 33 24 23 18 bytes

•1P42•b3ôs61%èŠ.Sè

-9 bytes by porting @ovs' Python 3 answer, so make sure to upvote him!
-6 bytes thanks to @Grimmy.

Input of the character as codepoint integer. Input-order as c,b,a.

Try it online or verify all test cases.

Explanation:

•1P42•             # Push compressed integer 18208022
      b            # Convert it to binary 1000101011101010100010110
       3ô          # Split it in parts of size 3:
                   #  [100,"010",101,110,101,"010","001","011",0]
         s         # Take the first codepoint input
          61%      # Take modulo-61
             è     # Index it into the binary list (0-based and with wraparound)
              Š    # Triple-swap to take the next two inputs
               .S  # Compare them (-1 if a<b; 0 if a==b; 1 if a>b)
                 è # And use that to index into the triplet (where -1 is the last item)
                   # (after which the result is output implicitly)

See this 05AB1E tip of mine (sections How to compress large integers?) to understand why •1P42• is 18208022.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Here's 18 \$\endgroup\$ – Grimmy Apr 24 at 6:14
  • \$\begingroup\$ @Grimmy Oh nice! Thanks. \$\endgroup\$ – Kevin Cruijssen Apr 24 at 6:40
5
\$\begingroup\$

Python 3, 99 82 bytes

17 bytes saved thanks to @ovs!

Uses the operator similarities a<b <=> a≱b, etc.

lambda a,o,b:[a<b,a>b,a<=b,a>=b,a==b,a!=b]['<≱>≰≤≯≥≮= ≠'.find(o)//2]

Try it online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Python 2, 42 41 38 bytes

Takes in as input two operands \$ a \$ and \$ b \$, and the operator \$ c \$ in codepoint form. Test cases nicely borrowed from @newbie.

lambda a,c,b:(cmp(a,b)+63)*c%1895%57&1

Try it online!


The idea is the same as @newbie's, generating pseudorandom numbers until they match the output. The cmp function returns -1, 0, or 1 if the left argument is less, equal to, or greater than the right argument, respectively. And also because MathJax looks nice, here is the formula in MathJax:

$$ ((((\text{cmp}(a,b) + 63) * c) \bmod 1895) \bmod 57) \bmod 2 $$

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice :))))))))) \$\endgroup\$ – newbie Apr 17 at 8:19
  • \$\begingroup\$ 39 bytes, i guess i'll leave my answer unchanged \$\endgroup\$ – newbie Apr 17 at 9:35
  • \$\begingroup\$ @newbie That's a great answer, but I think you should post it as yours, since you found it. \$\endgroup\$ – dingledooper Apr 17 at 18:55
3
\$\begingroup\$

Python 3, 68 59 bytes

Takes the unicode code point of the operator as input.

lambda a,o,b:[a==b,a>b,a<=b,a>=b,a<=b,a>b,a<b,a!=b][o%61%9]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Io, 90 bytes

Port of the Python answer.

method(a,o,b,list(a<b,a>b,a<=b,a>=b,a==b,a!=b)at("<≱>≰≤≯≥≮= ≠"findSeq(o)/2))

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 80 bytes

\d+
$*
≠
<>
≤|≯
<=
≥|≮
=>
≰
>
≱
<
^(1+)(<.?1\1|.?>(?!\1)|<?=>?\1$)

Try it online! Link includes test suite. Takes input as acb, but the test suite deletes spaces to make the input more readable. Explanation:

\d+
$*

Convert to unary.

≠
<>
≤|≯
<=
≥|≮
=>
≰
>
≱
<

Replace the Unicode operators with ASCII-based logical operators. The => is reversed to make the final condition golfier.

^(1+)(<.?1\1|.?>(?!\1)|<?=>?\1$)

Match the first number, then check whether one of the relations can be fulfilled.

  • If the character after the first number is a <, then after an optional > or =, then to fulfil this relation the second number needs to equal 1 or more than the first number.
  • If after an optional < or =, there is a > before the second number, then to fulfil this relation the second number must not be at least equal to the first number.
  • If after an optional < there is an = before an optional >, then to fulfil this relation the second number must be equal to the first number.
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Sledgehammer, 17 bytes

16.75, to be overly specific

⣕⢌⢲⢼⠴⢺⢟⢼⣑⣮⣊⠞⠀⢄⡕⡝⢥

There's no point in trying to read that, so here's the corresponding Mathematica code with a reasonable explanation:

ToExpression@StringReplace[ToString@FullForm@ToExpression@Input[],"ot"->"ot@"]

The code evaluates the expression first. Unfortunately, the operator (and a few other similar ones) is not supported, and is kept verbatim. The code then rewrites the expression into a prefix-ish form (NotGreaterEqual[1, 1]), and replaces ot with ot@, turning NotGreaterEqual[1, 1] into Not@GreaterEqual[1, 1], a call of the function Not on the result of GreaterEqual. Of course, since that was a string replacement, the result is then evaluated once again.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 46 bytes

f(a,c,b){a="14353426"[c%61%9]>>(a>b?2:b>a)&1;}

Try it online!

Port of Arnauld's JavaScript answer.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 38 bytes

Nθ≔I§”←⧴LH⎚G₂ⅉυ”℅SηNζ⁼§⟦‹θζ⁼θζ›θζ⟧η÷η³

Try it online! Link is to verbose version of code. Takes input as a c b and outputs a Charcoal boolean, i.e. - for true, nothing for false. Explanation:

Nθ

Input a.

≔I§”←⧴LH⎚G₂ⅉυ”℅Sη

Input c and cyclically look up its ordinal in the compressed string __20__345___02531_ (the _s are arbitrary; the linked code uses spaces) and save its value.

Nζ

Input b.

⁼§⟦‹θζ⁼θζ›θζ⟧η÷η³

Make a list of the comparisons a<b, a=b, a>b, cyclically index using the c value, and negate the result if the c value is less than 3.

| improve this answer | |
\$\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.