String Formatting Hacker Rank Solution – Queslers

Problem: String Formatting Hacker Rank 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

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

Input Format

A single integer denoting n.

Constraints

1 <= n <= 99

Output Format

Print n lines where each line i (in the range 1 <= i <= n ) contains the respective decimal, octal, capitalized hexadecimal, and binary values of i. Each printed value must be formatted to the width of the binary value of n.

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 

String Formatting Hacker Rank Solution in Python 2

N = int(raw_input())

width = len(str(bin(N)))-2

for num in range(1,N+1):
    for base in 'doXb':
        print '{0:{width}{base}}'.format(num, base=base, width=width),
    print

String Formatting Hacker Rank Solution in Python 3

N = int(input())
l = len(bin(N)) - 2

for i in range(1, N + 1):
    f = ""
    for c in "doXb":
        if f:
            f += " "
        f += "{:>" + str(l) + c + "}"
    print(f.format(i, i, i, i))

String Formatting Hacker Rank Solution in pypy

n = int(raw_input())
width = len("{0:b}".format(n))
for i in xrange(1,n+1):
    print "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i, width=width)

String Formatting Hacker Rank Solution in pypy3

def print_formatted(number):
    width=len(bin(number))-2
    for num in range(1,number+1): 
        for base in ('d', 'o', 'X', 'b'):
            print("{0:{width}{base}}".format(num, base=base, width=width), end=' ')
        print()
String Formatting Hacker Rank Solution Review:

In our experience, we suggest you solve this String Formatting Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

String Formatting Problem is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

Conclusion:

I hope this String Formatting Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Merge the Tools! Hacker Rank Solution

Text Alignment Hacker Rank Solution

The Minion Game Hacker Rank Solution

itertools.product() Hacker Rank Solution

String Validators Hacker Rank Solution

Leave a Reply

Your email address will not be published.