Software Engineering

Tips on how to Convert Integer to Whitespace format in Python

Tips on how to Convert Integer to Whitespace format in Python
Written by admin


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 utilize these placeholders in your answer.

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 can 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 instantly adopted by [LF].

Notes

  • Legitimate Whitespace quantity should all the time have at the least 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 house, tabulation and linefeed are significant characters. All different characters are ignored. Nevertheless, for the needs of this easy problem, please don’t add some other characters within the output.
  • On this problem, enter will all the time be a sound unfavorable or constructive integer.
  • To your comfort, on this problem we are going to use unbleach() operate when evaluating your outcomes. This operate 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:].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:]

Check circumstances to validate our answer

def unbleach(ws):
    return ws.exchange(' ', '[space]').exchange('t', '[tab]').exchange('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"))

About the author

admin

Leave a Comment