# Implementing bool() without any conditionals [closed]

Title says it all. Given some input, turn it into a boolean value (0 or 1), without using conditional statements (e.x. == > < <= >= != ~=), or any function which uses conditional statements within its implementation, such as if, while and for. Your program must work with floating points, although you get more if it works with other items.

A general rule for determining whether bool(item) is False is if the item is empty ('' is an empty string, [] is an empty list.), or if the function used to turn an item of one type into another is given no input (in Python, str() returns '', list() returns [].)

Here are (some of) the different valid types. Your program is considered to work with one type if it works with one item in the following lists:

• Integers, floating points, doubles, longs
• Strings
• Lists, tuples, arrays (bytearrays)
• Dictionaries
• Functions
• Objects/Classes

Program score will be calculated like this: [# of characters]-(50*[# of types]). The program with the smallest score wins the challenge.

## closed as unclear what you're asking by John Dvorak, Justin, ProgramFOX, Timtech, Doorknob♦Mar 5 '14 at 14:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• This problem is deceptively difficult. Perhaps a more concise wording would be to disallow any language construct which passively performs a boolean conversion (as &&, ||, while, for(;foo;), etc. all do). – primo Dec 24 '12 at 19:25
• Would you give an example of valid input that does not use comparison operators (such as == > < <- >= !=)? I must be missing something. It seems to me that they are essential elements of the sorts of propositions one wishes to assess. – DavidC Dec 24 '12 at 20:00
• How do you measure how many types you handle? Do types such as float/double count as a single floating-point type or two separate types? – MrZander Dec 24 '12 at 20:33
• How can this possibly be language-agnostic? The problem definition makes various assumptions that seem to tie it to Python and close relatives, and the scoring system lets some languages claim a score of -Infinity. – Peter Taylor Dec 25 '12 at 11:55
• @PeterTaylor as I understand it, there are only 7 different 'types' possible, ergo a maximum (or rather, minimum) of -350 bonus. Although, I do agree that Set/Frozenset only applies to very few languages, and probably shouldn't be included. – primo Dec 25 '12 at 12:20

# Python - 122 chars - 50*13 types = -528

def b(x=False):
try:return[False][x]
except IndexError:return True
except:
try:return b(len(x))
except:return True


Works for the following built-in types (as listed in §3.2 The standard type hierarchy)

1. NotImplemented
2. Ellipsis
3. int
4. long
5. bool
6. string
7. unicode
8. tuple
9. list
10. bytearray
11. set
12. frozenset
13. dict
• Using exceptions to emulate conditionals... seems to simple, in retrospect ;) – primo Dec 25 '12 at 4:24
• What you have there seems to be 121 characters, not 122 :) Also, you can save one more character by passing instead of returning True and then doing that at the end. – Ry- Dec 31 '12 at 0:33

# MMIX (8 bytes assembled)

Assembly language has no statements.

bool    ZSNZ $0,$0,1
POP  1,0


# JavaScript, -278

function bool(_,$){try{$=_.length;$._;_=$}catch(_){}return 1-isNaN(_/_)}


Doesn’t use any boolean operators! If global leaking and input is okay, then it’s even shorter, at 65 characters:



## Perl, 5 char

1-!\$x


Works for integers, floating-point values, strings, and C. Although in Perl, these are all the same type.

• ! is a boolean operation – FUZxxl Dec 25 '12 at 17:47