Remove a requirement: the rule that came free
Why it matters Specs are written by people listing worries, not proving theorems. Two rules often say the same thing in different words, and each gets its own code.
A payments import reads a stream of ledger entries from a partner and must accept or reject each one as it arrives. The partner's spec lists four rules for an entry to be accepted:
centsis not zero.idis greater than the id of the last accepted entry.- No accepted
idappears twice. - The running total of accepted cents never goes below zero.
A rejected entry changes nothing: it does not count as the last entry and does not touch the total. The validator lives inside a long-running import worker that sees millions of entries a day.
v = ImportValidator()
v.feed({"id": 5, "account": "a", "cents": 100}) # -> True
v.feed({"id": 5, "account": "a", "cents": 100}) # -> FalseWrite a class ImportValidator whose feed(entry) returns True if the entry is accepted under all four rules and False if not. Entries are dicts with "id" (int), "account" and "cents" (int). Memory rule: after 100,000 entries the validator may hold at most 20 items, counted by kept(validator).
Spotted a problem in this lesson? Report it · Ask about this lesson
This exercise opens soon
Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.
This browser is blocking cookies for learn.floati.life, so lessons you finished here cannot open in order. Allow cookies for this site and reload.