-
-
Save skeptycal/7871dd32a0000d3c8ff4f4de8b81057d to your computer and use it in GitHub Desktop.
Pretty print large base 10 numbers and base 2 numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Take a very large number and pretty print it in triplets of 3 digits, each triplet separated by a space. | |
def pnum_spc(n): print(' '.join([''.join(list(str(n))[::-1][i:i+3]) for i in range(0, len(str(n)), 3)][::-1])) | |
# >>> pnum_spc(32 ** 13) | |
# 36 893 488 147 419 103 232 | |
# Print numbers as 32-bit binary numbers w/ spaces giving 4-bit words | |
def pbin_spc(n): print(' '.join([''.join(list(f'{n:032b}')[::-1][i:i+4][::-1]) for i in range(0, len(f'{n:032b}'), 4)][::-1])) | |
# >>> "{0:032b}".format(1234) | |
# '00000000000000000000010011010010' | |
# >>> pbin_spc(1234) | |
# 0000 0000 0000 0000 0000 0100 1101 0010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment