CSc 250 Lab 9

In this lab, you will be tasked with writing several functions that do various tasks with sets, dictionaries and file I/O. You should create one file named lab9.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 union. This function will take two parameters, each will be a set with arbitrary elements. This function will return a new set that is the union of the two. The "union" of two sets is all of the elements from both of the parameter sets. For example, running these tests:
print(union({1, 2, 3}, {3, 4, 5}))
print(union({'apple', 'orange', 'grape', 'fruit'}, {'apple', 'orange', 'kiwi'} ))
Should print out:
{1, 2, 3, 4, 5}
{'orange', 'fruit', 'apple', 'grape', 'kiwi'}

Problem 2

Write a function named intersection. This function will take two parameters, each will be a set with arbitrary elements. This function will return a new set that is the intersection of the two. The "union" of two sets is all of the elements that appear in both of the sets being intersected. For example, running these tests:
print(intersection({1, 2, 3}, {3, 4, 5}))
print(intersection({'apple', 'orange', 'grape', 'fruit'}, {'apple', 'orange', 'kiwi'} ))
Should print out:
{3}
{'orange', 'apple'}

Problem 3

Write a function named one_appearance_names. This function will take a single argument. The argument will be astring of space-separated names (or really, just words in general). This function will determine which of the names appear exactly one, and print them out. This function is not allowed to use lists (other than for splitting the input string), dictionaries, or the count function. In other words, you should use only sets and set operations. For example, if this test case is executed:
one_appearance_names('joe joe sam sam carly carly alex')
one_appearance_names('joe jim anne annie jim joe annie john james john annie luke annie paul sam')
The output should be:
--- names ---
  alex
  
--- names ---
  paul
  james
  luke
  anne
  sam

Problem 4

Write a function named average_grade. This function will accept two parameters. The first will be the name of a csv file, which will contain student grades for a class. The second will be a column number to average. This function will computer the average of all the grades in a particular column in the CSV file (not including the first row of labels). The column name along with the computed average will be printed. For example, create a file named grades.csv in the same directory as your program with these contents.
studentName,hw1,hw2,hw3,hw4,exam1,exam2,final
Jim,30,54,70,40,80,60,50
Anne,50,65,70,82,70,94,90
John,20,71,70,90,87,90,50
Katy,95,97,85,85,91,92,87
Carol,70,80,32,91,40,90,87
With this file in place, running these tests:
average_grade('grades.csv', 1)
average_grade('grades.csv', 3)
average_grade('grades.csv', 5)
Should print:
The average grade for hw1 is 53.0
The average grade for hw3 is 65.4
The average grade for exam1 is 73.6

Problem 5

Write a function named group_words. This function will accept two parameters, the name of an input file and the name of an output file. Ths input file should be a simple text file with one word on each line. The output file is the file we will open and write the results into. The function should open the input file, read in the contents, and then group all of the words based on the letter they begin with. You should also normalize all of the words into lower-case. For example, create a file named fruits.txt in the same directory as your program with these contents.
apple
orange
peach
kiwi
pear
blueberry
blackberry
Orange
pear
Kiwi
kiwi
With this file in place, run this test:
group_words('fruits.txt', 'out1.txt')
Then, the contents of out1.txt should be:
words beginning with a: apple
words beginning with o: orange
words beginning with p: peach pear
words beginning with k: kiwi
words beginning with b: blackberry blueberry
Once you get it working for that test case, try it with this file.

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

Solutions: Solution