【问题标题】:caught speeding - CodingBat发现超速 - CodingBat
【发布时间】:2020-01-03 10:36:36
【问题描述】:

你们中的大多数人都知道 CodingBat 上的“超速”编码问题。我试图解决它如下。我不太确定为什么它在那里不起作用。我查看了其他解决方案。我相信逻辑是相似的,但在实现上有一些差异。

“你开得太快了,警察拦住了你。编写代码来计算结果,编码为 int 值:0=无票,1=小票,2=大票。如果速度是小于等于60,结果为0。如果速度在61和80之间,结果为1。如果速度大于或等于81,结果为2。除非是你的生日——那天,你的速度可以是在所有情况下都高于 5。”

我的代码:

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

【问题讨论】:

  • 在循环中使用speed = range(0, 81) 有什么意义?速度不应该是单个值而不是序列吗?
  • 也许您应该尝试回答以下问题:它在哪些输入上失败?在这些情况下,结果和预期结果是什么?给定它失败的特定输入,您可以使用调试器或手动跟踪您的代码,看看发生了什么。

标签: python while-loop logic


【解决方案1】:

我在您的代码中添加了一些 cmets:

def caught_speeding(speed, is_birthday): # << You are given the speed as a parameter
  while not is_birthday: # << you're using a while loop like an 'if' statement
    speed = range(0,81) # << so no need to generate a speed (and you probably meant to use random rather than range
    if speed <= 60:
      return 0
    elif speed > 65 and speed < 80: # << this logic does not match the question
      return 1
    else: 
      if speed > 81: # << what about when you are going 80 or 81 mph?
        return 2
  return speed - 5 # << and if it'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. 您将speed 作为输入,然后再次重新分配 speed 在函数内部,它用 a 覆盖整数 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 < 81:
      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 时,就好像实际的speedis_birthday == False 低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 < 81, 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)

【讨论】:

  • 为什么第二次速度检查使用elif,而最后一次使用else ... if?为什么不使用另一个 elif 或只使用 else,因为任何大于 80 的速度都会导致返回 2。
  • 是的,我绝对不需要。我尽可能少地改进 OP 的原始代码。
  • 抱歉,我刚刚意识到您修改了他们的代码,而不是全部重写。
  • 正确,无论如何使用lambdas 可以轻松缩短为3、4 行。
【解决方案3】:
def caught_speeding(speed, is_birthday):
    # speed 60 or less ---> 0
    # b/t 61 and 80 ---> 1
    # 81+ ---> 2
    # if birthday 5+ in all

    if not is_birthday:
        if speed <= 60:
            return 0
        elif speed > 60 and speed <= 80:
            return 1
        elif speed > 80:
            return 2
        else:
            if speed <= 65:
                return 0
    elif speed > 65 and speed <= 85:
        return 1
    elif speed > 85:
        return 2

【讨论】:

    【解决方案4】:

    这对我来说似乎很容易理解:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2015-04-27
      • 2015-03-01
      • 2014-03-14
      • 1970-01-01
      相关资源
      最近更新 更多