Question: Python String Formatting [Strings]
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 each i from 1 to n. 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 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
Possible Solutions
Now let us go through the possible solutions for the given problem. The following code is already given in the editor of the Hacker Rank.
def print_formatted(number):
# your code goes here
if __name__ == '__main__':
n = int(input())
print_formatted(n)
We have to complete the function.
Solution-1: Using for loop
Let us first solve the problem using for loop and then we will explain the code.
def print_formatted(number):
pad = number.bit_length()
for i in range(1,number+1):
print(str(i).rjust(pad),oct(i).split("o")[1].rjust(pad),hex(i).split("x")[1].upper().rjust(pad),bin(i).split("b")[1].rjust(pad))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
This solution defines a function 'print_formatted
' which takes an integer 'number
' as input and prints the decimal, octal, hexadecimal, and binary representation of the integers from 1 to 'number
' in a formatted way, with each representation aligned vertically. The width of the output is determined by the number of bits required to represent the largest number, 'number
', and all the other representations are padded with spaces to match this width. If this code is run as the main program, it takes an input 'n
' and calls the 'print_formatted
' function with 'n
' as its argument.
Solution-2: Formating the numbers
Let us now use the format method to solve the given problem.
def print_formatted(number):
nbin = format(number,'b')
size = len(nbin)
for i in range(1,n+1):
octa = format(i,'o')
hexa = format(i,'X')
bina = format(i,'b')
print(str(i).rjust(size),str(octa).rjust(size),str(hexa).rjust(size),str(bina).rjust(size))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
This code defines a function 'print_formatted
' that takes an integer 'number
' as input and prints the decimal, octal, hexadecimal, and binary representations of the integers from 1 to 'number
', each representation aligned vertically. The function first calculates the binary representation of 'number
' and determines the number of bits required to represent it. It then formats each integer from 1 to 'number
' in octal, hexadecimal, and binary representation using the 'format' function, with each representation padded with spaces to match the width determined by the binary representation of 'number
'. The function finally prints the decimal, octal, hexadecimal, and binary representations of each integer from 1 to 'number
' in a formatted way, with each representation aligned vertically.
Solution-3: Alternative method
In Python, we can use the same code in various ways. Let us solve the given problem using a different method and then will explain the code in detail.
def print_formatted(upto: int):
w = upto.bit_length()
for num in range(1, upto + 1):
print(f"{num:>{w}d} {num:>{w}o} {num:>{w}X} {num:>{w}b}")
if __name__ == '__main__':
n = int(input())
print_formatted(n)
similar to other solutions, this code defines a function 'print_formatted' that takes an integer 'upto' as input and prints the decimal, octal, hexadecimal, and binary representations of the integers from 1 to 'upto
', each representation aligned vertically. Unlike the previous code, this code uses the format specifier 'f-string
' to format the output in a more concise way. It first calculates the number of bits required to represent 'upto
' using the 'bit_length
' method, and then uses this value to determine the width of each representation. The format specifier ':>{w}d
' represents decimal representation, ':>{w}o
' represents octal representation, ':>{w}X
' represents hexadecimal representation, and ':>{w}b
' represents binary representation. This code uses the format specifier in each print statement to format the output and align each representation vertically.
Summary
In this short article, we discussed how we can solve the String Format problem on Hacker Rank. We solved the problem using three different methods and explained each of them.
Further Readings
Question on Hacker Rank: String Formatting [Python Strings]