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

4 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

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.