#!/usr/bin/env python3 """Verify exact conversions of whole numbers across bases 10, 2, 12, and 60.""" from __future__ import annotations DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def to_base(n: int, base: int) -> str: if n == 0: return "0" rem, out = n, [] while rem > 0: rem, r = divmod(rem, base) out.append(DIGITS[r]) return "".join(reversed(out)) def to_sexagesimal(n: int) -> str: if n == 0: return "0" rem, places = n, [] while rem > 0: rem, r = divmod(rem, 60) places.append(r) places.reverse() return ":".join(f"{p:02d}" if i > 0 else str(p) for i, p in enumerate(places)) def from_base(s: str, base: int) -> int: total = 0 for ch in s: total = total * base + DIGITS.index(ch) return total def main() -> None: quantities = [13, 47, 60, 255] print(f"{'quantity':>8} {'base_10':>7} {'base_2 (binary)':>15} {'base_12':>7} {'base_60 (sexagesimal)':>21}") for q in quantities: b10 = to_base(q, 10) b2 = to_base(q, 2) b12 = to_base(q, 12) b60 = to_sexagesimal(q) assert from_base(b10, 10) == q assert from_base(b2, 2) == q assert from_base(b12, 12) == q print(f"{q:8d} {b10:>7} {b2:>15} {b12:>7} {b60:>21}") assert 1 * 10 + 3 == 13 assert 1 * 12 + 1 == 13 assert 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 == 13 print("\ncheck_13: 1*10+3 == 1*12+1 == 1*8+1*4+0*2+1*1 == 13") if __name__ == "__main__": main()