CodingBat Python make_chocolate code failing in "other tests"
Question:
There is a coding problem on CodingBat.com (Logic-2 Python section) that asks for a function to determine how many small chocolate bars are used for a weight requirement. The question is as follows:
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.
make_chocolate(4, 1, 9) → 4
make_chocolate(4, 1, 10) → -1
make_chocolate(4, 1, 7) → 2
I came up with this solution to the problem but it still fails in “other tests”. Is there any problem that causes this?
Code:
def make_chocolate(small, big, goal):
if (small + 5*big < goal) or (goal % 5 > small):
return -1
elif small >= goal:
return small
else:
smallnum = 0
for i in range(1,big+1):
if 5*i + small >= goal:
if 5*i > goal:
break
smallnum = goal - 5*i
return smallnum
EDIT:
I have managed to finish the problem thanks to Mariah Akinbi. I have updated the code as follows:
def make_chocolate(small, big, goal):
if (small + 5*big < goal) or (goal % 5 > small):
return -1
elif 5 <= goal:
smallnum = 0
for i in range(1,big+1):
if 5*i + small >= goal:
if 5*i > goal:
break
smallnum = goal - 5*i
return smallnum
return goal
Answers:
Your code seems to fail on:
make_chocolate(8,1,7)
–> returns 8 instead of 2
I believe the issue is:
elif small >= goal:
return small
The instructions say to use the big bars first, then use the nuggets to meet the goal.
Here is much simpler solution:
def make_chocolate(small, big, goal):
big_use = min(goal / 5, big)
small_needed = goal - big_use * 5
if small < small_needed:
return -1
return small_needed
see my code below
def make_chocolate(small, big, goal):
if goal-big*5<=small: #find out if the total chocolate can make the goal
if goal%5==0 and goal-big*5==0: # if small bars not required
return 0
if goal-big*5>0:
return goal-big*5
if goal-big*5<0 and goal%5<=small:
return goal%5
else:
return -1
if goal-big*5>small:
return -1
A Simpler Approach
def make_chocolate(smallBars, bigBars, goal):
bigBarsInKilo = bigBars * 5
while bigBarsInKilo > goal:
bigBarsInKilo -= 5
smallBarsRequired = goal - bigBarsInKilo
if smallBars >= abs(smallBarsRequired): # abs (to ignore negative values)
return smallBarsRequired
else:
return -1
print(make_chocolate(1, 2, 5))
def make_chocolate(small, big, goal):
if goal> big*5 +small or goal % 5 > small:
return -1
elif big *5 > goal:
return goal % 5
else:
return goal - big *5
There is a coding problem on CodingBat.com (Logic-2 Python section) that asks for a function to determine how many small chocolate bars are used for a weight requirement. The question is as follows:
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.
make_chocolate(4, 1, 9) → 4
make_chocolate(4, 1, 10) → -1
make_chocolate(4, 1, 7) → 2
I came up with this solution to the problem but it still fails in “other tests”. Is there any problem that causes this?
Code:
def make_chocolate(small, big, goal):
if (small + 5*big < goal) or (goal % 5 > small):
return -1
elif small >= goal:
return small
else:
smallnum = 0
for i in range(1,big+1):
if 5*i + small >= goal:
if 5*i > goal:
break
smallnum = goal - 5*i
return smallnum
EDIT:
I have managed to finish the problem thanks to Mariah Akinbi. I have updated the code as follows:
def make_chocolate(small, big, goal):
if (small + 5*big < goal) or (goal % 5 > small):
return -1
elif 5 <= goal:
smallnum = 0
for i in range(1,big+1):
if 5*i + small >= goal:
if 5*i > goal:
break
smallnum = goal - 5*i
return smallnum
return goal
Your code seems to fail on:
make_chocolate(8,1,7)
–> returns 8 instead of 2
I believe the issue is:
elif small >= goal:
return small
The instructions say to use the big bars first, then use the nuggets to meet the goal.
Here is much simpler solution:
def make_chocolate(small, big, goal):
big_use = min(goal / 5, big)
small_needed = goal - big_use * 5
if small < small_needed:
return -1
return small_needed
see my code below
def make_chocolate(small, big, goal):
if goal-big*5<=small: #find out if the total chocolate can make the goal
if goal%5==0 and goal-big*5==0: # if small bars not required
return 0
if goal-big*5>0:
return goal-big*5
if goal-big*5<0 and goal%5<=small:
return goal%5
else:
return -1
if goal-big*5>small:
return -1
A Simpler Approach
def make_chocolate(smallBars, bigBars, goal):
bigBarsInKilo = bigBars * 5
while bigBarsInKilo > goal:
bigBarsInKilo -= 5
smallBarsRequired = goal - bigBarsInKilo
if smallBars >= abs(smallBarsRequired): # abs (to ignore negative values)
return smallBarsRequired
else:
return -1
print(make_chocolate(1, 2, 5))
def make_chocolate(small, big, goal):
if goal> big*5 +small or goal % 5 > small:
return -1
elif big *5 > goal:
return goal % 5
else:
return goal - big *5