Image Algorithms Review

Rotate Left

Write the following function:

def rotate_image_left(image)

that takes a SimpleImage parameter called image and returns a version of that image, rotated 90 degrees to the left.

Rotate Left

Note that the source image's width may be different from its height, and that the dimensions of the result image reflect the rotation you have performed; the result image is as wide as the source was tall, and as tall as the source was wide.

Flip Vertical

Write the following function:

def flip_vertical(image)

that flips a SimpleImage parameter called image in the vertical direction.

An image grid

Opening the Black Box

In this program, you will tackle a series of problems intended to increase your familiarity with parameters and return statements. Implement the following functions:


Playing with Strings


Converting Strings to Integers

Write the following function:

def convert_to_int(s):

which takes in a string parameter s that is the string representation of some positive integer and converts it to an integer. For example, convert_to_int("42") should return 42 and convert_to_int("0") should return 0.


Remove All Occurrences

Write the following function:

def remove_all_occurrences(s, to_remove)

which removes all occurences of the length-one string to_remove from the string s. Here are some sample inputs and outputs for this function:

                
  >>> remove_all_occurrences('This is a test', 't')
  'This is a es'
  >>> remove_all_occurrence('----O----', '-')
  'O'
                
              

Palindrome Checker

Write the following function:

def is_palindrome(s)

which returns True if s is a palindrome and False otherwise. As a reminder, a palindrome is a string that reads the same forwards and backwards. Your function should behave as follows on the following inputs:

                    
  >>> is_palindrome('brahm')
  False
  >>> is_palindrome('maps spam')
  True
  >>> is_palindrome('Maps spam')
  False # uppercase 'M' and lowercase 'm' are not the same character
                    
                

As an extension, write a function is_palindrome_without_spaces(s) that returns True if s is a palindrome when all the spaces have been removed from the string.


Adding Commas to Numeric Strings

In many parts of the world, when large numbers are written out, it is traditional to use separator characters to split the digits into groups. In the United States, commas are typically used to separate the digits into groups of three. For example, the number one million is usually written in the following form:

1,000,000

As you can imagine, this is probably a task that is frequently done; to allow programmers to easily display numbers in this fashion, implement the method

def add_commas_to_numeric_string(digits)

which takes a string of decimal digits representing a number and returns the string formed by inserting commas after every third position, from right-to-left.

Here are some sample inputs and outputs of the add_commas_to_numeric_string function:

                  
  >>> add_commas_to_numeric_string('17')
  '17'
  >>> add_commas_to_numeric_string('1001')
  '1,001'
  >>> add_commas_to_numeric_string('12345678')
  '12,345,678'
  >>> add_commas_to_numeric_string('999999999')
  '999,999,999'
                  
              

If you'd like to try your solutions in PyCharm, download a starter project to import here. We've included doctests for you to test with, and running the program itself will run a series of tests.


Questions about Doctests

In class this week, we discussed doctests: ways of specifying the expected output of a function on certain parameters, which provide a convenient way of testing our functions as we write them. Doctests are an incredibly effective tool for programmers, but require careful consideration as we write them. As you go through the quarter, you'll develop a stronger sense of how to test your code, but it's never to early to start thinking about it. If you have time in section, discuss the following questions:

There aren't any correct answers to questions like this: in the solutions for this week's section, we've put together a few of our best ideas, but we encourage you to continue to restrategize on your own as you move through the quarter and start writing larger and larger programs.