146k views
4 votes
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.

A caughtSpeeding(60, false) → 0
B caughtSpeeding(65, false) → 1
C caughtSpeeding(65, true) → 0

User Interjay
by
7.9k points

1 Answer

5 votes

Final answer:

The student's question involves creating a Python function that computes if a driver receives a speeding ticket based on their speed and whether it is their birthday, adjusting the speed limit allowance if it is their birthday.The code is

def caughtSpeeding(speed, is_birthday):
speed_adjust = 5 if is_birthday else 0
if speed <= 60 + speed_adjust:
return 0
elif speed <= 80 + speed_adjust:
return 1
else:
return 2

Step-by-step explanation:

The student's question involves writing code to compute whether the driver gets a speeding ticket (encoded as an int value), based on their speed and whether it's their birthday. The possible results are 0 (no ticket), 1 (small ticket), and 2 (big ticket). The following Python function demonstrates how to make this computation:

def caughtSpeeding(speed, is_birthday):
speed_adjust = 5 if is_birthday else 0
if speed <= 60 + speed_adjust:
return 0
elif speed <= 80 + speed_adjust:
return 1
else:
return 2

For the given cases:

  • A: caughtSpeeding(60, false) results in 0 (no ticket)
  • B: caughtSpeeding(65, false) results in 1 (small ticket)
  • C: caughtSpeeding(65, true) results in 0 (no ticket, because of the birthday allowance)

User Sunil Johnson
by
8.1k points