Category Archives: CodingBat: Python

Coding Bat: Python. List-2

All solutions were successfully tested on 18 April 2013.

count_evens:

def count_evens(nums):
  count = 0
  for element in nums:
    if element % 2 == 0:
      count += 1
  return count

big_diff:

def big_diff(nums):
  return max(nums) - min(nums)

centered_average:

def centered_average(nums):
  sum = 0
  for element in nums:
    sum += element
  return (sum - min(nums) - max(nums)) / (len(nums)-2) 

sum13:

def sum13(nums):
  if len(nums) == 0:
    return 0

  for i in range(0, len(nums)):
    if nums[i] == 13:
      nums[i] = 0
      if i+1 < len(nums): 
        nums[i+1] = 0
  return sum(nums)

sum67:

def sum67(nums):
  for i in range(0, len(nums)):
    if nums[i] == 6:
      nums[i] = 0
      for j in range(i+1, len(nums)):
        temp = nums[j]
        nums[j] = 0
        if temp == 7:
          i = j + 1
          break
  return sum(nums)

Line 9 is not necessary. However, by adjusting “i” you ensure that this script runs in linear time, despite the nested loop.

has22:

def has22(nums):
  for i in range(0, len(nums)-1):
    #if nums[i] == 2 and nums[i+1] == 2:
    if nums[i:i+2] == [2,2]:
      return True     
  return False

The second option is much nicer to look at, but either way is fine.

Coding Bat: Python. String-2

All solutions were successfully tested on 18 April 2013.

double_char:

def double_char(str):
  result = ''
  for char in str:
    result += char * 2
  return result

count_hi:

def count_hi(str):
  count = 0
  for i in range(len(str)-1):
    if str[i:i+2] == 'hi':
      count += 1
  return count

cat_dog:

def cat_dog(str):
  count_cat = 0
  count_dog = 0
  for i in range(len(str)-2):
    if str[i:i+3] == 'dog':
      count_dog += 1
    if str[i:i+3] == 'cat':
      count_cat += 1
  
  return count_cat == count_dog

count_code:

def count_code(str):
  count = 0
  for i in range(0, len(str)-3):
    if str[i:i+2] == 'co' and str[i+3] == 'e':
      count += 1
  return count

end_other:

def end_other(a, b):
  a = a.lower()
  b = b.lower()
  #return (b.endswith(a) or a.endswith(b))
  return a[-(len(b)):] == b or a == b[-(len(a)):] 

Either way is fine.

xyz_there:

def xyz_there(str):
  for i in range(len(str)):
    if str[i] != '.' and str[i+1:i+4] == 'xyz':
      return True
  if str[0:3] == 'xyz':
    return True
  return False

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

Coding Bat: Python. Logic-1

All solutions were successfully tested on 17 April 2013.

cigar_party:

def cigar_party(cigars, is_weekend):
  if is_weekend:
    return cigars >= 40
  return 40 <= cigars <= 60

Pay attention to the last line! In Python it is possible to concatenate comparisons, just like you would do it in mathematics. This can lead to much cleaner code. In my opinion, the solution from the website is worse, but not just for that reason alone:

def cigar_party(cigars, is_weekend):
  if is_weekend:
    return (cigars >= 40)
  else:
    return (cigars >= 40 and cigars <= 60)

date_fashion:

def date_fashion(you, date):
  if you <= 2 or date <= 2:
    return 0
  if you >= 8 or date >= 8:
    return 2
  return 1

squirrel_play:

def squirrel_play(temp, is_summer):
  if is_summer:
    return 60 <= temp <= 100
  return 60 <= temp <= 90

caught_speeding:

def caught_speeding(speed, is_birthday):
  if is_birthday:
    speed -= 5
    
  if speed <= 60:
      return 0
  if 60 < speed <= 80:
    return 1
  return 2

sorta_sum:

def sorta_sum(a, b):
  if 10 <= a + b < 20:
    return 20
  return a + b

It is not necessary to put “a + b” in line 2 inside parentheses due to the rules of precedence of operators. A less experienced human reader might be able to parse this line more quickly with parens, though. However, you shouldn’t assume that you write code for a complete beginner.

alarm_clock:

def alarm_clock(day, vacation):
  if not vacation:
    if 1 <= day <= 5:
      return '7:00'
    return '10:00'
 
  if 1 <= day <= 5:
    return '10:00' 
  return 'off'

love6:

def love6(a, b):
  return a == 6 or b == 6 or (a + b) == 6 or abs(a - b) == 6

What, this looks ugly you say? I completely agree, and there is a much more pleasant solution:

def love6(a, b):
  return 6 in [a, b, a + b, abs(a - b)]

in1to10:

def in1to10(n, outside_mode):
  if not outside_mode:
    return n in range(1, 11)
  return n <= 1 or n >= 10

near_ten:

def near_ten(num):
 # return 0 <= (num % 10) <= 2 or 8 <= (num % 10) <= 10
 return num % 10 in [0,1,2,8,9,10]

Again, do you go for ugly or nice and clean?

Coding Bat: Python. List-1

All solutions were successfully tested on 16 April 2013.

first_last6:

def first_last6(nums):
  return nums[0] == 6 or nums[-1] == 6

same_first_last:

def same_first_last(nums):
  return len(nums) >= 1 and nums[0] == nums[-1]

make_pi:

def make_pi():
  return [3, 1, 4]

common_end:

def common_end(a, b):
  return a[0] == b[0] or a[-1] == b[-1]

sum3:

def sum3(nums):
  #return nums[0] + nums[1] + nums[2]
  return sum(nums)

Either way works. The version I’ve commented out is less elegant, though.

rotate_left3:

def rotate_left3(nums):
  return [nums[1], nums[2], nums[0]] 

reverse3:

def reverse3(nums):
  #return [nums[2], nums[1], nums[0]]
  return nums[::-1]

Again, the second version is more elegant or, as some phrase it, more “pythonic”.

max_end3:

def max_end3(nums):
  m = max(nums[0], nums[2])
  return [m, m, m]

For matters of comparison, here is the solution from the website:

def max_end3(nums):
  big = max(nums[0], nums[2])
  nums[0] = big
  nums[1] = big
  nums[2] = big
  return nums

It is less expressive and, frankly, a bit painful to look at. A much nicer way to assign “big” to three variables at once would be one of the two following ways:

def max_end3(nums):
  big = max(nums[0], nums[2])
  #nums[0], nums[1], nums[2] = big, big, big
  nums[0], nums[1], nums[2] = (big, ) * 3
  return nums

sum2:

def sum2(nums):
  if len(nums) == 0:
    return 0
  if len(nums) == 1:
    return nums[0]
  return nums[0] + nums[1]

middle_way:

def middle_way(a, b):
  return [a[1], b[1]]

make_ends:

def make_ends(nums):
  return [nums[0], nums[-1]]

has23:

def has23(nums):
  return 2 in nums or 3 in nums

Do you remember what this looked like in Java?