Lecture Exercises (20%)
Show the following exercises you were to create during the lecture:
- Basic Lists (25%)
Go to the website http://codingbat.com/python/List-2 and complete all List-2 exercises:
count_evens big_diff centered_average
sum13 sum67 has22
- Lists and Functions (25%)
Answers to the following questions should be implemented as functions in a program called lists.py.
Write a function largerThan that takes a list alist and a number num as arguments and returns the new version of list consisting of all alist elements larger than num. For example, if a list containing the values [1, 2, 4, 2, 7] and a value 2 are passed to the function, it returns a list [4, 7].
Write a function called numDistinct that takes a single list parameter alist and returns a list containing distinct values in alist. For example, if a list containing the values [1, 2, 4, 2, 7] is passed to the program, it returns a list [1, 2, 4, 7]. You are not allowed to use a dictionary while solving this problem.
Write a function called normalizeList that takes a single list parameter and replaces all negative values in that list with zeros and all numbers greater than a hundred with a 100. All other values in range 0-100 are to remain intact. For example, if the list contains [ 0, 35, -23, 100, 200 ], then after the function executes, it should contain [ 0, 35, 0, 100, 100 ]. You are to assume that the list argument contains numbers only. Since all the changes you make to the list argument will be visible in the calling block, this function does NOT need to have the return statement.
- Problem solving with lists, strings, functions, and files (30%)
Pig Latin is a language game of alterations played in English. To form the Pig Latin form of an English word:
if a word begins with a vowel, the ending way is added to the word, e.g. if becomes ifway
if a word begins with a consonant, the consonant is moved to the end of the word and the ending ay is added to the word, e.g. beast becomes eastbay
Write a program organized into functions that first reads a file containing words line by line from a text file called myPigLatinTest.txt. Assume that words are delimited by white spaces and that you only have valid words in this file.
When you read each line, convert it to a list of words using the method split.
Then, the program determines Pig Latin equivalents of the words in the list and prints them to an output file called PigLatinResults.txt.
For example, if the original file list contains:
if beast student
then the output file contains
ifway eastbay tudentsay
—– THE END —–