1

I am not sure why this code is failing for a few inputs (from CodingBat, link to question : Exercise Link). Problem details are below, I could probably do this question with if elif statements, but I want to use dictionaries. Also, I have read that it is not recommended to fetch key values from a dictionary as used below. But I will appreciate it if the issue in below program can be pointed out.

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.

  • caught_speeding(60, False) → 0
  • caught_speeding(65, False) → 1
  • caught_speeding(65, True) → 0
def caught_speeding(speed, is_birthday):
    Bir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
    NoBir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
    def getKey(dict,value):
        return [key for key in dict.keys() if (dict[key] == value)]
    if is_birthday:
        out1=getKey(Bir_dict,True)
        return out1[0]
    else:
        out2=getKey(NoBir_dict,True)
        return out2[0]

The programs is failing for

caught_speeding(65, False)
caught_speeding(65, True)

And working for

caught_speeding(70, False)
caught_speeding(75, False)
caught_speeding(75, True)
caught_speeding(40, False)
caught_speeding(40, True)
caught_speeding(90, False)
caught_speeding(60, False)
caught_speeding(80, False)

2 Answers 2

2

looks like you mixed Bir_dict and NoBir_dict. Can you try the code below?

def caught_speeding(speed, is_birthday):
        Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
        NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
        def getKey(dict,value):
            return [key for key in dict.keys() if (dict[key] == value)]
        if is_birthday:
            out1=getKey(Bir_dict,True)
            return out1[0]
        else:
            out2=getKey(NoBir_dict,True)
            return out2[0]

Despite it works, I can suggest an alternative way with dictionaries: Ticket definitions do not interfere with each other, in other words, there can only be one True statement within a dictionary. So, you can modify your code as:

def caught_speeding(speed, is_birthday):
        Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
        NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
        def getKey(dict):
            return [key for key in dict.keys() if (dict[key] == True)]
        if is_birthday:
            out1=getKey(Bir_dict)
            return out1[0]
        else:
            out2=getKey(NoBir_dict)
            return out2[0]
Sign up to request clarification or add additional context in comments.

2 Comments

Baa! Thank you. I should take a break.
Appreciate the improvement.
1

Wouldn't the problem consider the Birthday Dictionary to have the higher available values?

If it's my birthday and I'm going 65: I would expect no ticket. However if you generate the dictionaries and print them, you can see that is not the case:

def caught_speeding(speed, is_birthday):
    Bir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
    print Bir_dict
    NoBir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
    print NoBir_dict
    def getKey(dict,value):
        return [key for key in dict.keys() if (dict[key] == value)]
    if is_birthday:
        out1=getKey(Bir_dict,True)
        return out1[0]
    else:
        out2=getKey(NoBir_dict,True)
        return out2[0]

Output:

{0: False, 1: True, 2: False}
{0: True, 1: False, 2: False}
0
{0: False, 1: True, 2: False}
{0: True, 1: False, 2: False}
1

You simply have the values crossed in the dictionary objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.