Created
April 25, 2012 08:57
-
-
Save rubik/2488320 to your computer and use it in GitHub Desktop.
Split a number in batches
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
''' | |
Split a long number in batches. | |
>>> k = 178684461669052552311410692812805706249615844217278044703496837914086683543763273909969771627106004287604844670397177991379601L | |
>>> print pretty(k) | |
17868446 16690525 52311410 69281280 57062496 15844217 27804470 34968379 | |
14086683 54376327 39099697 71627106 00428760 48446703 97177991 379601 | |
>>> print pretty(k, 3, 9) | |
178 684 461 669 052 552 311 410 692 | |
812 805 706 249 615 844 217 278 044 | |
703 496 837 914 086 683 543 763 273 | |
909 969 771 627 106 004 287 604 844 | |
670 397 177 991 379 601 | |
>>> print pretty(k, 2, 6) | |
17 86 84 46 16 69 | |
05 25 52 31 14 10 | |
69 28 12 80 57 06 | |
24 96 15 84 42 17 | |
27 80 44 70 34 96 | |
83 79 14 08 66 83 | |
54 37 63 27 39 09 | |
96 97 71 62 71 06 | |
00 42 87 60 48 44 | |
67 03 97 17 79 91 | |
37 96 01 | |
''' | |
import itertools | |
def split_columns(iterable, size): | |
c = itertools.count() | |
for k, g in itertools.groupby(iterable, lambda item: c.next() // size): | |
yield list(g) | |
def pretty(number, size=8, per_line=8): | |
groups = split_columns(split_columns(str(number), size), per_line) | |
lines = (' '.join(''.join(group) for group in line) for line in groups) | |
return '\n'.join(lines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment