1

The question, my answer, and the output is screenshotted here

Constraints:

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.

My Code:

def caught_speeding(speed, is_birthday):
  
  if speed > 80 or (speed > 85 and is_birthday):
    return 2
  return int((60 < speed <= 80) or (65 < speed <= 85 and is_birthday))

caught_speeding(65, True) gives 1 but should be 0

caught_speeding(85, True) gives 2 but should be 1

the other tests are ok. When I trace the code I cant seem to find the error

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
Caleb
  • 11
  • 1

2 Answers2

1

For caught_speeding(65, True), the first condition is false so it falls through to the return statement. In that case, (60 < speed <= 80) is true, since 65 falls within that range. The other condition doesn't matter since it's joined with an or, so it's effectively doing return int(True) which is 1.

For caught_speeding(85, True), the speed > 80 condition is true, since 85 is greater than 80. Again, the other condition doesn't matter since it's joined with an or, so it executes the return 2 statement.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0

You can come up with a one-liner solution using the following logic, subtracting 5 from speed when is_birthday parameter gets a True value:

def caught_speeding(speed, is_birthday):
  if is_birthday:
      speed -= 5
  return 0 if speed < 60 else 1 if speed <= 80 and speed >= 61 else 2 if speed >= 81 else 0

print(caught_speeding(81, 0))
print(caught_speeding(65, True))
print(caught_speeding(85, True))

Output:

2
0
1
Cardstdani
  • 4,999
  • 3
  • 12
  • 31
  • what is this syntax called: return 0 ... else 1 ... else 2 ... else 0 I've never seen this before, specifically the else parts of the line – Caleb Jun 21 '22 at 16:40
  • Doesn't answer the question and is pretty long. Do you think a speed of 80.9 shall get unpunished? – Kelly Bundy Jun 21 '22 at 17:35
  • @kelly-bundy You're right, the solution is a little bit long. Also, a speed of 80.9 receives a result of only 1 because that's the rule it has to obey. – Cardstdani Jun 21 '22 at 17:46
  • "little bit" as in the return expression is about three times as long as necessary :-). I meant 80.9 not on the birthday. You return 0 then, not 1. That's why I said unpunished. – Kelly Bundy Jun 21 '22 at 17:50
  • @kelly-bundy Ok, I completely forgot about the birthday component. Thanks for remembering. I will update the answer – Cardstdani Jun 21 '22 at 17:51
  • Wait, what? Your birthday handling seems fine... – Kelly Bundy Jun 21 '22 at 17:53
  • To be clear: I think the speed is given as integer, so 80.9 is invalid. Otherwise the original spec is incomplete. It's just that your checking of lower bounds and your final `else 0` suggest that you think that that `0` can be reached, i.e., that non-integers in the gaps *are* allowed. – Kelly Bundy Jun 21 '22 at 17:59