2

I'm trying to do this program where given a number N, one has to print out the decimal, octal, hexadecimal and binary for all the numbers in range 1 to N. The trouble is that the platform requires the solution in a particular format.

Suppose the number is 17, so the output should be like :

    1     1     1     1
    2     2     2    10
    3     3     3    11
    4     4     4   100
    5     5     5   101
    6     6     6   110
    7     7     7   111
    8    10     8  1000
    9    11     9  1001
   10    12     A  1010
   11    13     B  1011
   12    14     C  1100
   13    15     D  1101
   14    16     E  1110
   15    17     F  1111
   16    20    10 10000
   17    21    11 10001

For 7 it would be like :

    1     1     1   1
    2     2     2  10
    3     3     3  11
    4     4     4 100
    5     5     5 101
    6     6     6 110
    7     7     7 111

If you notice, the above is required to be printed in a way that the decimal, octal and hexadecimal numbers need a minimum of 2 spaces at their left whereas the binary numbers need at least one space at their left. Now, as the length of the numbers increase the space needs to be given accordingly such that the minimum space is there even for the max length number. So, how do I print them using a variable space? So far I have tried this :

Code

def print_formatted(number):
    space=len(str(bin(number))[2:])
    for i in range(1,number+1):
        print('{:2d}'.format(i), end='')
        print('{:>3s}'.format(str(oct(i))[2:]), end='')
        print('{:>3s}'.format(str(hex(i))[2:]), end='')
        print('{:>'+str(space)+'s}'.format(str(bin(i))[2:]))

print_formatted(17)

Here, I just tried doing the required with just the binary numbers but it's giving me an error

    print('{:>'+str(space)+'s}'.format(str(bin(i))[2:]))
ValueError: Single '}' encountered in format string

Is there any fix/alternative for this?

0

1 Answer 1

3

Your problem is operator order - the + for string concattenation is weaker then the method call in

'{:>' + str(space) + 's}'.format(str(bin(i))[2:])

. Thats why you call the .format(...) only on "s}" - not the whole string. And thats where the

ValueError: Single '}' encountered in format string

comes from.

Putting the complete formatstring into parenthesis before applying .format to it fixes that.

You also need 1 more space for binary and can skip some str() that are not needed:

def print_formatted(number):
    space=len(str(bin(number))[2:])+1     # fix here
    for i in range(1,number+1):
        print('{:2d}'.format(i), end='')
        print('{:>3s}'.format(oct(i)[2:]), end='')
        print('{:>3s}'.format(hex(i)[2:]), end='')
        print(('{:>'+str(space)+'s}').format(bin(i)[2:])) # fix here

print_formatted(17)

Output:

 1  1  1     1
 2  2  2    10
 3  3  3    11
 4  4  4   100
 5  5  5   101
 6  6  6   110
 7  7  7   111
 8 10  8  1000
 9 11  9  1001
10 12  a  1010
11 13  b  1011
12 14  c  1100
13 15  d  1101
14 16  e  1110
15 17  f  1111
16 20 10 10000
17 21 11 10001

From your given output above you might need to prepend this by 2 spaces - not sure if its a formatting error in your output above or part of the restrictions.


You could also shorten this by using f-strings (and removing superflous str() around bin, oct, hex: they all return a strings already).

Then you need to calculate the the numbers you use to your space out your input values:

def print_formatted(number):
    de,bi,oc,he = len(str(number)), len(bin(number)), len(oct(number)), len(hex(number))

    for i in range(1,number+1):
        print(f'  {i:{de}d}{oct(i)[2:]:>{oc}s}{hex(i)[2:]:>{he}s}{bin(i)[2:]:>{bi}s}')

print_formatted(26)

to accomodate other values then 17, f.e. 128:

    1    1   1         1
    2    2   2        10
    3    3   3        11
...
    8   10   8      1000
...
   16   20  10     10000
...
   32   40  20    100000
...
   64  100  40   1000000
...
  128  200  80  10000000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.