‹ back to the board

Thursday, September 3

Novicesyntax & basics

The Tollkeeper of Thistle Bridge

The tollkeeper of Thistle Bridge keeps a small ledger nailed beside her stool. It lists what each kind of cart owes to cross: an ox-cart three coins, a hand-barrow one, a pilgrim's pack nothing at all. Anything not written in the ledger is charged the stranger's rate of 5 coins.

This morning a line of carts is waiting, and she wants the day's total before she lifts the bar.

You are given two arguments:

  • tolls — a dictionary (object) mapping a cart type (string) to its toll (a non-negative integer).
  • carts — a list (array) of cart-type strings, in the order they wait at the bridge.

Write bridgeToll(tolls, carts), which returns an integer: the sum of the tolls for every cart in carts. A cart whose type appears in tolls pays exactly the amount listed there. A cart whose type does not appear in tolls pays 5. Every cart in the list pays, even if the same type appears many times. An empty carts list totals 0.

  • dictionary and object access
  • Novice

examples

  1. case 1tolls of {ox: 3, mule: 2}, carts of ["ox", "wagon", "mule"]10 — 3 for the ox, 5 for the unlisted wagon, 2 for the mule
  2. case 2tolls of {pilgrim: 0, ox: 4}, carts of ["pilgrim", "pilgrim", "ox"]4 — a listed toll of zero is still a listed toll, so the pilgrims pay nothing
  3. case 3tolls of {ox: 3}, carts of []0 — nobody is waiting
reward
120 XP
signature
bridgeToll
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • mixed line with one stranger
  • a listed toll of zero
  • empty line at dawn
  • ledger lost, everyone a stranger
  • repeated unknown type
  • single listed cart
The Tollkeeper of Thistle Bridge · bitwick