Skip to content Skip to sidebar Skip to footer

Codingbat Make_bricks Timed Out With While Loop In Python

My goal is: 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). Return True if it is possible t

Solution 1:

Okay I know why it doesn't work. Your code would work. But if there is a loop with about ~229500 (I tried to find the limit value on codebat, but sometime it timeout at this value, sometimes it doesn't. But the value is arround 230k) And as you said : One time out, and every value becomes time out. So to sum up, your code is working, but for the make_bricks(1000000, 1000, 1000100) → True test, there is a too big loop, and Codebat crashes.

So if you want to make it work on Codebat, you have to get rid of the while statement :

defmake_bricks(small, big, goal):
    if small>=5 :
        if (goal - (big + (small // 5 - 1)) * 5) <= (small % 5 + 5):
            returnTrueelse:
            returnFalseelse :
        if (goal - big * 5) % 5 <= (small % 5) : 
            returnTrueelse :
            returnFalse

small//5 return the whole division. I think this is enought. (this should be the alst edit sorry)

Solution 2:

I had a similar problem. I think CodingBat doesn't support +=, -=, etc. So if you change the lines containing that, you should not get a Timed Out. For example, small -= 1 would become small = small - 1. I tested it and it worked on my end, in CodingBat.

Solution 3:

Just categorise the if statements according to whether the big bricks are cumulatively bigger or smaller than the goal.

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

Post a Comment for "Codingbat Make_bricks Timed Out With While Loop In Python"