我正在完成this 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
我真的不明白为什么,有人可以解释一下吗?
答案 0 :(得分:7)
这是一个较短的解决方案。
def make_bricks(small, big, goal):
return (goal%5)<=small and (goal-(big*5))<=small
答案 1 :(得分:1)
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
答案 2 :(得分:1)
def make_bricks(small, big, goal):
if (goal%5)<=small and (goal-(big*5))<=small:
return True
else:
return False
尝试并测试
答案 3 :(得分:1)
您没有考虑到您可能不需要使用所有5个。例如,您的代码失败:
def make_bricks(3, 2, 9)
你真的只能在这里使用一块大砖。这样你就可以得到4块而且只有两块小砖来弥补差异。
这就是工作示例有效的原因。它考虑到所有5个都可能无法使用。
关于编码包的好处是它们告诉你他们期待什么以及他们在试验期间得到了什么。您可以使用失败的值来查看代码,以查看失败的原因。铅笔和纸是你的朋友。
答案 4 :(得分:1)
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
答案 5 :(得分: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);
答案 6 :(得分: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
答案 7 :(得分: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
答案 8 :(得分:0)
public class UnitTest1 : IDisposable
{
IServiceProvider _serviceProvider;
MyDbContext _db;
public UnitTest1(ITestOutputHelper output)
{
_serviceProvider = new ServiceCollection()
.AddLogging(x => x.AddProvider(new TestLoggerProvider(output)))
.AddEntityFrameworkSqlite()
.BuildServiceProvider();
_db = new MyDbContext(
new DbContextOptionsBuilder<MyDbContext>()
// Don't call UseLoggerFactory! (a new service provider would be
// created every time without ever getting disposed)
.UseInternalServiceProvider(_serviceProvider)
.UseSqlite("Data Source=:memory:")
.Options);
}
[Fact]
public void Test1()
{
_db.Database.ExecuteSqlRaw("-- Can you see me?");
}
public void Dispose()
{
_db.Dispose();
(_serviceProvider as IDisposable)?.Dispose();
}
}
答案 9 :(得分: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
答案 10 :(得分:0)
<label htmlFor={id}>
<input
id={id}
type="checkbox"
defaultChecked={input.props.checked}
// disabled={input.props.disabled}
/>
<span className="custom-checkbox"></span>
{restChildren}
</label>
答案 11 :(得分: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
答案 12 :(得分: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
答案 13 :(得分: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
有趣的是,我们所有的方法都使用最大数量的大砖块找到了解决方案,这确实满足了这个问题,但是可能存在其他大小砖块的组合,它们给出了正确的答案。该问题并未指定您应该使用所有大砖块。我想您会遍历大块积木的范围,并确定结果是否正确(<大块大积木)。
答案 14 :(得分: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
答案 15 :(得分: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 3月23日,3:53” 这一行代码涵盖了所有可能性。如果情况1和情况2都不为True,则代码将返回False: 情况1:目标的余数除以5需要小于或等于小砖头。这样,我们有足够的小砖块来覆盖大砖块放置后剩下的少于5英寸的任何距离。 情况2:目标减去所有大砖块等于或小于小砖块。这种情况涵盖了大小不满足目标的情况,也包括仅使用小砖块就可以实现目标的情况
答案 16 :(得分:0)
使用此代码:
if(((small * 1)+(big * 5))&lt; goal):
return False
elif的((5 *(目标// 5))+小
return False
否则:
return True
答案 17 :(得分:0)
您可以尝试以下操作:
if(((small*1)+(big*5)) < goal):
return False
elif((5*(goal//5))+small<goal):
return False
else:
return True
答案 18 :(得分: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
&#13;
输入和输出:
预计运行
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,4,16)→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
其他测试
行
答案 19 :(得分: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)
答案 20 :(得分:-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
答案 21 :(得分:-1)
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
答案 22 :(得分:-1)
我还不知道python但是我为java解决了它。 我发布了java代码,以便您了解逻辑,然后您可以使用它来编写python程序。 我们必须考虑&#39;小&#39;的所有组合。和&#39;大&#39;砖块,导致目标。 所以我们需要两个for循环嵌套在其他内部的循环。
参见代码:
public boolean makeBricks(int small, int big, int goal) {
for(int i = 0; i<=(big*5) ;i=i+5){
for(int j=0; j<=small;j++){
if(i+j==goal){
return true;
}
}
}
return false;
}
编辑1: 让我们看一下您发布的解决方案。
If goal > small + big*5
return false
这句话确保如果所有的砖块都被收集起来,那么它们就无法弥补“目标”,所以函数会立即返回为假。
现在&#39;其他&#39;部分:
else:
return goal % 5 <= small
在此,检查表达式的剩余部分(目标%5)是否小于或等于“小”的数量。砖块 。 这是因为&#39;小&#39;砖块长1英寸。如果每块小砖的长度为2英寸,则条件为
return (goal%5)/2 <= small.
如果小砖块是&#39; n&#39;那么条件就是
return (goal%5)/n <=small.
我们在学校里学到了一个非常简单的数学规则,巧妙地使用了。
数字=(它的除数*除法)+余数
答案 23 :(得分:-1)
我相信这应该有效。但由于某种原因,它在这里不起作用。
http://codingbat.com/prob/p118406
def make_bricks(small, big, goal):
for i in range(0, big + 1):
for j in range(0, small + 1):
if j + i * 5 == goal:
return True
return False
答案 24 :(得分:-1)
def make_bricks(small, big, goal):
for i in range(0, big + 1):
for j in range(0, small + 1):
if j + i * 5 == goal:
return True
return False