Is it possible to compare items without if?

Asked

Viewed 111 times

0

I now challenged myself little to develop a function that returns if a number is greater than 5 without the use of any comparison operator, as assertion, while, ternary operator, if which directly or indirectly compares the given number with 5.

Return could be True/False or 0/1.

I tried using bitwise operators, but I was not successful and not even a "Clue" with it.

1 answer

1


Subtract and compare with 0, but it would only work with numbers. Then you can even fix an and and have something similar to an if in some cases.

a = 5

a - 5 == 0 and 3

Return the 3, then you can join some logical operators and do more things.

  • This is basically a hack, but it’s the same thing, basically (a-5 == 5-5)

  • So I don’t even understand what you want, is there an example of what you want in other language? The thing is, after the expression, he gets the first one if he goes True and evaluate the rest, or just return False.

  • 1

    I said up there: "without the use of any comparison operator, as [...] comparing directly or indirectly the given number with 5."

  • 1

    I guess there’s no way so you can even invent another function with a character that directly compares numbers. Like in these functional languages you can even create a function that accepts an argument and compares with a number (with little Boilerplate), but inside it will still have a == ou > ou <=. An example in Haskell would be to mount a function that accepts a number and returns a bool, then calls it with the number you want to compare, the syntax would be fn 2 to compare the 2 for example, then the function would be fn x = x > 5. But it’s not even for code golfing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.