[英]Make Bricks Python
I'm trying to solve this question:我正在尝试解决这个问题:
We want to make a row of bricks that is goal inches long.
我们想制作一排目标英寸长的砖块。 We have a number of small bricks (1 inch each) and big bricks (5 inches each).
我们有一些小砖块(每块 1 英寸)和大砖块(每块 5 英寸)。 Return True if it is possible to make the goal by choosing from the given bricks.
如果可以通过从给定的积木中进行选择来实现目标,则返回 True。 This is a little harder than it looks and can be done without any loops.
这比看起来要难一点,并且可以在没有任何循环的情况下完成。
make_bricks(3, 1, 8) → True make_bricks(3, 1, 9) → False make_bricks(3, 2, 10) → True
For that, I made this code:为此,我制作了这段代码:
def make_bricks(small, big, goal):
tam = small + big*5
ex_gran = goal - small
if goal <= small:
return True
elif ex_gran > big*5:
return False
elif ex_gran <= big * 5 and (ex_gran % 5 <= small) :
return True
else:
return False
And this is the result os the tests:这是测试的结果:
Expected Run
make_bricks(3, 1, 8) → True True OK
make_bricks(3, 1, 9) → False False OK
make_bricks(3, 2, 10) → True True OK
make_bricks(3, 2, 8) → True True OK
make_bricks(3, 2, 9) → False True X
make_bricks(6, 1, 11) → True True OK
make_bricks(6, 0, 11) → False False OK
make_bricks(1, 4, 11) → True True OK
make_bricks(0, 3, 10) → True True OK
make_bricks(1, 4, 12) → False True X
make_bricks(3, 1, 7) → True False X
make_bricks(1, 1, 7) → False False OK
make_bricks(2, 1, 7) → True True OK
make_bricks(7, 1, 11) → True True OK
make_bricks(7, 1, 8) → True True OK
make_bricks(7, 1, 13) → False False OK
make_bricks(43, 1, 46) → True True OK
make_bricks(40, 1, 46) → False False OK
make_bricks(40, 2, 47) → True True OK
make_bricks(40, 2, 50) → True True OK
make_bricks(40, 2, 52) → False False OK
make_bricks(22, 2, 33) → False False OK
make_bricks(0, 2, 10) → True True OK
make_bricks(1000000, 1000, 1000100) → True True OK
make_bricks(2, 1000000, 100003) → False True X
make_bricks(20, 0, 19) → True True OK
make_bricks(20, 0, 21) → False False OK
make_bricks(20, 4, 51) → False False OK
make_bricks(20, 4, 39) → True True OK
Only 4 of them was wrong, but I still can't figure it out the error.其中只有 4 个是错误的,但我仍然无法弄清楚错误。
What is wrong?怎么了?
Try first to use as many big bricks as possible, then complete with the small ones.首先尝试使用尽可能多的大积木,然后再使用小积木。 Note that this works because the size of the big ones is a multiple of the size of the small ones, you would need another approach if the sizes were for example 2 and 5.
请注意,这是有效的,因为大的大小是小的大小的倍数,如果大小是例如 2 和 5,则您将需要另一种方法。
def make_bricks(small, big, goal):
max_big = goal // 5 # max number of big we can use
nb_big = min(big, max_big) # big ones we really use
return small >= goal - 5 * nb_big # True if we have enough small ones to complete
Error is simple:) it lies on line 16. Although the code could be shorten a lot.错误很简单:) 它位于第 16 行。尽管代码可以缩短很多。
First if all, notice that condition ex_gran <= big * 5
is irrelevant since the conditional before was already false ( elif ex_gran > big*5:
) and we are assuming small, big and goal are all integers, so you can take that away from the code.首先,请注意条件
ex_gran <= big * 5
是无关紧要的,因为之前的条件已经为假( elif ex_gran > big*5:
)并且我们假设 small、big 和 goal 都是整数,所以你可以把它拿走从代码。 (Althogh this is not what is bringing troubles to the code, that's more of a style remark). (虽然这不是给代码带来麻烦的原因,但这更像是一种风格评论)。
Trouble is generated by this conditional: (ex_gran % 5 <= small)
.这个条件会产生问题:
(ex_gran % 5 <= small)
。
As I understand, in the example make_bricks(1, 4, 12)
your code returns True
when it should return False
.据我了解,在示例
make_bricks(1, 4, 12)
您的代码在应返回False
时返回True
。 What happens is that the code checks whether (12 - 1) <= 4*5
which is True
, and if (12 - 1) % 5 <= 1
which is also True
and returns True
in line 18 because of this.发生的情况是代码检查
(12 - 1) <= 4*5
是否为True
,如果(12 - 1) % 5 <= 1
也为True
并因此在第 18 行返回True
。
Let's "fix" the code.让我们“修复”代码。 Remember that from math we know there are integers m and r such that
goal = 5*m + r
(moreover r = goal % 5
).请记住,从数学中我们知道有整数 m 和 r 使得
goal = 5*m + r
(而且r = goal % 5
)。 After we know (goal-small) <= big*5
(from false evaluation in line 12) we have two cases:在我们知道
(goal-small) <= big*5
(来自第 12 行的错误评估)之后,我们有两种情况:
Case 1: (goal - small) = big*5
, which implies goal = big*5 + small
and so we should return True
.情况 1:
(goal - small) = big*5
,这意味着goal = big*5 + small
所以我们应该返回True
。
Case 2: (goal - small) < big*5
, then we know goal < big*5 + small
.情况 2:
(goal - small) < big*5
,那么我们知道goal < big*5 + small
。 Here you should check whether (goal % 5) <= small
.在这里你应该检查是否
(goal % 5) <= small
。 If this is false, then we can't arrange the bricks to obtain the goal length (since we don't have enough 1s to fill the residual, as in the example above).如果这是错误的,那么我们就无法通过排列砖块来获得目标长度(因为我们没有足够的 1 来填充残差,如上例所示)。 Otherwise, you should return
True
, since we have enough bricks to surpass the goal length and enough 1s to fill the residual.否则,您应该返回
True
,因为我们有足够的砖块来超过目标长度,并且有足够的 1 来填充残差。
Your code ends up looking like this (I took the liberty of renaming the variables):您的代码最终看起来像这样(我冒昧地重命名了变量):
def make_bricks(small_bricks, big_bricks, goal):
if ( (5*big_bricks + small_bricks) < goal ): #we can't reach the length
return False
elif (small_bricks < (goal%5)) : #we can surpass the length but we don't have
#enough 1s to fill goal's residual w.r.t. 5 div
return False
else: #We can reach the length and have enough 1s to fill residual
return True
notice I took away some case goal <= small
as it's not needed here.请注意,我去掉了一些 case
goal <= small
因为这里不需要。
# This code works with those 3 tests but got some other one # wrong can some help fixed it?
def make_bricks(small, big, goal):
big = big * 5
if small == goal or big == goal:
return true
elif small + big == goal or big - small == goal:
return True
else:
return False
print(make_bricks(3, 1, 8)) # → True
print(make_bricks(3, 1, 9)) # → False
print(make_bricks(3, 2, 10)) # → True
This should work...这应该工作...
def make_bricks(small, big, goal):
total_bricks = (1 * small) + (5 * big)
if total_bricks >= goal:
if goal%5 == 0:
if goal/5 <= goal:
x = True
elif goal%5 <= small:
x = True
else:
x = False
else:
x = False
return x
This is probably not the most efficient way but it will work.. Probably Not the most Readable这可能不是最有效的方法,但它会起作用。可能不是最可读的
def make_bricks(small, big, goal):
if (big is not 0 and (big*5+small*1 > goal) and goal % 5 <= small) or goal < small or big*5+small*1 == goal:
return True
else:
return False
def make_bricks(small, big, goal):
return small >= goal - 5*min(goal//5,big)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.