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.
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.
Write the following function:
def flip_vertical(image)
that flips a SimpleImage parameter called
image in the vertical direction.
In this program, you will tackle a series of problems intended to increase your
familiarity with parameters and return statements. Implement the
following functions:
def in_range(n, low, high), which takes in three integer parameters and returns
True if n is between low and high
(inclusive of both ends of the range) and False otherwise.
def is_even(n), which takes in an integer parameter n and returns
True if n is even and False. You may wish to use the
% operator, which returns the remainder when dividing one number by the other.
For instance, 7 % 3 evaluates to 1, since the remainder when dividing
7 by 3 is 1.
def is_prime(n), which takes in an integer parameter n and returns
True if n is prime and False otherwise. As a reminder,
a prime number is a number which has no factors other than 1 and itself.
def only_one_even(num1, num2), which takes in two integer parameters and returns
True if and only if exactly one of them is even.
def is_power_of_two(n), which takes in an integer parameter
n and returns True if n is a power of 2 and False
otherwise. A power of 2 is a number which can be produced by repeatedly multiplying 1 by 2
(and consequently, continuously dividing the number by 2 will lead back to 1).
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.
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'
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.
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.
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.