Coding Bat: Python. Logic-2

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

12 thoughts on “Coding Bat: Python. Logic-2

  1. Tom Tabak

    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

    Reply
    1. Tom Tabak

      [CODE]
      def make_chocolate(small, big, goal):
      x = goal%5
      if x>small or small+big*5big:
      return (goal-big*5)
      return x
      [/CODE]

      Reply
  2. John

    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

    Reply
  3. Tania

    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

    Reply
  4. DarkBergamotte

    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

    Reply
  5. Solomon Li

    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

    Reply
  6. Leo Liang

    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

    Reply
  7. Josiah

    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!

    Reply
  8. Al

    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))

    Reply
    1. Gregor Ulm Post author

      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?

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.