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.

7 thoughts on “Coding Bat: Python. List-2

  1. aramini

    Just my 2 cents…

    The generalized instructions for List-2 says:
    “Medium python list problems — 1 loop.. Use a[0], a[1], … to access elements in a list, len(a) is the length.”
    ( http://codingbat.com/python/List-2 )

    So, for sum67, the solution below follows the “one-loop” constraint. It only uses one loop and if statements/Boolean logic. Not the most elegent or clever solution, but I think it is easier for a “n00b” like me to understand than Gregor’s solution for sum67. (Although I do understand that when coding in the real world where things aren’t simply academic in nature, you’d want code to be pretty, clean, and concise).

    def sum67(nums):
    found6 = False
    result = 0
    for n in nums:
    if n==6:
    found6 = True
    continue
    if n==7 and found6:
    found6 = False
    continue
    if not found6:
    result += n
    return result

    Reply
    1. Chris

      Armani,

      Your is a much better solution – no need at all for the nested loops, and no need for list indexes.

      I did it slightly differently:

      “`
      def sum67_loop(nums):
      “””
      A basic loop method. the key is to keep a flag
      set to know whether a 6 has been encountered
      “””
      total = 0
      is6 = False
      for num in nums:
      if num == 6 or is6:
      is6 = True
      else:
      total += num
      if num == 7:
      is6 = False
      return total
      “`
      But the logic is the same.

      Reply
  2. Leo Liang

    For sum67, here is my solution:
    def sum67(nums):
    sum = 0
    ignore = False
    for i in nums:
    if i == 6:
    ignore = True
    if not ignore:
    sum += i
    if i == 7:
    ignore = False
    return sum

    Reply
  3. Omenofis

    a little comment in your solution of sum13 exercise.
    Your solution works for most of the cases but not for all. I may not know a lot of python just learning it, but this solution works better:
    def sum13(nums):
    if len(nums)==0:
    return 0
    total=0
    for i in range(len(nums)):
    total+=nums[i]
    if nums[i]==13:
    total-=nums[i]
    if i+1<len(nums):
    if nums[i+1]==13:
    continue
    total-=nums[i+1]
    return total

    Reply
    1. Gregor Ulm Post author

      My solution passes all test cases on the CodingBat website, so I view it as correct. What test does your code pass which mine does not? Your approach is suboptimal because you add and then remove elements. A more elegant approach would be to simply skip over every element after a ’13’. Here’s how you’d do it:

      def sum13(nums):
      
        total = 0
       
        i = 0
        while i < len(nums):
          val = nums[i]
          if val == 13:
            i += 2
          else:
            total += val
            i += 1
            
        return total
      
      Reply

Leave a Reply to Gregor Ulm Cancel 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.