4

我正在完成这个CodingBat 问题:

我们想要制作一排目标英寸长的砖块。我们有许多小砖(每个 1 英寸)和大砖(每个 5 英寸)。如果可以通过从给定的砖块中选择来实现目标,则返回 True。这比看起来要难一些,并且可以在没有任何循环的情况下完成。

make_bricks(3, 1, 8) → True
make_bricks(3, 1, 9) → False
make_bricks(3, 2, 10) → True

我写:

if small + big * 5 >= goal: return True
else: return False

我的错误是:make_bricks(3, 2, 9)False(我设置为真,因为 (2*5)+3=13 大于 9。

make_bricks(2, 1000000, 100003) → False
make_bricks(1, 4, 12) → False

我找到了这个答案,而不是通过了这些测试:

if goal > small + big * 5:
  return False
else:
  return goal % 5 <= small

我真的不明白为什么,有人可以解释一下吗?

4

22 回答 22

11

这是一个较短的解决方案。

def make_bricks(small, big, goal):
    return (goal%5)<=small and (goal-(big*5))<=small
于 2017-07-25T10:33:46.073 回答
2
def make_bricks(small, big, goal):
    if (goal//5 <= big) and ((goal - 5*(goal//5))<= small): return True
    elif (goal//5 >= big) and ((goal - 5*(big)) <= small): return True
    return False
于 2014-12-29T22:20:28.247 回答
2
def make_bricks(small,big,goal):
    # for checking how much small bricks we needed we use (goal%5)
    if small<(goal%5) or (small+big*5)<goal:
       return False
    else:
       return True
于 2018-09-20T08:46:06.710 回答
1

您没有考虑到您可能不需要使用所有 5。例如,您的代码在以下情况下失败:

def make_bricks(3, 2, 9)

你真的只能在这里使用一块大砖。这让你有 4 块,只有两块小砖块来弥补差额。

这就是工作示例有效的原因。考虑到所有 5 可能无法使用。

codingbat 的好处在于,它们会告诉您他们的期望以及他们在试验期间得到的结果。您可以使用失败的值遍历代码以查看失败的原因。铅笔和纸是你的朋友。

于 2013-01-05T18:46:43.567 回答
1
def make_bricks(small, big, goal):
  if (goal%5)<=small and (goal-(big*5))<=small:
    return True
  else:
    return False

久经考验

于 2016-11-11T23:14:26.680 回答
0
#Did it with recursion:

def make_bricks(small, big, goal):
    if (small + big * 5 )< goal:
        return False
    if (small + big * 5) == goal:
        return True
    return make_bricks(small-1, big, goal) or make_bricks(small, big-1, goal) 
于 2014-08-20T19:08:06.337 回答
0

这个 Python 代码在 Codingbat 页面中对我有用。

def make_bricks(small, big, goal):
  small = small * 1
  big = big * 5
  while big > 0:
    if small == goal or big == goal:
      return True
    if small + big == goal:
      return True
    if small > goal:
      return True
    if big < goal and (small + big) > goal:
      return True
    big = big - 5
  if small > goal:
    return True
  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    False   OK  
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   False   OK  
make_bricks(3, 1, 7) → True True    OK  
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 False   OK  
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  
other tests
OK
于 2016-07-30T09:58:58.500 回答
0
def make_bricks(small, big, goal):

  def b_req(big, goal):
    req = goal // 5
    if req <= big and req > 0 :
      return req
    else:
      return big
  if goal < 5:
    s_req = goal
  else:
    s_req = goal - ( b_req(big, goal) * 5 )

  if small + big * 5 < goal or s_req > small :
    return False
  elif s_req <= small :
    return True
于 2019-11-24T17:28:36.300 回答
0
def make_bricks(small, big, goal):
  big_used = min(goal / 5, big)
  small_used = min(goal - big_used * 5, small) 
  return big_used * 5 + small_used == goal
于 2020-12-24T14:08:30.903 回答
0

您可以尝试以下方法:

if(((small*1)+(big*5)) < goal):
    return False
elif((5*(goal//5))+small<goal):
    return False
else:
    return True
于 2016-08-22T07:19:28.907 回答
0

虽然这个问题已经被回答死了,但我想发布我的单行字。我们使用的逻辑是 1)找出将使用多少大砖(最多可用的数量)然后 2)看看是否有足够的小砖来弥补差异。

示例代码允许您以不同长度的砖块进行编码:

def make_bricks(small, big, goal):
  smallLen = 1
  bigLen = 5

  bigs = goal // bigLen
  smallGoal = goal - (min(bigs, big) * bigLen)

  return smallGoal <= (small * smallLen)

您可以通过将其粉碎来重写它,但它的可读性较差(如果令人满意的话)

def make_bricks(small, big, goal):
  return goal - (min(goal // 5, big) * 5) <= small

有趣的是,我们所有的方法都使用最大数量的大砖块找到了解决方案,这确实满足了这个问题,但可能还有其他大小砖块的组合可以给出正确答案。问题没有指定你应该使用所有的大砖。我想你会遍历大块的范围,并确定如果任何一个数字 < 大块的结果也是正确的。

于 2019-06-23T19:03:03.140 回答
0
def make_bricks(small, big, goal):

   new_goal = goal - (big * 5) 

   if 5*big > goal:
     new_goal = goal % 5

   return new_goal <= small
于 2020-05-08T20:26:59.123 回答
0
def make_bricks(small, big, goal):
  
  if (goal > big*5 + small) : return False   #You do not have enough bricks to make the goal
  if (goal % 5 > small) : return False       #You do not have enough small bricks to make the goal
  
  return True                                # In all other cases i.e. when all the failure cases above have been taken care of  
于 2020-10-29T21:39:45.507 回答
0

使用此代码:

if(((small*1)+(big*5))< goal):
    return False

elif((5*(goal//5))+small<goal):
    return False

else:
    return True
于 2016-08-22T07:28:19.463 回答
0

这就是我想出的:

def make_bricks(small, big, goal):
   bigs = big * 5
   if (bigs >= goal) and (goal % 5 <= small):
     return True   
   if (goal - small <= bigs) and (bigs < goal):    
     return True  
   else:
     return False

甚至不如 Haasan Razas 的回答那么优雅:

def make_bricks(small, big, goal):
    return (goal%5)<=small and (goal-(big*5))<=small

** 回应“如果可能 - 你能解释一下你的答案吗? - N997 Mar 23 at 3:53” 这一行代码涵盖了所有可能性。如果 case 1 和 case 2 都不是 True,则代码将返回 False: Case 1:目标除以 5 的余数需要小于或等于小砖块。这样我们就有足够的小砖来覆盖大砖铺好后剩下的不到 5 英寸的距离。案例2:目标减去所有大砖小于或等于小砖。本案例涵盖了大小不足以实现目标的场景,也涵盖了仅使用小砖块即可实现目标的场景

于 2019-05-08T16:28:17.100 回答
0
def make_bricks(small, big, goal):

#If you can do with all small, go for it
    if (small >= goal):
        return True

#If small are not enough, find out how many big and small combos
    rem = goal%5
    if (big*5 >= goal):
       if(small >= rem):
          return True
    else:
       if (small >= goal-(big*5) ):
          return True
    return False
于 2020-02-12T05:49:22.480 回答
0
def make_bricks(small, big, goal):
   if (goal//5) <= big: 
       if (goal%5) <= small: 
         return True
       else: 
         return False
   if (goal//5) >= big:
       if (goal - (big*5)) <= small:
         return True
       else:
         return False
于 2019-06-01T04:00:21.437 回答
0
def make_bricks(small, big, goal):
  if big*5 > goal:
    big = goal//5 
  return big*5 +small >= goal   
于 2020-02-03T17:43:17.820 回答
0

def make_bricks(小,大,目标):

b=big*5

s=small*1

`如果 b== 目标或 s== 目标:

 return True

如果 b==0: #0

if s>=goal:

 return True

如果 s==0:

return b>=goal

if b<goal:

if (b+s)>=goal:
  return True
else:
  return False

如果 b> 目标:

 if ((b+s)-goal)%5==0:
   return True

别的:

return False  
于 2019-10-28T10:30:06.407 回答
0

3行代码中的完整解决方案:

def make_bricks(small, big, goal):
  #find max number of big bricks required
  bigBrickNeeded = goal / 5;
  #check if you have enough big bricks, if not use all big bricks avaible
  bigBrickNeeded = min(big, bigBrickNeeded);
  #fill the gap with small bricks
  return small >= (goal - bigBrickNeeded*5);
于 2018-08-31T13:16:13.600 回答
-1
def make_bricks(small, big, goal):  

   b=big*5
   s=small*1

   if b==goal or s==goal:   
       return True

   if b==0:  #0
       if s>=goal: 
           return True

   if s==0:    
       return b>=goal

   if b<goal:   
       if (b+s)>=goal:
           return True
       else:
           return False

   if b>goal:
       if ((b+s)-goal)%5==0:
           return True
   else: 
       return False  
于 2019-10-28T10:05:14.170 回答
-1

该问题表明它可以在没有循环的情况下解决。这个解决方案对我有用,但可能不是最有效的:

def make_bricks(small, big, goal):
  if (big*5) + small == goal:
    return True
  elif (goal//5) >= goal:
    return True
  elif small >= goal:
    return True
  else:
    bn = goal//5
    sn = goal - (bn*5)

    if big>= bn and sn<=small:
      return True
    elif big < bn and goal<= (big*5) + small:
      return True
    else:
      return False
于 2016-11-24T12:48:02.263 回答