Input Format
A single integer denoting .
Constraints
Output Format
Print  lines where each line  (in the range ) contains the respective decimal, octal, capitalized hexadecimal, and binary values of . Each printed value must be formatted to the width of the binary value of .

hackerrank string formatting problem solution in python programming

Problem solution in python 2 programming language.

def fun(N):
    width = len(bin(N)) - 2
    for i in range(1,N+1):
        print "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = width)

n = input()
fun(n)

Second solution

n = int(raw_input())
spacing = len(bin(n)[2:])

for i in range(1,n+1):
    print str(i).rjust(spacing, ' '),str(oct(i)[1:]).rjust(spacing, ' '),str(hex(i)[2:].upper()).rjust(spacing, ' '),str(bin(i)[2:]).rjust(spacing, ' ')

Problem solution in python 3 programming language.

def fun(N):
    width = len(bin(N)) - 2
    for i in range(1,N+1):
        print("{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = width))

n = input()
fun(n)

Second solution

n = int(input())
spacing = len(bin(n)[2:])

for i in range(1,n+1):
    print(str(i).rjust(spacing, ' '),str(oct(i)[1:]).rjust(spacing, ' '),str(hex(i)[2:].upper()).rjust(spacing, ' '),str(bin(i)[2:]).rjust(spacing, ' '))