CSc 250 Lab 8

In this lab, you will be tasked with writing several functions that do various tasks with dictionaries. You should create one file named lab8.py and put all of the functions you complete, along with the test-cases, in the same file. Submit just this file to D2L when you are finished.

Problem 1 (warm-up)

Write a function named print_dictionary. This function will take one parameter - a dictionary where the keys and values can be any type. This function will simply loop through all the key/value pairs and print them out. The formatting should match the example(s) shown below. Running these tests:
data = {1:2, 3:4, 5:6, 7:8}
print_dictionary(data)
data = {'joe': 123, 'sam':811, 'jill':708}
print_dictionary(data)
Should print out:
--- dictionary contents ---
         1 -> 2
         3 -> 4
         5 -> 6
         7 -> 8
---------------------------
--- dictionary contents ---
       joe -> 123
       sam -> 811
      jill -> 708
---------------------------

Problem 2

Write a function named count_nums. This function will take one argument - a list of integer numbers. The function will use a dictionary to count how many times each number occurs. It will then print out all of the counts. For example, running these tests:
nums = [1, 3, 7, 1, 3, 5, 9, 3, 3, 7, 5, 6, 9, 1, 4, 8, 1]
count_nums(nums)
print('-----')
nums = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 5, 1]
count_nums(nums)
Should produce the output:
1 appears 4 times
3 appears 4 times
7 appears 2 times
5 appears 2 times
9 appears 2 times
6 appears 1 times
4 appears 1 times
8 appears 1 times
-----
1 appears 4 times
2 appears 2 times
3 appears 2 times
4 appears 2 times
5 appears 3 times

Problem 3

Write a function named lowest_highest_score. This function will accept a single parameter, which is a dictionary that represents the class grades of multiple students. The keys will be the student names, and the values will be the students' current grade in the class (out of 100). This function should find the student with the lowest and the highest grade in the class, and print their names and grades/ For example, if this test case is executed:
students = {'jim':80, 'annie':75, 'jane':95, 'jake': 97, 'adam':50, 'lily':81}
lowest_highest_score(students)
The output should be:
 Lowest grade: adam : 50%
Highest grade: jake : 97%

Problem 4

Write a function named get_elements_in_both. This function will accept two parameters, each of which will be a dictionary. This function will create a new dictionary, and add only key/value paris that exist in both of the two parameter dictionaries. This new dictionary will be returned. For example, if this test case is executed:
a = {1:1, 2:2, 3:3}
b = {1:2, 3:4, 5:5, 2:2, 1:1}
print(get_elements_in_both(a, b))
The output should be:
{1: 1, 2: 2}

Problem 5

Write a function named combine. This function will accept three parameters, each of which will be a dictionary. This function will create a new dictionary which will "combine" the key/value pairs from all three of these parmaeter dictionaries. This new dictionary will be returned. For example, if this test case is executed:
a = {1:1, 2:2}
b = {1:1, 3:3}
c = {0:0, 2:2}
print(combine(a, b, c))
The output should be:
{1: 1, 2: 2, 3: 3, 0: 0}

You must turn in at least 4 of the problems on D2L to get credit for this.

Solutions: Solutions.zip