Processing math: 100%

CPSC120A
Fundamentals of Computer Science

Activity 16

Strings

Exclaim

Create the function append_exclamation(phrase) that returns a string which is the string parameter phrase with an exclamation point appended to the end.

Test Cases

print('Input: ?\tActual:', append_exclamation('?'), '\t\tExpected: ?')
    

Extents

Create the function get_extents(phrase) that returns a string containing the first and last characters of the string parameter phrase in order.

Test Cases

print('Input: ?\tActual:', get_extents('?'), '\tExpected: ?')
    

Password Generation

Passwords are possibly the most important way that individuals can ensure their own safety on the Internet. However, it is also typically the easiest thing for a hacker to figure out, or to get their hands on. This is simply because most Internet users choose incredibly weak passwords. The easiest way to get around this issue to to use a randomly generated password.

Details

Write the Python function generate_password(size) that returns a string of random lower-case letters. It should be possible for the generated password to contain any letter. The parameter size size is a positive integer and is the number of characters the returned password string should contain.

Example

>>> print(generate_password(5))
crkkb
>>> print(generate_password(10))
bgchwxqybn
    
  • The function should repeatedly generate a random character and use string concatenate to accumulate the characters into a single string.

  • There is no built-in way to generate random characters. There is a way to generate random numbers with the random module. Use random numbers and the index operator, [], to copy random characters out of the string "abcdefghijklmnopqrstuvwxyz".

Challenge

Even random strings of lowercase characters are pretty easy to break. A better program would also include upper-case letters and digits. Alter your function so that it is garenteed to include at least 1 lower-case letter, 1 upper-case letter and 1 digit. Note, the location of the required characters should be still be completely random.


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(W/S)+11.8((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.

Examples

>>> print(compute_reading_level('I do not like them Sam I am. I do not like green eggs and ham.'))
-1.4075
>>> print(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
    

Hint

  • Determining the number of words requires counting the number of space characters and determining the number of sentences requires counting the period characters. 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 a space or periord, increment the appropriate counter.

  • 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

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.