Question: Exercise 1 Write a function to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are the

Exercise 1

Write a function to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are the cubes of the keys.

Exercise 2

Write a function that takes two dicts as parameters, and returns one dict with only the key:value pairs in common. If the values are different for a key, they are not included. Sample dictionary: {'key1': 1, 'key2': 3, 'key3': 2}, {'key1': 1, 'key2': 2} Return: {'key1': 1}

Extension: Change the function to take a list of dicts and perform the same logic on any number of dicts.

Exercise 3

Write code that performs a pretend POST for a hypothetical new major in CCT that requires CCT109, 110, and 111. The program should remove any students who don't make POST from dictionary of students. See attached starter code. Remember that a student needs 65% in each required course, as well as a 70% average in the 3 courses to be selected. FYI, the second number is hypothetical and changes each year; this is only a simulation.

To run the starter code, try the following command from a prompt:

pip install names

or

pip3 install names

If all else fails, here's a version with a hardcoded static list of students

Exercise 4

Section 11.2 of the textbook describes a method for creating a histogram that captures the frequency of words in a text file. Observe the code that can be used to accomplish it.

fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() for word in words: if word not in counts: counts[word] = 1 else: counts[word] += 1 print(counts)

Using this approach, analyze ProjectGutenbergFrankensteinShelley.txt. Try another encoding (Links to an external site.) if you're having trouble on Windows.

1. Print out the words of the text in alphabetical order.

2. What are the top 3 words and their counts?

3. You may notice that in the Project Gutenenberg edition, there is some meta-information that will be counted as regular words in the text by default. How could you alter your program to avoid this? (Notice there is a special line in the text). Fix this. Did the counts of the top 3 words change?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!