#!/usr/bin/env python3 """CC0-1.0: finite VT-code teaching example, not a production decoder.""" import hashlib import itertools import json from pathlib import Path def check_word(word): if not isinstance(word, str) or any(bit not in "01" for bit in word): raise ValueError("expected a binary string") def codewords(n, residue): if type(n) is not int or not 1 <= n <= 10: raise ValueError("enumeration length must be an integer in 1..10") if type(residue) is not int or not 0 <= residue <= n: raise ValueError("residue must be an integer in 0..n") return tuple( "".join(bits) for bits in itertools.product("01", repeat=n) if sum(i * int(bit) for i, bit in enumerate(bits, 1)) % (n + 1) == residue ) def deletions(word): check_word(word) return {word[:i] + word[i + 1:] for i in range(len(word))} def insertions(word): check_word(word) return { word[:i] + bit + word[i:] for i in range(len(word) + 1) for bit in "01" } def deletion_candidates(observed, n, residue): """Recover a binary VT word after one deletion, without a codebook search. Uses Sloane section 2's checksum deficiency. The singleton list preserves the example's API. A returned codeword does not verify the channel model or the supplied length and residue. """ if type(n) is not int or n < 1: raise ValueError("block length must be a positive integer") if type(residue) is not int or not 0 <= residue <= n: raise ValueError("residue must be an integer in 0..n") check_word(observed) if len(observed) != n - 1: raise ValueError("expected exactly one deletion from the known block") weight = checksum = 0 for position, bit in enumerate(observed, 1): if bit == "1": weight += 1 checksum += position deficiency = (residue - checksum) % (n + 1) # A missing zero shifts right-hand ones; a missing one also contributes # its own position. Insert at the first gap with the required prefix count. if deficiency <= weight: missing, counted, remaining = "0", "1", weight - deficiency else: missing, counted, remaining = "1", "0", deficiency - weight - 1 gap = 0 while remaining: if observed[gap] == counted: remaining -= 1 gap += 1 return [observed[:gap] + missing + observed[gap:]] def example(): words = codewords(4, 0) return { "schemaVersion": 1, "evidenceType": "exact finite mathematical example; no media measurements", "sourceSha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "parameters": {"alphabet": [0, 1], "n": 4, "residue": 0, "modulus": 5}, "rows": [ {"codeword": word, "distinctSingleDeletionReads": sorted(deletions(word))} for word in words ], "deletionLocationExample": { "codeword": "0110", "observed": "010", "positionsOneBased": [2, 3] }, "twoDeletionControl": {"codewords": ["0110", "1001"], "sharedRead": "01"}, "substitutionControl": {"codewords": ["0000", "1001"], "sharedRead": "1000"}, "nonbinaryControl": { "codewords": ["213", "132"], "weightedSums": [ sum(i * int(x) for i, x in enumerate(word, 1)) for word in ["213", "132"] ], "sharedRead": "13", "scope": "naive raw-symbol congruence; not a nonbinary VT construction" }, "limits": [ "Known block length and residue; exact binary symbols and one edit.", "No channel distribution, empirical rate, confidence interval or benchmark.", "Enumeration illustrates bounded lengths, not the general coding theorem." ] } if __name__ == "__main__": print(json.dumps(example(), indent=2))