This is a rather common inquiry in contests that require computing elements of a sequence.
For example, consider the following recursive implementation of the factorial function (Python):
f = lambda n:n<1or n*f(n-1)
This works well for all positive n; f(n)
returns the required result as an int.
However, f(0)
will return the result of the comparison n<1
, which is the the bool True.
Fixing this is easy enough, but it costs a few bytes:
f = lambda n:0**n or n*f(n-1)
While it is possible to think of the Booleans True and False as numbers (usually 1 and 0) and use them as such in languages such as Python, languages such as Java or Ruby do not even allow casting a Boolean to a numeric type.
Since this is such a common scenario, I think we would benefit from a default. If a challenge doesn't contemplate returning Booleans instead of numbers, should it be allowed or forbidden?
>>> isinstance(0 < 1, int)
True
When has it ever lied to us? \$\endgroup\$>>> True == 1
True
\$\endgroup\$bool
is a trivial subclass ofint
\$\endgroup\$==
may cast1
toTrue
. \$\endgroup\$int()
,bool()
and etc functions are like casts, but they aren't actually casts. The fact is, True and False are numbers in Pyfon. \$\endgroup\$1 is True
it returns false, in Python.... \$\endgroup\$[] is []
. \$\endgroup\$True is True
returns True.... \$\endgroup\$is
tests for reference equality. Every instance ofTrue
is the same object, but this is not necessarily true for[]
,""
,1
, etc. \$\endgroup\$