omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / sum-digits-of-an-integer / oracle.py
| 1 | # Independent reference for "Sum digits of an integer" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | import string |
| 4 | DIG = string.digits + string.ascii_lowercase |
| 5 | def digits(n, b): |
| 6 | out = [] |
| 7 | while True: |
| 8 | n, r = divmod(n, b) |
| 9 | out.append(r) |
| 10 | if n == 0: |
| 11 | return out[::-1] |
| 12 | for n, b in [(1, 10), (1234, 10), (0xfe, 16), (0xf0e, 16), (255, 8), (2**64 - 1, 2), (2**64 - 1, 36)]: |
| 13 | ds = digits(n, b) |
| 14 | print(f"{''.join(DIG[d] for d in ds)} (base {b}): digit sum {sum(ds)}") |