CondingBat Python puzzle results in "Timed out"

codingbat python answers
codingbat python warmup 1 answers
codingbat answers
codingbat python warmup 2 answers
python practice websites
codingbat python logic-2 answers
python logics
codingbat c++

I am trying to solve this CodingBat problem:

We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.

I understand the logic of the problem but whenever I try to run it, I get a Timed Out exception. So can anyone tell me what I'm doing wrong?

def make_chocolate(small, big, goal):

  total = 0

  if goal < 5:
    big = 0
  for i in xrange(big):
    total += 5
    if total == goal:
      return 0
    elif total+5>goal:
      break
  for k in xrange(small):
    total +=1
    if total == goal:
      return (k+1)

  return -1

The failure is caused by time it takes to do this test:

makeChocolate(1000, 1000000, 5000006)

Even though the other tests can pass, once one test times out, the report shows all tests as timing out. To see this is true, change xrange(big) to xrange(big if big < 101 else 0) and you will see every test pass except for the one above.

Web based evaluators need timeouts like these for performance reasons . It must be that the Python timeout allows for less loops than the Java timeout.

Here is non looping solution that passes:

def make_chocolate(small, big, goal):
    big *= 5
    if big + small < goal or small < goal%5:
        return -1
    small = goal - big
    return small%5 if small < 0 else small 

A slightly different solution is needed for Java due to how it treats the modulo for negative numbers.

public int makeChocolate(int small, int big, int goal) {
    big *= 5;
    if (big + small < goal || small < goal%5)
        return -1;
    small = goal - big;
    return small < 0 ? (big+small)%5 : small;
}

CodingBat Python Warmup-1, CodingBat code practice. Java · Python. Warmup-1 chance. Simple warmup problems to get started, no loops (solutions available)  CondingBat Python puzzle results in “Timed out”. I am trying to solve this CodingBat problem: We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars.

This is clunky, but it works:

def make_chocolate(small, big, goal):
    bigbars=goal//5
    if bigbars<=big:
        smallgoal=goal-(bigbars*5)
        if smallgoal>=0 and smallgoal<=small:
            return smallgoal
        if smallgoal>small:
            return -1
    if bigbars>big:
        smallgoal=goal-(big*5)
        if smallgoal<=small:
            return smallgoal
        if smallgoal>small:
            return -1

CondingBat Python puzzle results in "Timed out", I am trying to solve this CodingBat problem: We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each)  Python Lists The same as with strings, the len() function returns the length of a list, and [i] accesses the ith element. The same loop as above, for num in nums: , will loop over all the values in a list.

You could simply do it this way:

def make_chocolate(small, big, goal):
    noOfBigs = big if(5 * big <= goal) else goal / 5
    return  goal - (noOfBigs * 5)  if small >= (goal - (noOfBigs * 5)) else -1

diezguerra/codingbat-python-solutions: CodingBat Python , CodingBat Python Solutions. Pretty self explanatory, the solutions I came up with when I did the problem sets. There are probably better solutions, comments are  Mastering CodingBat (Java), Vol. 2: Intermediate A in paperback now available for pre-order; Recent Comments. Gregor Ulm on Coding Bat: Python. String-2; John doe on Coding Bat: Python. String-2; Jeff on An Example of the Beauty of Haskell; Gregor Ulm on Relational Division in SQL The Easy Way; Natka on Coding Bat: Python. Logic-2; Archives

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

codingbat-python/logic1.py at master · gregorykremler/codingbat , problem solutions for codingbat.com - gregorykremler/codingbat-python. codingbat-python/logic1/logic1.py Basic boolean logic puzzles -- if else or not. #. We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble. monkey_trouble (True, True) → True. monkey_trouble (False, False) → True. monkey_trouble (True, False) → False.

Seems like cleaner logic if you first reduce Big value to the number you'll actually use:

def make_chocolate(small, big, goal):
  while big * 5 > goal:
    big -= 1
  if (goal - (big * 5)) <= small:
    return goal - (big * 5)
  else:
    return -1

Codingbat python exercises, If you are looking for a python challenge and are a beginner programmer, this might be Codingbat Python Questions and Answers Section 1 This document is  Warmup-1 chance. Simple warmup problems to get started, no loops (solutions available) monkey_trouble H. parrot_trouble H.

Adaptive and Adaptable Learning: 11th European Conference on , Kodr combines several features from tools like Coding Bat, Python Tutor [4], Pythy [3] and a console output section where output and submission result is printed. In addition Kodr was designed with a completely modular challenge module  Mastering CodingBat (Java), Vol. 2: Intermediate A in paperback now available for pre-order; Recent Comments. Gregor Ulm on Coding Bat: Python. String-2; John doe on Coding Bat: Python. String-2; Jeff on An Example of the Beauty of Haskell; Gregor Ulm on Relational Division in SQL The Easy Way; Natka on Coding Bat: Python. Logic-2; Archives

Practicing Python: 3 favorite online resources, I've been learning Python for almost a year. full with discussion of a topic and a link to a solution, Practice Python is a great Create a free account and track your progress on the various series of puzzles. CodingBat is free. Functional filtering and mapping operations on lists with lambdas. Java Example Solution Code. Java String Introduction (video) Java Substring v2 (video) Java String Equals and Loops. Java String indexOf and Parsing. Java If and Boolean Logic. If Boolean Logic Example Solution Code 1 (video) If Boolean Logic Example Solution Code 2 (video)

CodingBat python question : learnprogramming, CodingBat python question Is one of the answers better than the other? I'm learning programming as a hobby because I enjoy puzzles, math, and solving  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 to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks.

Comments
  • What happens if goal > small and not a multiple of 5?
  • Will you post the exact error?
  • "Timed Out" This is on codingbat, so it doesn't say much.
  • please explain this code - give it a bit more detail as to what you are doing, and what you have done to 'fix/help' the OP
  • Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
  • Can you add any supporting explanation for this answer? How does this solve the OP's question? There are several other answers, so please tell us what makes this one work.