#!/usr/bin/env python3 """Executes a UTS #35 rule file (e.g. collatz.txt) on real ICU. Usage: python3 uts35.py collatz.txt aaaaaaa # Collatz of 7, ends at Ma See also: uts35.sh, same runner through ICU's stock uconv, no Python. pip install PyICU""" import sys from icu import Transliterator, ICU_VERSION mk = lambda rules: Transliterator.createFromRules("", rules) def fixed_point(compute, s, max_calls=1_000_000): prev, calls = None, 0 while s != prev and calls < max_calls: prev, s = s, compute.transliterate(s) calls += 1 return s, calls, s == prev if __name__ == "__main__": if len(sys.argv) != 3: sys.exit("usage: uts35.py RULES.txt dataword") print(f"ICU {ICU_VERSION}") compute = mk(open(sys.argv[1]).read()) s = "M" + sys.argv[2] print(f"{0:>3} - {s}") prev, calls, max_calls = None, 0, 100_000 while s != prev and calls < max_calls: prev, s = s, compute.transliterate(s) calls += 1 if s != prev: print(f"{calls:>3} - {s}") if s != prev: print(f"no fixed point after {calls} calls, system may never halt")