HackerRankInterview QuestionsPythonPython InterviewRandom

Hackerrank String Formatting solution in python | python question solution

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

  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  from  to . 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 .

Constraints

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
In this HackerRank String Formatting problem in python we need to code a program in which we can accept an integer input n that's greater than 1. and on the output screen, we need to print out the binary, decimal, hexadecimal and octal number of that given integer.

Problem solution in Python 2 programming.

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

Problem solution in Python 3 programming.

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))

Problem solution in PyPy programming.

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)

Problem solution in pypy3 programming.

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()


Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science