Thanks to Dr. Alan McLeod for the content of this lab.

These exercises will give you some more practice with functions, strings and lists. Of course you will also need to use loops and conditionals! Full solutions are provided for most exercises, but as always, you should try the exercise first before looking at the supplied code. The exercises start simple and get more complicated, as usual.

Note that the sample solutions are not as fully commented as they should be, nor do they always use the best variable names, so you need to really examine the code (and I was lazy). Good programming style and "readability" really do make a difference when looking at someone else's work.

Exercise 1

Here is a program that checks the sides of a triangle to see if they would make a legal triangle and then calculates the area. Write a function that accepts the three side values in a list and then returns a boolean True if the triangle would be legal or False if not. The function should not contain any calls to print(...). The function must also return False if the list is composed of anything other than 3 numbers. Replace the code in main() that checks the sides of the triangle with a call to this function.

Add a second function that calculates and returns the area of a triangle, assuming the supplied list of side dimensions represents a legal triangle. Replace the code in main() that calculates the area with a call to this function. (Answer: Exercise1.py - notice how the entries in the list are assigned to variables!)

Exercise 2

For Exercise 3 of the Week 5 lab you needed to use the random module to generate random numbers. This time, in addition to using random.randint(...), invoke random.seed() once first to set a random seed value for the random number generator. Write a function that accepts the low end of a range, the high end of a range (as integers) and the number of numbers desired. This function will then return a list of random integer numbers in the range specified of the size specified. Test this function in main() (using fairly small lists!) You can assume that only legal parameters will be supplied.

Write a second function that calculates and returns, as a float, the average value of any size supplied list. Generate a larger size list (1,000 elements?) in main() and compare the average of the data to the mid value of the range supplied. How good is this random number generator? Increase the size of the list to see how this affects the average value. Run your program several times for each size list to get an idea of how close the average comes to the value it should be.

Write a third function that returns the median value of the list. To do this you must sort the list and then extract the value from the exact middle position in the list.

Write another function that accepts a number (entered by the user in main()) and a sorted list of any size and returns a list consisting of just the occurrences of that number in the list, along with the closest lesser value and the closest greater value. For example, if the number occurs 3 times in the list, the returned list would consist of that number, the next smaller value and the next greater value, to give a list of size 5. If the number is not found in the list, return a list of size 2 containing the number that would occur just before the number and the one that occurs just after. (Answer: Exercise2.py)

Exercise 3

Python provides a number of useful string methods which we have not yet explored in lecture (other than format(...)). To see a list of string methods and their descriptions, select Python Manuals from the Python program group or Help -> Python Docs from within IDLE. Click on the Index tab and enter "string" in the text box. Double-click on the methods option shown in the list. Take a look at what is available; there's quite a bit here! We'll talk more about strings and some of these methods in a future lecture.

Write a function with a single parameter that is a line of string text. The function must go through each character in the string and count the number of lowercase characters, uppercase characters, digits and spaces. The function must return these values in a single four-element tuple.

Write a main() function which asks the user to enter a line of text. Call the function created previously and capture the returned tuple. Print the analysis of the string on the screen using the four values in the tuple; you should not use a for loop for this part, just four calls to print(...). (Answer: Exercise3.py)

More Exercises

You should be able to do any of the Programming Exercises at the end of Chapter 6. Problems 4 and 5 look pretty easy. Problems 6, 7 and 10 look a bit more difficult. From the Chapter 8 Programming Exercises, you should be able to do problems 9, 11 and 12.

For example, here is problem 6 from Chapter 6:

Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following functions in the program:

Here is a solution.