Don't like this style? Click here to change it! blue.css

Lab 4: Practice by way of five functions

In this lab you will create five separate functions. Turn in one email with 5 links, one link for each function.

Function 1: 2, 9, 16, 23, 30, …

Write a function which consumes an integer n and returns a list of the first n integers in the sequence given by 2, 9, 16, 23, 30, 37, …. Use a while loop (be careful not to go infinite).

For instance: assert arith(7) == [2, 9, 16, 23, 30, 37, 44]

Function 2: Second largest

Write a function which consumes a list of at least 2 integers and returns the second largest integer in the list. Use a for loop.

For instance assert second_largest([1, 2, 3, 4, 5]) == 4.

Function 3: String-Bookends

Write a function which consumes a string containing a single word (no spaces). Return the string with the first and last letters lowercase and all other letters uppercase. For instance assert bookends("BAnanA") == "bANANa".

Function 4: Cavern Dweller

Write a function which consumes a direction (one of "north", "south", "east", and "west") and a distance (a positive integer). It should produce a sentence describing how far you moved. For instance you could return something like: cave_crawl("North", 3) == "You walked 3 miles north, you must be tired."

(I might not assert this one, as you can be creative.)

Function 5: Triangular numbers

Write a function which consumes a positive integer n and returns the sum of the first n positive integers. For example assert triangular(3) == 6.