String Formatting Python Hackerrank solution

Given an integer,n, print the following values for each integer i from 1 to n: Decimal Octal Hexadecimal (capitalized) Binary Function Description
Admin

String Formatting Python Hackerrank solution

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

Decimal

Octal

Hexadecimal (capitalized)

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 i each  from 1 to number. Each value should be space-padded to match the width of the binary value of number and the values should be separated by a single space.

Input Format

A single integer denoting .

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

Solution

def print_formatted(number):
    if 1<=number<=99:
        blen = len(str(format(number,"b")))
        for i in range(1,n+1):
            print("{0:d}".format(i).rjust(blen),end=' ')
            print("{0:o}".format(i).rjust(blen),end=' ')
            print("{0:X}".format(i).rjust(blen),end=' ')
            print("{0:b}".format(i).rjust(blen))


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

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.