Curriculum

Curriculum

Port One Rule Across Python, JavaScript, and Go

Scenario

Port canBookGroup(freeSeats, requested, hasPermission) across Python, JavaScript, and Go.

Selected choicePick a choice below
Result—
Python
def can_book_group(free_seats: int, requested: int, has_permission: bool) -> bool:
    if any(isinstance(v, bool) or not isinstance(v, int) for v in (free_seats, requested)) or free_seats < 0 or requested <= 0 or not isinstance(has_permission, bool):
        raise ValueError("INVALID_GROUP_BOOKING")
    return has_permission and requested <= free_seats
JavaScript
export function canBookGroup(freeSeats, requested, hasPermission) {
  if (!Number.isInteger(freeSeats) || !Number.isInteger(requested) || freeSeats < 0 || requested <= 0 || typeof hasPermission !== "boolean") {
    throw new RangeError("INVALID_GROUP_BOOKING");
  }
  return hasPermission && requested <= freeSeats;
}
Go
func canBookGroup(freeSeats int, requested int, hasPermission bool) (bool, error) {
	if freeSeats < 0 || requested <= 0 {
		return false, errors.New("INVALID_GROUP_BOOKING")
	}
	return hasPermission && requested <= freeSeats, nil
}

Try it yourself

Compare each shared contract case across all three implementations.

Reveal the explanation
  1. freeSeats = 4, requested = 4, hasPermission = true

    All 3 return true (exact capacity fit: requested <= freeSeats).

  2. freeSeats = 4, requested = 5, hasPermission = true

    All 3 return false (1 seat over capacity).

  3. freeSeats = 4, requested = 0, hasPermission = true

    All 3 reject with INVALID_GROUP_BOOKING (group size must be >= 1).

Syntax changes across languages, but shared fixtures stay constant. When all three pass the same JSON table, your port is verified.

Review first
Download shared test cases

Your turn

Add a fourth case checking freeSeats = 4, requested = 2, hasPermission = false and verify all three snippets return false.

Common mistake Allowing requested = 0 in one language while rejecting it in the other two.

Check your work

Python -> False, JS -> false, Go -> (false, nil).

Quick check

What is the most reliable way to prove Python, JavaScript, and Go implementations share the same behavior?
Show the answer and reasoning

Correct answer Run the exact same typical, boundary, and invalid fixtures against all three

Shared contract fixtures verify identical runtime behavior regardless of syntax conventions.