Programming 1, 2021-22
Week 7: Exercises

1. Same Values

Write a function same_values(l, m) that returns True if the lists l and m contain the same values, i.e. every element of l is in m and every element of m is in l. You may assume that all list elements are immutable.

2. Most Common Word

Write a program that reads text until the end of input is reached, and prints out the most common word in the input text, along with its number of occurrences.

3. Most Common Characters

Write a program that reads a string and print out the five most common characters in the string along with their occurrence counts, e.g.

e 23

t 20

o 18

n 15

i 14

Write the characters in decreasing order of frequency. The string may contain any Unicode characters.

4. Duplicate Values

  1. Write a function that takes a list of integers and returns True if there are any duplicate values in the list. Use sorting to accomplish this task. Do not modify the original list.

  2. Write a function with the same behavior, using a set to accomplish the task.

  3. Which implementation do you think will be faster on a large list in the best case? In the worst case?

  4. As an experiment, generate a list of 1,000,000 integers in the range from 1 to 1,000,000,000, then call both of the above functions on your list. How does the exection time compare?

  5. Modify the experiment so that all the integers your generated list are unique. Now run both functions on your list. How does the execution time compare?