String Formatting Python HackerRank solution

String Formatting Python HackerRank solution
String Formatting Python HackerRank solution

Given an integer, n, print the following values for each integer i from 1 to n :

  1. Decimal
  2. Octal
  3. Hexadecimal (capitalized)
  4. Binary

Function Description

Complete the print_formatted function in the editor below.

print_formatted has the following parameters:

  • int number: the maximum value to print

Prints

The four values must be printed on a single line in the order specified above for each i from 1 to number . Each value should be space-padded to match the width of the binary value of  and the values should be separated by a single space.

Input Format

A single integer denoting n.

Constraints

  • 1<=n<=99

Sample Input

17

Sample 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

Code:

#String Formatting python HackerRank solution

def print_formatted(number):
    # your code goes here
    a=len(bin(number)[2:])
    for i in range(1,n+1):
        print(str(i).rjust(a,' '),end=" ")
        print(oct(i)[2:].rjust(a,' '),end=" ")
        print((hex(i)[2:].upper()).rjust(a,' '),end=" ")
        print(bin(i)[2:].rjust(a,' '),end=" ")
        print("")

if __name__ == '__main__':
    n = int(raw_input())
    print_formatted(n)

#String Formatting python HackerRank solution

Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment