Activity - Practice with Chapter 4 and 5

Create a program called practice.py. Your code will accomplish the following tasks.

Pig Latin

  1. Write a function called pig_latin_word(word) that takes a single word as a string, then converts the word to Pig Latin and returns it. Recall the basic rules of Pig Latin: take the first letter of a word, put it at the end, then append “ay”. The only exception is if the first letter is a vowel, in which case we keep it as is and append “hay” to the end. For example, “boot” in Pig Latin is “ootbay”, and “image” is “imagehay”. It will be helpful to consider a container of vowels so that you may quickly check if a letter vowel. Additionally, you may use the slice operator to grab only parts of a string.

  2. Write a function called pig_latin(line) that takes an entire line of text as a string (including several words), and converts the text to Pig Latin. Hint: Separate the line into words, and call your previous function for each word to build up a string that is returned.

String analysis

  1. Write a function group_by_first_letter(words) that takes a list of words and returns a dictionary where the keys are the first letters of the words, and the values are lists of words starting with that letter. The output should be case-insensitive. For example,
     words = ["apple", "banana", "Avocado", "berry", "apricot", "blueberry"]
     d = group_by_first_letter(words)
     print(d)
    

    would output

     {'a': ['apple', 'Avocado', 'apricot'], 'b': ['banana', 'berry', 'blueberry']}
    
  2. Write a function most_often_first_letter(words) that takes a list of words and returns the letter of the alphabet which occurs most frequently in that list of words, regardless of case. Hint: Use the function you created for the previous problem. Then, iterate over the dictionary to find which key has the largest value. For example,
     words = ["apple", "banana", "Avocado", "berry", "apricot", "cherry"]
     print("The most often occuring first letter is: ", most_often_first_letter(words))
    

    would output

     The most often occuring first letter is: a
    
  3. Write a function called word_occurrences(myString) that takes one string argument consisting of words (only letters capital or lowercase and spaces). The function should return a dictionary in which each key represents a word from the string normalized to lowercase, and the value represents the number of times that the word appeared in the string. Call your function to test it using the input below.
     s1 = "Hi You how are you"
     print( word_occurrences(s1) ) # outputs {'hi': 1, 'you': 2, 'how': 1, 'are': 1}
    
     s2 = "in the cat in the hat"
     print( word_occurrences(s2) ) # outputs {'in': 2, 'the': 2, 'cat': 1, 'hat': 1}
    

Optional

If you finish early, try the following problems.

  1. Add some additional Pig Latin rules to your converter. For example, words that start with “th”, “st”, “qu”, “pl” or “tr” should move both of those letters to the end. For example, “stop” is “opstay”, and “there” is “erethay” in Pig Latin. There are many other Pig Latin rules that you can find online if you want a true converter.

  2. Write a function count_consonants(myString) that takes one string argument and returns the number of consonents (vowels are “a”, “e”, “i”, “o”, “u”; consonants are all other letters of the alphabet). Case does not matter. Call your function to test it using the input below.
     print( count_consonants("Hello there!") )
     print( count_consonants("hi") )
    

    would output

         6
         1
    
  3. Write a function reverse_words(sentence) that takes a string containing multiple words and returns a string where the order of the words is reversed. Call the function to test it using the given input. For example,
     reversed = reverse_words("Python is great for scientific computing")
     print(reversed)
    

    would output

     computing scientific for great is Python
    
  4. Write a function filter_even_numbers(myList) that takes a list of integers and returns a new list containing only the numbers from myList that were even. Call the function to test it using the given input. For example,
     numbers = [1, 2, 3, 4, 5, 6]
     newList = filter_even_numbers(numbers)
     print("Original list:")
     print(numbers)
     print("Filtered list:")
     print(newList)
    

    would output

     Original list:
     [1, 2, 3, 4, 5, 6]
     Filtered list:
     [2, 4, 6]
    

How to submit

Make sure you saved your work and that it runs without errors. Submit your python file to Moodle.