The problem
Hereinafter, [space]
refers to " "
, [tab]
refers to "t"
, and [LF]
refers to "n"
for illustrative functions. This doesn’t imply that you need to use these placeholders in your resolution.
In esoteric language known as Whitespace, numbers are represented within the following format:
- first character represents the signal:
[space]
for plus,[tab]
for minus; - characters after that and till
[LF]
are the binary illustration of the integer:[space]
for 0,[tab]
for 1. - characters after which might be the binary illustration of absolutely the worth of the quantity, with
[space]
for 0,[tab]
for 1, the rightmost bit is the least significand bit, and no main zero bits. - the binary illustration is straight away adopted by
[LF]
.
Notes
- Legitimate Whitespace quantity should all the time have a minimum of two characters: an indication and the terminator. In case there are solely two characters, the quantity is the same as zero.
- For the needs of this problem, zero should all the time be represented as
[space][LF]
. - In Whitespace, solely area, tabulation and linefeed are significant characters. All different characters are ignored. Nonetheless, for the needs of this straightforward problem, please don’t add another characters within the output.
- On this problem, enter will all the time be a sound unfavorable or optimistic integer.
- To your comfort, on this problem we’ll use
unbleach()
perform when evaluating your outcomes. This perform replaces whitespace characters with[space]
,[tab]
, and[LF]
to make fail messages extra apparent. You’ll be able to see the way it works in Instance Check Instances.
Examples
1
in Whitespace is" tn"
.- “ in Whitespace is
" n"
. -1
in Whitespace is"ttn"
.2
in Whitespace is" t n"
.-3
in Whitespace is"tttn"
.
The answer in Python code
Possibility 1:
from string import maketrans
def whitespace_number(n):
return (' n' if n == 0 else
'{:+b}n'.format(n).translate(maketrans('+-01', ' t t')))
Possibility 2:
def whitespace_number(n):
signal = ' t'[n < 0]
quantity = bin(abs(n))[2:].substitute('0', ' ').substitute('1', 't') if n else ''
return signal + quantity + 'n'
Possibility 3:
whitespace_number=lambda n:f"{n:+b}n".translate(str.maketrans("+-01"," t"*2))[n==0:]
Check circumstances to validate our resolution
def unbleach(ws):
return ws.substitute(' ', '[space]').substitute('t', '[tab]').substitute('n', '[LF]')
check.assert_equals(unbleach(whitespace_number( 1)), unbleach( " tn"))
check.assert_equals(unbleach(whitespace_number( 0)), unbleach( " n"))
check.assert_equals(unbleach(whitespace_number(-1)), unbleach( "ttn"))
check.assert_equals(unbleach(whitespace_number( 2)), unbleach( " t n"))
check.assert_equals(unbleach(whitespace_number(-3)), unbleach("tttn"))