4
\$\begingroup\$

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

\$\endgroup\$
7
  • \$\begingroup\$ @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 add code-golf as well. \$\endgroup\$ Jan 6 '20 at 6:29
  • 1
    \$\begingroup\$ What's the range of possible speeds? I think this can have a big effect on what shortcuts are possible. \$\endgroup\$
    – xnor
    Jan 6 '20 at 6:57
  • \$\begingroup\$ It would be @xnor 0 to 199 \$\endgroup\$ Jan 6 '20 at 6:59
  • \$\begingroup\$ 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. \$\endgroup\$
    – xnor
    Jan 6 '20 at 7:07
  • 1
    \$\begingroup\$ Other than input and print without parens, Python 2 has cmp, which should let you do your method without the min/max clamping. \$\endgroup\$
    – xnor
    Jan 6 '20 at 7:14
15
\$\begingroup\$

Python 3, 32 bytes

lambda a,b:(a-b*5>60)+(a-b*5>80)

Try it online!


Python 3, 50 49 bytes

If io must be done with stdin and stdout

a,b=map(int,open(0));print((a-b*5>60)+(a-b*5>80))

Try it online!

-1 byte thanks to @dingledooper

\$\endgroup\$
8
  • \$\begingroup\$ Ah! Beautiful! Thanks! Would accept in a while. \$\endgroup\$ Jan 6 '20 at 7:00
  • \$\begingroup\$ Save some characters by doing i=input; (-3) and then later i() (+8) for a total of +5 characters saved. \$\endgroup\$
    – LMD
    Jan 6 '20 at 23:14
  • 1
    \$\begingroup\$ Do you mean like i=input;s=int(i())-5*int(i());print((s>60)+(s>80))? \$\endgroup\$ Jan 7 '20 at 3:07
  • \$\begingroup\$ @LMD could you show what you did? I tried this yesterday, couldn't shrink below 50 bytes. \$\endgroup\$ Jan 7 '20 at 6:31
  • \$\begingroup\$ @SayandipDutta, yeah, like Mukundan showed \$\endgroup\$
    – LMD
    Jan 7 '20 at 16:02
2
\$\begingroup\$

Python 3, 51 45 bytes

lambda a,b:min(2,max(0,(a//1-5*b//1-41)//20))

Try it online!

I was able to shave 6 bytes from your approach by using everyone's favourite python golfing keyword: lambda.

This turns your program into an anonymous function, which then can be called in the footer of a program.

Edit: I know that this question is way old, but I only just recently thought of using //1 to convert things to an integer instead of using int(...).

\$\endgroup\$
3
  • \$\begingroup\$ Thanks. But need to take input, and print as well. Meant to be a script, can't eval on repl :\ \$\endgroup\$ Jan 6 '20 at 6:43
  • \$\begingroup\$ @SayandipDutta Functions are usually considered valid responses here \$\endgroup\$
    – Citty
    Jan 7 '20 at 13:35
  • 1
    \$\begingroup\$ I'm sure they are. It's just that it didn't suit my usecase. I hope you understand :) \$\endgroup\$ Jan 7 '20 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.