Python-HackerRank-String Formatting
def print_formatted(number): results = [] for i in range(1, n+1): decimal = str(i) octal = str(oct(i)[2:]) hex_ = str(hex(i)[2:]).upper() binary = str(bin(i)[2:])
results.append([decimal, octal, hex_, binary]) # print(results)
width = len(results[-1][3]) # print(width) # largest binary number
for i in results: print(*(rep.rjust(width) for rep in i))if __name__ == '__main__': n = int(input()) print_formatted(n)
def print_formatted(number):
results = []
for i in range(1, n+1):
decimal = str(i)
octal = str(oct(i)[2:])
hex_ = str(hex(i)[2:]).upper()
binary = str(bin(i)[2:])
results.append([decimal, octal, hex_, binary])
# print(results)
width = len(results[-1][3])
# print(width) # largest binary number
for i in results:
print(*(rep.rjust(width) for rep in i))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
Comments
Post a Comment