#MATL, 20 bytes
MATL, 20 bytes
U|sG36ZA13\[BID1]m+g
Input is a string enclosed in single quotes. Output is 1
for truthy or 0
for falsy.
Try it online! Or verify all test cases.
###How it works
How it works
U|s % Take input implicitly. Interpret as number, absolute value, sum
G36ZA % Push input again. Convert from base-36
13\ % Modulo 13
[BID1] % Push array [6 3 8 1]
m % True if member: gives 1 iff the result from modulo 13 is in the array
+g % Add, convert to logical. Display implicitly
This performs two tests on the input:
- Try to interpret the input as a number, and detect if it is nonzero. The function used to interpret a string as a number outputs an empty array if it's not possible; and the sum of the entries of an empty array is
0
. So it suffices to try the conversion, take the absolute value, and sum. This gives a positive value if the input contains a nonzero number, and0
otherwise. - Assuming the input string doesn't represent a number, we need to classify it into one of the two given sets. To do this, the input is interpreted as the digits of a number expressed in base-36, using alphabet
'01...9ab...z'
. The base conversion function is case-insensitive and ignores digits not present in the alphabet (in our case,'-'
). It turns out that the modulo 13 of the resulting number is1
,3
,6
or8
for the truthy strings, and doesn't give any of those values for the falsy strings. So it can be used as a signature. We thus perform modulo 13 and see if the result is any of those four values. This gives1
if it is, or0
otherwise.
The final result should be truthy if any of the two conditions is met, and falsy otherwise. So we add the two numbers resulting from 1 and 2 above and convert to logical, which gives 1
or 0
as standardized truthy/falsy values.