We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Determine max width (binary of n) →
Loop through numbers →
Convert to all bases →
Align right →
Print in one row.
def print_formatted(number):
width=len(bin(number)[2:])
for i in range (1,number+1):
decimal=str(i)
octal=oct(i)[2:]
hexa=hex(i)[2:].upper()
binary=bin(i)[2:]
print(decimal.rjust(width),
octal.rjust(width),hexa.rjust(width),binary.rjust(width))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
String Formatting
You are viewing a single comment's thread. Return to all comments →
Determine max width (binary of n) → Loop through numbers → Convert to all bases → Align right → Print in one row.
def print_formatted(number):