String Formatting Hackerrank Solution
Given an integer, , print the following values for each integer from to :
- 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 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 .
## Solution:--
def print_formatted(number):
length=len("{0:b}".format(number))
for i in range(1,number+1):
print("{0:{w}d} {0:{w}o} {0:{w}X} {0:{w}b}".format(i, w=length))
Comments
Post a Comment