CPSC120
Fundamentals of Computer Science

In-class Activity 24

  1. Create a Python function that takes a string and returns a dictionary where each key in the dictionary is a letter in the alphabet and each value is the frequency of the corresponding key’s occurrence in the input string. The frequency of a character is the number of times it occurs in the string divided by the total number of characters in the string. The function should ignore case and non-alphabetic characters.

  2. Create a Python function that takes a string and prints the letters of the alphabet in descending order of how frequent they are in the input string.

  3. What would the following snippet of Python code print if run?

    spam = {"zero": 0, "one": 1, "two": 2}
    print(spam["zero"])
  4. What would the following snippet of Python code print if run?

    spam = {"zero": 0, "one": 1, "two": 2}
    print(spam[1])
  5. What would the following snippet of Python code print if run?

    spam = {"zero": 0, "one": 1, "two": 2}
    print(spam["three"])
  6. What would the following snippet of Python code print if run?

    spam = {"zero": 0, "one": 1, "two": 2}
    print(max(spam.values()))
  7. What would the following snippet of Python code print if run?

    spam = {}
    ham = [1, 2, 3, 4, 5, 6]
    for i in ham:
        spam[i // 2] = i
    keys = list(spam.keys())
    keys.sort()
    print(keys)