英文:
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 <= 60:
return 0
elif speed > 65 and speed < 80:
return 1
else:
if speed > 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): # << 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
,这会用一个range
对象覆盖整数。 - 你的检查是错误的,你漏掉了从 61 到 65 的值,因此如果在这个范围内的任何数字作为速度传递给函数,如果
is_birthday
设置为False
,你将陷入无限循环。 - 当
is_birthday
为True
时,你返回的是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_birthday
为 True
时,运行就好像实际的 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.
- You are taking the
speed
as input, then again reassigning
speed
inside the function, which overwrites the integer with a
range
object. - 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 isis_birthday
is
set toFalse
. - When
is_birthday
isTrue
, you are returning thespeed - 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 <= 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
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 > 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)
Or,
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
Or (particular for this case),
def caught_speeding(speed, is_birthday):
speed = speed - 5*is_birthday
return [speed <= 60, 60 < speed <= 80, speed > 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>60)+(s>80)
答案3
得分: 1
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
英文:
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
得分: 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 <=61 + speed_adj:
return 0
if speed <=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 <= 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论