Curriculum

Curriculum

Check the Same Inputs in Two Languages

Scenario

The booking function accepts 1 or more free seats. Run the same checks in Python and JavaScript.

Selected choicePick a choice below
Result—
Python
# booking.py
def can_book(free_seats: int) -> bool:
    return free_seats > 0

assert can_book(1)
assert not can_book(0)
print("OK")

# python3 booking.py
JavaScript
// booking.mjs
import assert from "node:assert/strict";

function canBook(freeSeats) {
  return freeSeats > 0;
}

assert.equal(canBook(1), true);
assert.equal(canBook(0), false);
console.log("OK");

// node booking.mjs

Try it yourself

Predict what both implementations should return.

Reveal the explanation
  1. 1 free seat

    Both return their language’s true boolean.

  2. 0 free seats

    Both return their language’s false boolean.

  3. 2 free seats

    Both should return true. Add this case to each set of checks.

Treat the expected cases as a shared contract. After changing one implementation, rerun the same cases in both.

Shared booking contract

Accept a booking when at least one seat is free.

Read free seats
Is the count greater than zero?
Yes
Accept
No
Decline
Free seatsExpected
0false
1true
2true
Runnable reference files

Your turn

Implement the booking rule in a second language and run the unchanged shared cases.

Common mistake Changing the expected behavior to fit the second implementation.

Check your work

Both implementations pass. One sentence distinguishes what changed in the code from what stayed the same in the rule.