Practice 1
Write a function is_substring(word_1, word_2)
. Your
function should use the in
operator to determine if the
string word_1 is a substring
of word_2. word_1 is a substring
of word_2 if all of the characters of word_1
exist sequentially inside of word_2.
Practice 2
Write a function sum_ord(some_text)
. Your
function should use the for character in word
style of
loop to sum the ordinal values of all of the characters
in some_text.
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 timescharacter
occurs intext
. 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:
- allow any number of space, tab, or new line characters between words and sentences.
- allow sentences to end in a period, question mark, or exclamation point.
- ignore all non-alphabetic characters when counting letters.
Most of us like to keep secrets. For example, I don't want you to know my credit card number. I'd rather you not be able to buy things with my money. However, communication over the Internet is like trying to shout in a crowded room. Everyone can hear what you are saying. So, to protect our valuable secrets, we rely on encryption schemes. Today, you are going to implement a simplistic encryption scheme known since Roman times: Caesar cipher.
Details
In a file called caesar.py write the
function encrypt_word(a_word)
. This function takes a
single string as a parameter. Your function should return the
encryption of the a_word parameter.
Encryption in the Caesar schema relies on shifting letters in the alphabet. By default, this shift is 3 letters. So, \(a\) becomes \(d\), \(b\) becomes \(e\), etc. The only issue with this is at the end of the alphabet. The end of the alphabet wraps around to the front. So, \(x\) becomes \(a\), \(y\) becomes \(b\), and \(z\) becomes \(c\).
Sample Test Cases
Function Parameters | Expected Output |
---|---|
hello | -khoor |
goodbye | jrrgebh |
Hint
-
The
ord
function gives you the ASCII representation of a character. If you subtract theord('a')
from this, you will get the position in alphabet a character is. -
You can use
%
to make the cyclic property of the algorithm works. There are 26 letters in our alphabet. -
The
chr
function takes an integer, and returns to you the character that represents the ASCII value.
Challenge
Encryption for Caesar is useful, but in order to talk to anyone
they need to be able to decrypt the Caesar encryption. Add a
function decrypt_word(a_word)
, which returns the
decryption of the a_word.
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.