Image Algorithms Review

Rotate Left

                
  def rotate_image_left(image):
      out = SimpleImage.blank(image.height, image.width)
  
      for y in range(image.height):
          for x in range(image.width):
              source_pixel = image.get_pixel(x, y)
              dest_pixel = image.get_pixel(y, image.width - 1 - x)
              dest_pixel.red = source_pixel.red
              dest_pixel.green = source_pixel.green
              dest_pixel.blue = source_pixel.blue
  
      return out
                
            

Flip Vertical

                
  def flip_vertical(image):
      out = SimpleImage.blank(image.width, image.height)
  
      for y in range(image.height):
          for x in range(image.width):
              source_pixel = image.get_pixel(x, y)
              dest_pixel = image.get_pixel(x, image.height - 1 - y)
              dest_pixel.red = source_pixel.red
              dest_pixel.green = source_pixel.green
              dest_pixel.blue = source_pixel.blue
  
      return out
                
            

Opening the Black Box

              
def in_range(n, low, high):
    if n >= low and n <= high:
        return True 
    return False

def is_even(n):
    if n % 2 == 0:
        return True 
    return False

def is_prime(n):
    for possible_factor in range(2, n):
        # we skip 0 and 1, since they're not the factors we're interested in 
        if n % possible_factor == 0:
            # we've found a factor of n, so n isn't prime and we can return False
            return False # ends the function
    # if the loop ends, it means we didn't find any factors
    # and so we just return True
    return True

def only_one_even(num1, num2):
    if num1 % 2 == 0 and num2 % 2 == 1:
        return True 
    if num1 % 2 == 1 and num2 % 2 == 0:
        return True
    return False

def only_one_even(num1, num2):
    if (num1 % 2 == 0 and num2 % 2 == 1) or (num1 % 2 == 1 and num2 % 2 == 0):
        return True 
    return False

def is_power_of_two(n):
    if n < 1:
        # powers of 2 are equal to 1 or greater
        return False
    while n > 1:
        if n % 2 == 1:
            # n is odd, so can't be divided by 2. Therefore it's not a power of 2
            return False
        n /= 2 # divide n by 2, and continue checking
    # we got down to 1, so n is a power of 2!
    return False
              
            

Playing with Strings


Converting Strings to Integers

                              
def convert_to_int(s):
    result = 0
    for ch in s:
        result *= 10
        result += int(ch)
    return result
                              
                          

Remove All Occurrences

                              
def remove_all_occurrences(s, to_remove):
    result = ""

    for ch in s:
        if ch != to_remove:
            result += ch

    return result
                              
                          

Palindrome Checker

                      
def is_palindrome(s):
    for i in range(len(s)):
        if s[i] != s[len(s) - 1 - i]:
            return False
    return True
                      
                  

Adding Comma to Numeric Strings

                              
def add_commas_to_numeric_string(digits):
    """
    Solution 1
    """
    result = ""
    num_digits = 0
    for char_index in range(len(digits)):
        result = digits[len(digits) - char_index - 1] + result
        num_digits += 1
        if num_digits % 3 == 0 and char_index > 0 and \
            num_digits < len(digits):
            """
            We need to check that
            a) we've added a multiple of 3 number of digits
            b) we've added at least one digit
            c) there are more digits to add
            """
            result="," + result
    return result

def add_commas_to_numeric_string(s):
    """
    Alternate Solution
    >>> add_commas('1234567890')
    '1,234,567,890'
    >>> add_commas('12345')
    '12,345'
    >>> add_commas('1234')
    '1,234'
    >>> add_commas('123')
    '123'
    >>> add_commas('12')
    '12'
    >>> add_commas('1')
    '1'
    """
    result = ''
    for i in range(len(s)):
        # Append the digits left-right as usual.
        # Notice if multiple-of-3 to_do after the digit just added.
        result += s[i]
        to_do = len(s) - (i + 1)
        if to_do > 0 and to_do % 3 == 0:
            result += ','
    return result
                              
                          

Questions about Doctests