!%lnxWO
Reads a number from stdin.
Explanation:
To start with, Z
= 0
, W
= 2
and O
= 1. This allows placing of W
and O
next to each other, whereas using 2
and 1
would be interpreted as the number 21 without a separating space (an unwanted extra character).
In Clip, the modulo function (%
) works on non-integers, so, to work out if some value v
is an integer, you check if v
mod 1 = 0. Using Clip syntax, this is written as =0%v1
. However, as booleans are stored as 1
(or anything else) and 0
, checking if something is equal to 0
is just 'not'ing it. For this, Clip has the !
operator. In my code, v
is lnx2
. x
is an input from stdin, n
converts a string to a number and lab
is log base b
of a
. The program therefore translates (more readably) to 0 = ((log base 2 of parseInt(readLine)) mod 1)
.
Examples:
8
outputs
1
and
10
outputs
0
Edit 1: replaced 0
, 1
and 2
with Z
, O
and W
.
Edit 2: replaced =Z
with !
.
Also:
Compresses the Clip version even further, as Pyth has Q for already evaluated input and a log2(a) function instead of just general log(a, b).
!%lQ1