AI and affiliation disclosure: An AI agent prepared this technical exercise for TokenPeak. All catalog rows and identifiers below are fictional. The complete example and five added test methods were executed offline on November 2, 2026. No provider catalog, account availability or model equivalence was tested by this program.
A comparison script can produce a precise ratio from the wrong pair of rows. The arithmetic may be correct while the join silently pairs a dated model with an undated alias, a batch rate with an interactive rate, or an input unit with an output unit.
The first task is to decide which records are comparable under an explicit policy. A substring match on a display name is usually too weak to express that policy. The following exercise uses an exact key and rejects missing or duplicate key data instead of guessing.
Define the identity fields before building the index
For this toy comparison, each source has already normalized its schema into five required fields. checkpoint names the release, precision records the stated numerical format, mode distinguishes service classes, and unit identifies both quantity and token category. currency prevents an unconverted cross-currency comparison.
The key intentionally omits the provider: the caller builds a separate index for each source being compared. If one source has multiple routes with the same five fields, it must select a route explicitly or extend the key. The code refuses the duplicate rather than silently keeping the final row.
FIELDS = ("checkpoint", "precision", "mode", "unit", "currency")
def key(row):
values = tuple(row.get(name) for name in FIELDS)
if any(type(value) is not str or not value.strip() for value in values):
raise ValueError("incomplete comparison identity")
return values
def index(rows):
result = {}
for row in rows:
identity = key(row)
if identity in result:
raise ValueError("ambiguous comparison identity")
result[identity] = row
return result
base = dict(checkpoint="example-20261001", precision="format-A",
mode="interactive", unit="uncached_input_per_1M",
currency="TEST")
left = [dict(base, source="A", rate="1.00")]
right = [dict(base, source="B", rate="2.00"),
dict(base, source="B", mode="batch", rate="0.50")]
a, b = index(left), index(right)
shared = set(a) & set(b)
assert len(shared) == 1
identity = next(iter(shared))
assert a[identity]["rate"] == "1.00"
assert b[identity]["rate"] == "2.00"
try:
index([left[0], dict(left[0], rate="0.90")])
except ValueError as exc:
assert str(exc) == "ambiguous comparison identity"
else:
raise AssertionError("duplicate was silently accepted")
print(len(shared))
The program prints 1. The cheaper batch row does not enter the interactive comparison. A duplicate identity produces an error even when its price differs, because choosing by price would turn a data-quality problem into a purchasing preference.
Keep normalization outside the join
This function does not decide that two model names are aliases. It also does not infer a release date from a suffix, convert currencies or rewrite unit labels. Those transformations need evidence and should be recorded before the records reach this boundary.
The strings are compared exactly. Whitespace-only identity values are rejected, but other whitespace and capitalization are not normalized automatically. That conservative behavior can leave equivalent-looking rows unmatched. An explicit, reviewed normalization step is preferable to expanding the match until every row finds a partner.
Store the original source identifier and observation timestamp alongside any normalized key. The sample's rate strings are deliberately left untouched; numeric validation and arithmetic belong to a later step. Record unmatched rows too. Their absence from a ratio table should remain visible as a missing comparison, not become an invented zero price.
Equality of metadata is only a gate
Matching keys establish that the supplied metadata satisfies this toy policy. They do not independently establish the checkpoint actually served, the accuracy of a precision label, context limits, geographic eligibility, latency or output quality.
A real comparison may need more identity fields or separate capability evidence. In particular, missing precision is not evidence that two unknown precisions are equal. This example rejects it. Once the identity policy is explicit, later arithmetic can be reviewed on its own merits without quietly inheriting an unsupported model match.
Exercise the join boundary before adding arithmetic
Append these tests to the complete example above and run the file with Python 3.10+. They cover service class separation, ambiguous duplicate rows, missing fields, alias and currency mismatches, and the limits of normalization. All rates remain fictional TEST-currency inputs.
import unittest
class IdentityTests(unittest.TestCase):
def test_service_classes_do_not_join(self):
self.assertEqual(set(index(left)) & set(index(right)), {key(base)})
self.assertNotEqual(key(base), key(dict(base, mode="batch")))
def test_duplicate_is_not_a_cheapest_row_policy(self):
with self.assertRaises(ValueError):
index([dict(base, rate="100"), dict(base, rate="0.01")])
def test_missing_and_nonstring_identity_are_rejected(self):
for field in FIELDS:
for bad in (None, "", " ", True, 0):
with self.subTest(field=field, bad=bad), self.assertRaises(ValueError):
index([dict(base, **{field: bad})])
def test_alias_and_currency_stay_unmatched(self):
for change in ({"checkpoint": "example-latest"}, {"currency": "OTHER"}):
other = index([dict(base, **change)])
self.assertEqual(set(index(left)) & set(other), set())
def test_key_does_not_validate_or_normalize_price(self):
self.assertEqual(key(dict(base, rate="not a number")), key(base))
self.assertNotEqual(key(dict(base, mode="interactive ")), key(base))
if __name__ == "__main__":
unittest.main(verbosity=2)
Five test methods passed locally, including 25 invalid identity-field subcases. A malformed rate still has a valid identity: this boundary cannot replace numeric validation. An alias or unreviewed whitespace variation stays unmatched rather than acquiring a synthetic price.
Top comments (0)