Port One Rule Across Python, JavaScript, and Go
Scenario
Port canBookGroup(freeSeats, requested, hasPermission) across Python, JavaScript, and Go.
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_seatsexport 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;
}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.
| Case | Result |
|---|---|
| freeSeats = 4, requested = 4, hasPermission = true | Select a choice above to see what happens. |
| freeSeats = 4, requested = 5, hasPermission = true | Select a choice above to see what happens. |
| freeSeats = 4, requested = 0, hasPermission = true | Select a choice above to see what happens. |
Reveal the explanation
- freeSeats = 4, requested = 4, hasPermission = true
All 3 return true (exact capacity fit: requested <= freeSeats).
- freeSeats = 4, requested = 5, hasPermission = true
All 3 return false (1 seat over capacity).
- 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
This browser could not save your progress. Changes last until you leave the page.