我正在尝试解决这个问题:
我们要制造一排目标长为英寸的砖块。我们有一个 小砖(每个1英寸)和大砖(每个5英寸)的数量。 如果可以通过从以下选项中选择目标来返回目标,则返回True 给砖头。这比看起来要难一点并且可以完成 没有任何循环。
make_bricks(3, 1, 8) → True make_bricks(3, 1, 9) → False make_bricks(3, 2, 10) → True
为此,我编写了以下代码:
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
这是测试的结果:
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
其中只有4个是错误的,但我仍然无法弄清错误。
怎么了?
答案 0 :(得分:2)
首先尝试使用尽可能多的大砖块,然后再使用较小的砖块。请注意,这之所以可行是因为大尺寸的尺寸是小尺寸的尺寸的倍数,如果尺寸为例如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
答案 1 :(得分:0)
错误很简单:)它位于第16行。尽管代码可能会大大缩短。
首先,如果有的话,请注意条件ex_gran <= big * 5
是不相关的,因为条件before已经为假(elif ex_gran > big*5:
),并且我们假设small,big和目标都是整数,因此可以将其删除从代码。 (虽然这不是给代码带来麻烦的原因,但更多是样式说明)。
故障是由以下条件产生的:(ex_gran % 5 <= small)
。
据我了解,在示例make_bricks(1, 4, 12)
中,您的代码在应返回True
时返回了False
。发生的事情是该代码检查是否(12 - 1) <= 4*5
是True
,是否(12 - 1) % 5 <= 1
也是True
并因此在第18行返回True
。
让我们“修复”代码。请记住,从数学上我们知道存在整数m和r使得goal = 5*m + r
(而且r = goal % 5
)。知道了(goal-small) <= big*5
(根据第12行的错误评估)之后,我们有两种情况:
情况1:(goal - small) = big*5
,表示goal = big*5 + small
,因此我们应该返回True
。
情况2:(goal - small) < big*5
,那么我们知道goal < big*5 + small
。在这里,您应该检查是否为(goal % 5) <= small
。如果这是错误的,那么我们将无法排列积木来获得目标长度(因为如上例所示,由于我们没有足够的1来填充残差)。否则,您应该返回True
,因为我们有足够多的积木来超过目标长度,并且有足够的1s来填补残差。
您的代码最终看起来像这样(我可以随意重命名变量):
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
注意,我删除了一些案例goal <= small
,因为这里不需要。
答案 2 :(得分:0)
# 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
答案 3 :(得分:0)
这应该有效...
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
答案 4 :(得分:0)
这可能不是最有效的方法,但它会起作用.. 可能不是最易读的
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