4
I am trying to find the shortest code in python 3, to solve this problem:
You are driving a little too fast, and a police officer stops you.
Write code to take two integer inputs, first one corresponds to speed, seconds one is either 1 or 0, corresponding to True and False respectively to indicate whether it is birthday. Then 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.
input:
60
0
Output:
0
input:
65
0
Output:
1
input:
65
1
output:
0
Here's my shortest solution (57 chars
). Is there any way I can make it shorter in Python?
print(min(2,max(0,(int(input())-5*int(input())-41)//20)))
Reference: Problem inspired from this
@JoKing I did not know, I will add
tips
tag. Not sure if I want this to become a competition. If you recommend I can addcode-golf
as well. – Sayandip Dutta – 2020-01-06T06:29:14.5631What's the range of possible speeds? I think this can have a big effect on what shortcuts are possible. – xnor – 2020-01-06T06:57:50.327
It would be @xnor 0 to 199 – Sayandip Dutta – 2020-01-06T06:59:52.410
Does it have to be in Python 3, or is Python 2 fine as well? I think Python 2 could save quite a number of bytes. – xnor – 2020-01-06T07:07:22.353
Python 3. Will add this in the question/tags. – Sayandip Dutta – 2020-01-06T07:10:12.993
@xnor but I will be interested if there's anything other than
input
that can save bytes in python 2. – Sayandip Dutta – 2020-01-06T07:11:55.5131Other than
input
andprint
without parens, Python 2 hascmp
, which should let you do your method without themin/max
clamping. – xnor – 2020-01-06T07:14:29.113