CPSC120A
Fundamentals of Computer Science I

Lab 17

Traversal and Slice

Use the command line to create a new directory called lab17 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Reading Level

Some states require that legal documents not be written above a certain grade level. The Flesch-Kincaid grade level is a numeric score that indicates the minimum grade level in which an average student would be able to understand a text. Writing a function that computes the grade level of text requires being able to count the number of occurrences of certain characters.

Details

In a file called reading_level.py write the function compute_reading_level(text). The function should return the Flesch-Kincaid grade level of the input text. The Flesch-Kincaid grade level equation requires determining the number of syllables in a sentence, which is very difficult for a program to compute without a dictionary. The following equation is an approximation to the Flesch-Kincaid grade level that does not use syllables:

$$0.39\cdot(W/S)+11.8\cdot((L/3)/W)-15.59$$

Where \(W\) is the total number of words, \(S\) is the total number of sentences, and \(L\) is the total number of letters. Assume that all sentences end in a period, that all words are separated by a single space, and that all characters that are not a space or period are letters.

Sample Test Cases

Function Parameters Expected Output
'I do not like them Sam I am. I do not like green eggs and ham.' -1.4075
'To be or not to be that is the question. Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune or to take arms against a sea of troubles and by opposing end them.' 7.445769230769233

Hint

  • Determining the number of words requires counting the number of space characters and determining the number of sentences requires counting the period characters. Instead of writing code to count each of these characters separately, write a function count_character(character, text) that counts the number of times character occurs in text. The function should use an accumulator variable to count and a for loop to traverse the text one character at a time. For each character in the text that is equal to input character, increment the counter.

  • Use the above function to determine the number of words and sentences. Determine the number of letters by using the length of the input text and the number of non-alphabetic characters. Before computing the reading level, test your code by printing the number of words, sentences, and letters on short examples.

Challenge

The character count function was easy to write because it assumes that the input text only has alphabetic characters, single space characters, and period characters. This isn't very useful because most text does not conform to these restrictions. Modify the function so that it can work for any text. It should:

  1. allow any number of space, tab, or new line characters between words and sentences.
  2. allow sentences to end in a period, question mark, or exclamation point.
  3. ignore all non-alphabetic characters when counting letters.
Pig Latin Translator

Pig Latin is a somewhat silly language parody that is actually entirely English based. It is a simple transposition of letters that gives the effect of a "foreign" sounding language. However, even Thomas Jefferson couldn't resist writing letters to his friends in Pig Latin. The rules for converting an English word to Pig Latin are incredibly easy:

Details

Write a function called convert_to_pig_latin(text) in a file called pig_latin.py. This function takes a string as a parameter, and returns a string which is the pig latin translation of the text parameter.

Make sure your program handles all necessary cases gracefully. What additional test cases should you check?

Sample Test Cases

Function Parameters Expected Output
"scram" am'scray
"immediate" immediateyay

Hint

  • Before converting the input text to Pig Latin, the function must find the index of the first vowel. Finding the index of the first vowel would be easier if there was a function that returned whether a single character is a vowel. So write the function is_vowel(character). The function has one parameter character an alphabetic string of length 1. The function should return True if character is 'a', 'e', 'i', 'o', or 'u' and False otherwise. Test this function before proceeding.

  • Finding the index of the first vowel requires traversing the text and testing whether each character is a vowel. Writing this as a separate function would make writing the translation function easier. So write the function index_of_first_vowel(text) that returns the index of first vowel in the specified text. Since the location of the first vowel is unknown, the number of times the traversal loop will need to run is unknown and so the function will not be able to use a for loop to traverse the string. Instead, the function should use a while loop that increments an index variable every iteration while the character at the index variable is not a vowel.

  • With the above two functions, writing the translation function is simple. If the index of the first vowel is 0, then apply the first Pig Latin rule using concatenation. If the index of the first vowel is not 0, then apply the second Pig Latin rule using a slice and concatenation.

Challenge

Ofyay ourse'cay, ityay akes'may ittle'lay ense'say o'tay ite'wray ayay ogram'pray o'tay anslate'tray ayay ord'way o'tay ig'pay atin'lay ifyay ityay annot'cay anslate'tray ack'bay e'thay otheryay ay'way! Ite'wray ayay unction'fay onvert_from_pig_latin'cay at'thay akes'tay ayay ord'way alreadyyay inyay ig'pay atin'lay, andyay eturns'ray e'thay Englishyay equivalentyay.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.