CPSC120
Fundamentals of Computer Science

Activity 21

Strings

Extents

Create the function get_extents(phrase: str) -> str that returns a string containing the first and last characters of the string parameter phrase, in order, as a single string.

Test Cases

import test

def get_extents(phrase: str) -> str:
    # Put your code here

def main() -> None:
    test.equal(get_extents("hello"), "ho")
    # Put more test cases here
    return None

main()

Count Character

Create the function count(character: str, text: str) -> int that returns the number of times that character occurs in text. The function should assert that the character string is of length 1.

Test Cases

import test

def count(character: str, text: str) -> int:
    # Put your code here

def main() -> None:
    test.equal(count('e', "hello everybody!"), 3)
    # Put more test cases here
    return None

main()

Reading Level

Some states require that legal documents not be written above a particular 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 writing requires counting the number of character occurrences.

Details

Write the function compute_reading_level(text: str) -> float. 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 all sentences end in a period, all words are separated by a single space, and all characters that are not space or period are letters.

Test Cases

import test

def compute_reading_level(text: str) -> float:
    # Put your code here

def main() -> None:
    test.equal(compute_reading_level("I do not like them Sam I am. I do not like green eggs and ham."), -1.4075)
    test.equal(compute_reading_level("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)
    return None

main()

Hint

  • Determining the number of words requires counting the number of space characters.

  • Determining the number of sentences requires counting the period characters.

  • Use the count function you created to count the spaces and periods.

  • Determine the number of letters using the length of the input text and the number of non-alphabetic characters.

Challenge

Modify the function so that it can work for any text. It should:

  1. allow any number of space, tab, or newline 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.