This Python program is designed to take a number as input and print its decimal, octal, hexadecimal, and binary representations in a formatted manner. The width of the printed numbers is determined by the binary representation of the input number. This is a common task in programming, especially when dealing with different number systems.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution works by first determining the width of the binary representation of the input number. It then iterates from 1 to the input number, converting each iteration value to its octal, hexadecimal, and binary representations. The hexadecimal representation is converted to uppercase. Each of these values, along with the decimal representation, is then printed in a formatted manner, right-aligned with the determined width. The formatting is done using Python's f-string formatting.
def print_formatted(number):
width = len(bin(number)[2:])
for i in range(1, number + 1):
o = oct(i)[2:]
h = hex(i)[2:]
h = h.upper()
b = bin(i)[2:]
d = str(i)
print(f"{d:>{width}} {o:>{width}} {h:>{width}} {b:>{width}}")
if __name__ == "__main__":
n = int(input())
print_formatted(n)
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.