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.
- Prepare
- Python
- Strings
- String Formatting
- Discussions
String Formatting
String Formatting
Sort by
recency
|
1744 Discussions
|
Please Login in order to post a comment
Very Sneaky this one:
Hello Everybody ; In my code you dont have to use "rjust" or 50+ lines of code ,mine is just a pure concepts with few lines .If you do like it upvote me
def print_formatted(number):
if name == 'main': n = int(input()) print_formatted(n)
def octal(number): nm = number st = '' while nm > 0: st += str(nm % 8) nm //= 8
if st: return st[::-1] return '0'
def binary(number): nm = number st = '' while nm > 0: st += str(nm % 2) nm //= 2 if st: return st[::-1] return '0'
def hexadecimal(number): nm = number st = '' digit = '0123456789ABCDEF' while nm > 0: st += digit[nm % 16] nm //= 16 if st: return st[::-1] return '0'
def print_formatted(n): width = len(bin(n)[2:]) # max width of binary for i in range(1, n+1): deci = str(i).rjust(width) octa = octal(i).rjust(width) hexa = hexadecimal(i).rjust(width) bina = binary(i).rjust(width) print(deci, octa, hexa, bina) if name == 'main': n = int(input()) print_formatted(n)
Not that easy
This was challenging task.