Practice with Loops
Most of today's exercises are more practice with loops. For these exercises, you don't need to include error-checking. But, if you have time, and you want more experience, after you've written these functions so that they work, go back and add appropriate error-checking code to the definitions.
-
Write
maximum()
which takes a list and returns the largest element in the list. Do not use the built-inmax()
function.Test your
maximum
function using OwlTestWhat happens if you use this on a string, like
maximum("abracadabra")
? What about a list of strings? What about a list containing both numbers and strings? -
Write
evens_list()
which takes a list of numbers and returns a list of all of the even integers in the list.As a first step, how can you use the
%
operator to determine if a number is even? -
Write
n_squares()
that takes a numbern
and returns a list of the squares 02 through (n
-1)2.Hint: When you know exactly how many times you want to loop, you can loop over a list generated by
range()
. -
Mixing flavored drinks, both non-alcoholic and alcoholic, is currently popular. First, define two lists of strings, each containing various drink flavors.
Define
mixed_drinks()
that takes your two lists. It returns a list of all the strings formed by concatenating a flavor from the first list with one from the second list, plus a hyphen to separate them. For example,mixed_drinks(["apple", "strawberry"], ["Coke", "lemonade"])
should return["apple-Coke", "apple-lemonade", "strawberry-Coke", "strawberry-lemonade"]
.Hint: You'll want two loops — one nested inside the other.
-
As another example with nested loops, write a function
print_triangle
that takes a string and prints a triangle like the following. For the input"hello"
it should printh h e h e l h e l l h e l l o
Hint: Decompose the problem into easier pieces. First write a loop that prints one line of this triangle, given the input string and a number of characters to print. Next write an outer loop that prints multiple lines of increasing size. How can you use
range()
in this outer loop?Hint: If you use
print
with a trailing comma, the nextprint
output goes on the same line. For a simple example, tryprint 1, print 2, print 3
Create a modification of your
print_triangle
function calledlist_triangle
that returns the output strings in a list. That is,list_triangle("hello")
would return["h", "he", "hel", "hell", "hello"]
(Note the lack of spaces between the letters.)
Plotting
We've recently added a function to CodeSkulptor to plot functions. In the CodeSkulptor documentation, read the Graphics / SimplePlot section.
-
Use
simpleplot.plot_lines()
on some sample lists until you feel comfortable using it. For example, you could plot a series of numbers and their squares:dataset = [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36)]
By the way, we haven't explained what the difference is between
(2, 4)
and[2, 4]
yet. For right now, the difference doesn't matter, but we'll explain it very soon. -
Look at the Predator-prey slides from last Friday. On slide 10 you'll see a detailed algorithm that uses the math formulas from slide 9.
You'll need a working version of this to use on your current assignment. But, don't worry, we'll give you one in case you have problems writing it yourself.