HomeSoftware EngineeringMethods to Convert Integer to Whitespace format in Python

# Methods to Convert Integer to Whitespace format in Python

## The problem#

Hereinafter, `[space]` refers to `" "``[tab]` refers to `"t"`, and `[LF]` refers to `"n"` for illustrative functions. This doesn’t imply that you should use these placeholders in your answer.

In esoteric language referred to 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]`.
• Legitimate Whitespace quantity should at all times have no less than 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 at all times 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 easy problem, please don’t add another characters within the output.
• On this problem, enter will at all times be a sound unfavourable or optimistic integer.
• In 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 Take a look at 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:].exchange('0', ' ').exchange('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:]
``````

## Take a look at circumstances to validate our answer#

``````def unbleach(ws):
return ws.exchange(' ', '[space]').exchange('t', '[tab]').exchange('n', '[LF]')

take a look at.assert_equals(unbleach(whitespace_number( 1)), unbleach(   " tn"))
take a look at.assert_equals(unbleach(whitespace_number( 0)), unbleach(     " n"))
take a look at.assert_equals(unbleach(whitespace_number(-1)), unbleach(  "ttn"))
take a look at.assert_equals(unbleach(whitespace_number( 2)), unbleach(  " t n"))
take a look at.assert_equals(unbleach(whitespace_number(-3)), unbleach("tttn"))
``````
RELATED ARTICLES