Study Guide In-lab Exam #2 Spring 2014 Updated by Dr. Diesburg on 4/15/2014 The first in-lab exam was designed to see if you understood basic python programming syntax/structure as well as conditionals and iteration. The second in-lab exam will focus on materials from chapters 5-9 and the functional decomposition design materials given out in Lab 11. The main topics include: Writing functions rather than a simple sequence of commands Lists: What are they? How do you add items to a list (.append(), .insert()) How do you remove things from a list (.pop(), and .pop(index)) How do YOU search a list? (writing code that does this, not using native functions) How do YOU sort a list? (writing code that does this, not using native functions) Dictionaries: What are they? How do you access an individual value based on its key? How do you change/set a key to a particular value? How do you get access to all of the keys of a dictionary? How do you get access to all of the values of a dictionary? Sets: What are they? How do you create an empty set? How do you add items to a set? How do you check to see if an item is in a set? How do you use the common features such as union, intersection, and set difference? Sample list programs: Write a function called negate(myList) which takes in a list of numbers as a parameter and negates each value in the list. Since the function changes the list provided as a parameter it does not need to return anything. Write a function called negativeCopy(myList) which takes in a list of numbers as a parameter and returns a newly created list which is contains the negative values of the original list. For example, For example, negativeCopy([ 1, -2, 3.5, 4.2, ] ) would return [-1, 2, -3.5, -4.2] Write a function called addList(myList, addend) which takes in a list of numbers and a single addend as parameters. The function returns a newly created list which is each number in the original list plus the addend. For example, addList([1,2,3.5,4.2] , 3) would return [4, 5, 6.5, 7.2] Write a function called reverseList(myList) which takes in a list of items and returns a newly created list which is the original list in reverse. For example, reverseList([ 1, 2, 3.5, 4.2]) would return [4.2, 3.5, 2, 1] Write a function called removeItem(myList,item) which takes in a list and an item and returns a new list with each occurrence of the item removed from the original list. For example, removeItem([1, 2, “the”, “test”, 3, “is”, “the”, “best” ], “the”) would return [1, 2, “test”, 3, “is”, “best” ] Write a function called sumOf(myList) which takes in a list of numbers as a parameter and returns the sum of all of the values in the list. Write a function called productOf(myList) which takes in a list of numbers as a parameter and returns the product of all of the values in the list. Write a function called count(myList,item) which takes in a list and an item and returns an integer representing how many times the item was in the original list. For example, count([1, 2, “the”, “test”, 3, “is”, “the”, “best” ], “the”) would return 2, but count([1, 2, “the”, “test”, 3, “is”, “the”, “best” ], “Another”) would return 0. (note, I am asking you to do the work here, not use native functionality). Write a function called mean(myList) which takes in a list of numbers and returns a decimal value which is the mean (the statistical “average”) of the numbers in the list. Write a function called median(myList) which takes in a list of numbers and returns the value which is the arithmetic median of that list (the “middle” value when the list is sorted into order) Write a function called mode(myList) which takes in a list of numbers and returns the value which occurs in the list the most times. Sample dictionary programs: Write a function called keyCount(myDictionary) which takes in a dictionary as a parameter and returns an integer representing the number of keys in the dictionary Write a function called valueCount(myDictionary) which takes in a dictionary as a parameter and returns an integer representing the number of UNIQUE values in the dictionary. Write a function called valueCount(myDictionary,value) which takes in a dictionary as a parameter and returns an integer representing how many times THAT value is a value in the dictionary. Write a function called setValue(myDictionary, key, newValue) which takes in a dictionary as a parameter, locates the key in the dictionary and replaces the existing value with the newValue parameter. Write a function called mostCommon(myString) which takes in a string and returns the most common letter in that string. Write a fucntion called letterCounts(mystring) which takes in a string and returns a dictionary of letter counts in that string. Sample set programs: Write a function called commonLetters(name1,name2) that takes in the names of two people. It then returns a list of the letters common to both names. Write a function called boxOfCrayons(colors1,colors2) that takes in two lists of colors and returns the list of all the colors from the two lists. Write a function called howFarAhead(student1,student2) that takes in two lists of courses completed by two students. It should return a list of all of the courses that student1 has completed that student2 has NOT completed.