caught speeding – CodingBat

huangapple go评论72阅读模式
英文:

caught speeding - CodingBat

问题

大多数人都知道在CodingBat上的“超速被抓”编码问题。我已经尝试解决如下。我不太确定为什么它在那里没有工作。我查看了其他解决方案,并且我相信逻辑是相似的,但在实现上有一些差异。

我的代码:

def caught_speeding(speed, is_birthday):
  while not is_birthday:
    if speed <= 60:
      return 0
    elif speed > 65 and speed <= 80:
      return 1
    else: 
      if speed > 80:
        return 2
  return speed - 5

希望这有助于解决你的问题。

英文:

most of you are aware of the 'caught speeding' coding problem on CodingBat. I have tried to solve it as below. i am not quite sure why it did not work there. i have viewed other solutions. and i believe the logic is similar but with some differences in implementation.

"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."

my code:

def caught_speeding(speed, is_birthday):
  while not is_birthday:
    speed = range(0,81)
    if speed &lt;= 60:
      return 0
    elif speed &gt; 65 and speed &lt; 80:
      return 1
    else: 
      if speed &gt; 81:
        return 2
  return speed - 5

答案1

得分: 2

我已经对你的代码添加了一些注释:

def caught_speeding(speed, is_birthday): 
  while not is_birthday: 
    speed = range(0, 81) 
    if speed <= 60:
      return 0
    elif speed > 65 and speed < 80: 
      return 1
    else: 
      if speed > 81: 
        return 2
  return speed - 5 

我已经翻译好了,没有其他内容。

英文:

I've added some comments to your code:

def caught_speeding(speed, is_birthday): # &lt;&lt; You are given the speed as a parameter
  while not is_birthday: # &lt;&lt; you&#39;re using a while loop like an &#39;if&#39; statement
    speed = range(0,81) # &lt;&lt; so no need to generate a speed (and you probably meant to use random rather than range
    if speed &lt;= 60:
      return 0
    elif speed &gt; 65 and speed &lt; 80: # &lt;&lt; this logic does not match the question
      return 1
    else: 
      if speed &gt; 81: # &lt;&lt; what about when you are going 80 or 81 mph?
        return 2
  return speed - 5 # &lt;&lt; and if it&#39;s your birthday it means you can add 5 mph to the speeds before you get a larger ticket. You are just subtracting 5

答案2

得分: 1

以下是代码部分的翻译:

  1. 你正在将 speed 作为输入,然后在函数内部再次赋值
    speed,这会用一个 range 对象覆盖整数。
  2. 你的检查是错误的,你漏掉了从 61 到 65 的值,因此如果在这个范围内的任何数字作为速度传递给函数,如果 is_birthday 设置为 False,你将陷入无限循环。
  3. is_birthdayTrue 时,你返回的是 speed - 5 而不是他即将得到的罚单。

因此,你可以尝试这个修复后的代码:

def caught_speeding(speed, is_birthday):
  while not is_birthday:
    if speed <= 60:
      return 0
    elif speed > 60 and speed <= 80:
      return 1
    else: 
      if speed > 80:
        return 2
  return caught_speeding(speed - 5, False)

print(caught_speeding(64, False))
# 1
print(caught_speeding(64, True))
# 0

在这里,我已经修复了前两个问题,如你所期望的那样,并对于最后一个问题,使用了递归。因此,当 is_birthdayTrue 时,运行就好像实际的 speed 低5,is_birthday == False

如果你想学点新东西(假设你还不知道):

def caught_speeding(speed, is_birthday):
    two = lambda x: 2 if x > 80 else one(x)
    one = lambda x: 1 if 60 < x <= 80 else zero(x)
    zero = lambda x: 0 if x <= 60 else two(x)
    return two(speed - 5*is_birthday)

或者,

def caught_speeding(speed, is_birthday):
    d = {0: lambda x: x <= 60, 1: lambda x: 60 < x <= 80, 2: lambda x: x > 80}
    for ticket, condition in d.items():
        if condition(speed - 5*is_birthday): return ticket

或者(特别适用于这种情况),

def caught_speeding(speed, is_birthday):
    speed = speed - 5*is_birthday
    return [speed <= 60, 60 < speed <= 80, speed > 80].index(True)

或者(特别适用于这种情况),

