How to get the same output without using any type of conditionals?! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

How to get the same output without using any type of conditionals?!

Yesterday, I read an article that made think about it. So, it is possible to get the same results in codes below without using if-statements (or ternary operators, or switch statements)...🤔❓ Please feel free to provide explanation and/or examples in whichever language you want to. Thanks! https://code.sololearn.com/c3MdQIvKD75p/?ref=app https://code.sololearn.com/cSpS7b4aMmsZ/?ref=app

18th Jan 2019, 4:42 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
32 Answers
18th Jan 2019, 9:49 PM
ShortCode
+ 7
Andrei Voinea Thanks for sharing you knowledge. It would've been interesting seeing the similar result you are mentioning. You can submit a solution in any other language in case you want to. 🙂👍🏻
19th Jan 2019, 10:08 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 6
ODNLT Your reply makes me think again! Technically, a "Logical Operator" could be considered a 'conditional statement', as well❓‼️ what do you think?!🤔
18th Jan 2019, 9:57 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 6
ODLNT Thanks for your clarification! 🤓
19th Jan 2019, 12:33 AM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 5
ODLNT Wouldn't be "while" also a conditional statement?! Maybe, any other possible solution?!🙈
18th Jan 2019, 6:58 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 5
Jordan Toum What is your proposal?!
19th Jan 2019, 12:09 AM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 5
Python, without any dirty hacks and even any binary operators https://code.sololearn.com/cee677wS1Cgg - oops, abs() here has hidden conditional Okay, this version even better, only int() used as trick (round() can do the trick too) https://code.sololearn.com/cDuVhB9XZwXn So, here we go, my variant number 3. Now works with all numbers. int() does only trick https://code.sololearn.com/cr1CHBfR55Um
20th Jan 2019, 4:29 PM
Maksim
Maksim - avatar
+ 4
ODLNT Let's see if someone else can provide other solutions (and in other languages) as well!
18th Jan 2019, 7:47 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 4
In Ruby, there is a powerful hash map which can be any type (boolean too). See this. https://code.sololearn.com/c0p8l0L7Cskn/?ref=app
18th Jan 2019, 9:12 PM
ShortCode
+ 4
I think this is the way👇👇 https://code.sololearn.com/ce8RfqPSXMFH/?ref=app
19th Jan 2019, 7:55 PM
Sudhanshu
Sudhanshu - avatar
+ 4
Bennett Post Please, put it into a code in PlayGround. So, we can discuss it in the comment section like in previous submissions! Just edit your entry including the code, thanks!
21st Jan 2019, 7:56 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 3
Geovanny I can guarantee that there will be many other solutions posted.
18th Jan 2019, 7:59 PM
ODLNT
ODLNT - avatar
+ 3
Geovanny Technically no, logical operators are conjunctions adding conditional statements together to create a compound/complex conditional statement, but your point is well taken in that my answer still uses boolean to solve the problem.
19th Jan 2019, 12:28 AM
ODLNT
ODLNT - avatar
18th Jan 2019, 9:59 PM
ShortCode
+ 2
Let me know what you think of this solution. https://code.sololearn.com/WuVNjxTtE2Za/?ref=app
19th Jan 2019, 3:16 AM
Paul K Sadler
Paul K Sadler - avatar
+ 2
https://code.sololearn.com/cILU7oSOYfIM/?ref=app
19th Jan 2019, 2:40 PM
Dan Makarov
Dan Makarov - avatar
19th Jan 2019, 4:25 PM
Dan Makarov
Dan Makarov - avatar
+ 2
https://code.sololearn.com/cwn8n8NT3KVy/?ref=app
20th Jan 2019, 9:58 AM
Shashank Shekhar
Shashank Shekhar - avatar
+ 2
Okay, this version even better, only int() used as trick (round() can do the trick too) https://code.sololearn.com/cDuVhB9XZwXn
20th Jan 2019, 8:24 PM
Maksim
Maksim - avatar