All solutions were successfully tested on 18 April 2013.
make_bricks:
def make_bricks(small, big, goal): return goal%5 >= 0 and goal%5 - small <= 0 and small + 5*big >= goal
lone_sum:
def lone_sum(a, b, c):
if a == b == c:
return 0
if b == c:
return a
if a == c:
return b
if a == b:
return c
return a + b + c
lucky_sum:
def lucky_sum(a, b, c):
if a == 13:
return 0
if b == 13:
return a
if c == 13:
return a + b
return a + b + c
no_teen_sum:
def no_teen_sum(a, b, c):
return fix_teen(a) + fix_teen(b) + fix_teen(c)
def fix_teen(n):
#if 13 <= n <= 14 or 17 <= n <= 19:
if n in [13, 14, 17, 18, 19]:
return 0
return n
I consider checking for list membership to be more elegant than multiple comparison operations.
round_sum:
def round_sum(a, b, c):
return round10(a) + round10(b) + round10(c)
def round10(n):
if n % 10 >= 5:
return n + 10 - (n % 10)
return n - (n % 10)
close_far:
def close_far(a, b, c): cond1 = abs(a-b) <= 1 and abs(b-c) >=2 and abs(a-c) >= 2 cond2 = abs(a-c) <= 1 and abs(a-b) >=2 and abs(c-b) >= 2 return cond1 or cond2
make_chocolate:
def make_chocolate(small, big, goal):
maxBig = goal / 5
if big >= maxBig:
if small >= (goal - maxBig * 5):
return goal - maxBig * 5
if big < maxBig:
if small >= (goal - big * 5):
return goal - big * 5
return -1
Hi! I’m just starting to learn coding(for about 5 days now) so I’m still not clear if all working solutions are acceptable, or should I follow some unwritten rules. If so, I would like to know which ones 🙂
For example, here is my take on the def make_chocolate problem:
Thank you for your effort! I believe I learned a lot from your solutions.
def make_chocolate(small, big, goal):
x = goal%5
if x>small or small+big*5big:
return (goal-big*5)
return x
[CODE]
def make_chocolate(small, big, goal):
x = goal%5
if x>small or small+big*5big:
return (goal-big*5)
return x
[/CODE]
Sorry, can’t find a way to post code,feel free to delete all comments 🙂
good job on the make_bricks problem
you can simplify it a bit:
def make_bricks(small, big, goal):
return small >= goal%5 and small + 5*big >= goal
your “goal%5 >= 0” clause is a tautology, because a%b is always >= 0
I appreciate the solutions you have posted. Very helpful. Here is an alternative for lone_sum
def lone_sum(a, b, c):
sum = 0
values = [a, b, c]
for v in values:
if values.count(v) == 1:
sum += v
return sum
Here is an alternative for make_chocolate :
def make_chocolate(small, big, goal):
if goal>big*5 + small or small<goal % 5:
return -1
remaining = goal – big*5
while remaining<0:
remaining += 5
return remaining
def no_teen_sum(a, b, c):
return sum(filter(fix_teen, [a, b, c]))
def fix_teen(n):
my_range = list(range(13, 15)) + list(range(17, 20))
return n not in my_range
For make_chocolate, I have
def make_chocolate(small, big, goal):
if goal / 5 <= big:
amount_big = goal / 5
else:
amount_big = big
if goal – (amount_big * 5) <= small:
amount_small = goal – (amount_big * 5)
else:
amount_small = -1
return amount_small
———- or simply ————–
def make_chocolate(small, big, goal):
amount_big = (goal / 5) if (goal / 5 <= big) else big
amount_small = (goal – (amount_big * 5)) if (goal – (amount_big * 5) <= small) else -1
return amount_small
def make_chocolate(small, big, goal):
while big > 0 and goal >= 5:
goal = goal – 5
big = big – 1
if goal <= small:
return goal
return -1
There's my solution for make chocolate. Hope this helps!
Little quicker, cleaner way to run round_sum
def round_sum(a, b, c):
return round10(a) + round10(b) + round10(c)
def round10(num):
return int(round(num,-1))
Just like with the other comment you left, you’re using an in-built method. This defeats the purpose of this exercise. Imagine you face a problem for which there is no library function available. What would you do then?
Thank you