def caught_speeding(speed, is_birthday):
    return min(max(0,(speed - 5*is_birthday-41)//20),2)

或者(特别适用于这种情况),

def caught_speeding(speed, is_birthday):
    s = speed-5*is_birthday
    return (s>60)+(s>80)
英文:

There are multiple issues with the code, but can be fixed without too much work.

  1. You are taking the speed as input, then again reassigning
    speed inside the function, which overwrites the integer with a
    range object.
  2. Your checking is wrong, you are missing the values from 61
    to 65, hence if any number in this range is given as speed to the
    function, you will be stuck in an infinite loop is is_birthday is
    set to False.
  3. When is_birthday is True, you are returning the speed - 5 not the ticket he is going to get.

So, you may try this:

def caught_speeding(speed, is_birthday):
  while not is_birthday:
    if speed &lt;= 60:
      return 0
    elif speed &gt; 60 and speed &lt; 81:
      return 1
    else: 
      if speed &gt; 80:
        return 2
  return caught_speeding(speed - 5, False)

print(caught_speeding(64, False))
# 1
print(caught_speeding(64, True))
# 0

Here I have fixed the first two problems as you would expect, and for the last problem, used recursion. So when is_birthday is True, run as if the actual speed is 5 lower and is_birthday == False.

If you want to learn something new (assuming you don't know already):

def caught_speeding(speed, is_birthday):
    two = lambda x: 2 if x &gt; 80 else one(x)
    one = lambda x: 1 if 60 &lt; x &lt;= 80 else zero(x)
    zero = lambda x: 0 if x &lt;= 60 else two(x)
    return two(speed - 5*is_birthday)

Or,

def caught_speeding(speed, is_birthday):
    d = {0: lambda x: x &lt;= 60, 1: lambda x: 60 &lt; x &lt; 81, 2: lambda x: x &gt; 80}
    for ticket, condition in d.items():
        if condition(speed - 5*is_birthday): return ticket

Or (particular for this case),

def caught_speeding(speed, is_birthday):
    speed = speed - 5*is_birthday
    return [speed &lt;= 60, 60 &lt; speed &lt;= 80, speed &gt; 80].index(True)

Or (particular for this case),

def caught_speeding(speed, is_birthday):
    return min(max(0,(speed - 5*is_birthday-41)//20),2)

Or (particular for this case),

def caught_speeding(speed, is_birthday):
    s = speed-5*is_birthday
    return (s&gt;60)+(s&gt;80)

答案3

得分: 1

def caught_speeding(speed, is_birthday):
    # speed 60 or less ---&gt; 0
    # b/t 61 and 80 ---&gt; 1
    # 81+ ---&gt; 2
    # if birthday 5+ in all

    if not is_birthday:
        if speed &lt;= 60:
            return 0
        elif speed &gt; 60 and speed &lt;= 80:
            return 1
        elif speed &gt; 80:
            return 2
        else:
            if speed &lt;= 65:
                return 0
    elif speed &gt; 65 and speed &lt;= 85:
        return 1
    elif speed &gt; 85:
        return 2
英文:
def caught_speeding(speed, is_birthday):
    # speed 60 or less ---&gt; 0
    # b/t 61 and 80 ---&gt; 1
    # 81+ ---&gt; 2
    # if birthday 5+ in all

    if not is_birthday:
        if speed &lt;= 60:
            return 0
        elif speed &gt; 60 and speed &lt;= 80:
            return 1
        elif speed &gt; 80:
            return 2
        else:
            if speed &lt;= 65:
                return 0
    elif speed &gt; 65 and speed &lt;= 85:
        return 1
    elif speed &gt; 85:
        return 2

答案4

得分: 0

这对我来说很容易理解:

def caught_speeding(speed, is_birthday):
       
  speed_adj = 0
  
  if is_birthday:
    speed_adj = 5
  if speed <= 61 + speed_adj:
    return 0
  if speed <= 80 + speed_adj:
    return 1
  return 2
英文:

This seems easy to understand for me:

def caught_speeding(speed, is_birthday):
               
  speed_adj=0
  
  if is_birthday:
    speed_adj=5
  if speed &lt;=61 + speed_adj:
    return 0
  if speed &lt;=80 + speed_adj:
    return 1
  return 2

答案5

得分: 0

我的代码运行正常。

def caught_speeding(speed, is_birthday):
    if (is_birthday == False and speed <= 60):
        return 0
    elif (is_birthday == False and (speed > 60 and speed <= 80)):
        return 1    
    elif (is_birthday == False and speed > 80):
        return 2
    elif (is_birthday == True and speed <= 65):
        return 0
    elif (is_birthday == True and (speed > 65 and speed <= 85)):
        return 1
    elif (is_birthday == True and speed > 85):
        return 2
    else:
        pass
英文:

my code and it's working fine.

def caught_speeding(speed, is_birthday):

    if (is_birthday == False and speed &lt;= 60):
        return 0
    elif (is_birthday == False and (speed &gt; 60 and speed &lt;= 80)):
        return 1    
    elif (is_birthday == False and speed &gt; 80):
        return 2
    elif (is_birthday == True and speed &lt;= 65):
        return 0
    elif (is_birthday == True and (speed &gt; 65 and speed &lt;= 85)):
        return 1
    elif (is_birthday == True and speed &gt; 85):
        return 2
    else:
        pass

huangapple
  • 本文由 发表于 2020年1月3日 18:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定