bhylu.com

Advertisements

Code Challenge

Recognise a reserve command carrying a positive integer seat count. Give unsupported shapes a defined fallback.

Understand the idea

Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

Your task

Recognise a reserve command carrying a positive integer seat count. Give unsupported shapes a defined fallback.

Show the worked solution
Python

command = ("reserve", 2)
match command:
    case ("reserve", int(seats)) if seats > 0:
        print(seats)
    case _:
        print("Unsupported")

Check the result

The valid command prints 2.

Test one more case

Test a different command name, missing count, and invalid negative count.