BYU logo Computer Science

To start this lab, download this zip file.

Lab 23 - Grouping

Activities

last_letter.py

For a given list of words, group the words by the last letter of the word.

Return a dictionary mapping last letter to the words with that last letter.

Example

The list

['who', 'you', 'too', 'moo', 'queue', 'stew', 'rue']

would result in

{'o': ['who', 'too', 'moo'], 'u': ['you'], 'e': ['queue', 'rue'], 'w': ['stew']}

student_majors.py

Given a dictionary mapping students to their major, return a dictionary mapping majors to a list of all students of that major.

Example

Input

{'Jane': 'CS', 'Joe': 'Biology', 'Jill': 'Biology', 'Jack': 'CS'}

Output

{'CS': ['Jane', 'Jack'], 'Biology': ['Joe', 'Jill']}

vowel_count.py

Group a list of words by the number of vowels in each word.

Example

Input

['bear', 'koala', 'panda', 'cub', 'cougar', 'hippopotamus']

*Output

{1: ['cub'], 2: ['bear', 'panda'], 3: ['koala', 'cougar'], 5: ['hippopotamus']}

topics_referenced.py

Write a function that counts the number of times a topic is referrenced in a file.

The name of the file and a dictionary mapping words to topics are inputs.

If a word from the file does not have a topic (i.e. it is not in the topic dictionary), ignore it.

Example

Input

A file:

dog cat car pizza

Topics:

{'dog': 'pet', 'cat': 'pet', 'pizza': 'food'}

Output

{'pet': 2, 'food': 1}

first_vowel.py

Group a list of words by the first vowel in the word, then return the group that has the most words.

Example

Input

['apple', 'dog', 'cat', 'horse', 'fish', 'moose']

Output

['dog', 'horse', 'moose']

letter_count.py

Group a list of words by the maximum repeated-letter count.

So, for any word, determine the letter with the highest frequency.

Now, group the words by the word’s maximum letter-frequency.

For example:

  • tumultuous goes in group 4 (four ‘u’)
  • banana goes in group 3 (three ‘a’)
  • apple goes in group 2 (two ‘p’)
  • cat goes in group 1 (max count of any letter is 1)

Example

Input

['misissippi', 'dog', 'hmmmm', 'goof', 'apple']

Output

{4: ['mississippi', 'hmmmm'], 1: ['dog'], 2: ['goof', 'apple']}

Grading

ActivityPoints
last_letter.py5
student_majors.py5
vowel_count.py5
topics_referenced.py5
first_vowel.py10
letter_count.py10