Editorial for Lyndon's Golf Contest 1 P3 - Boolean (Buffed)
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
42 bytes
One possible way to solve this problem is to output true if the number of nots and trues combined is odd, and false otherwise. Because not, true, and false are the only possible tokens, it is easy to calculate this by counting the number of ts in the entire string, . Rather than using an
if statement to determine what to output, a shorter method would be to store the two outputs in a list, and access the correct one by its index. This gives us a -byte solution:
print(['false','true'][input().count('t')%2])
Shorter yet, we can convert the integer to a boolean (True or False) via a comparison, cast it to a string, and then convert it to lowercase. This trick of reusing Python's built-in boolean literals brings our solution down to bytes:
print(str(input().count('t')%2>0).lower())
38 bytes
In order to go lower, we must find a way to convert the given input into something useful, without adding too much complexity to our program. The len() function is a perfect candidate for this, as it not only is short, but maps every unique input to a unique integer. Notice first that the length of the input can only come in two forms: and
, for some
. Equivalently, if the length is
, it must hold that
. Then, notice that the answer is
true iff . From this information, we can construct a specialized formula that happens to solve our original problem.
x%8%5>0 and 0<x%8<5, for example, are both valid ways to determine the input's truthiness. With this in mind, we obtain a -byte solution:
print(str(len(input())%8%5>0).lower())
37 bytes
The reduction to bytes is a simple, yet sneaky trick. If we instead consider
, the possible values for
are
, and the answer is
true iff . From this, it becomes clear that a single byte can be saved by shortening the formula to
-x%8>3, leading to our final solution:
print(str(-len(input())%8>3).lower())
Comments