-
Write a function
char_freq()
that takes a string and builds a frequency listing of the characters contained in it. Represent the frequency listing as a Python dictionary. Try it with something likechar_freq("abbabcbdbabdbdbabababcbcbab")
. -
Write a function
char_freq_table()
that take a file name as argument, builds a frequency listing of the characters contained in the file, and prints a sorted and nicely formatted character frequency table to the screen. -
The third person singular verb form in English is distinguished by the suffix
-s
, which is added to the stem of the infinitive form:run
->runs
. A simple set of rules can be given as follows:a. If the verb ends in
y
, remove it and addies
b. If the verb ends ino
,ch
,s
,sh
,x
orz
, addes
c. By default just adds
Your task in this exercise is to define a function
make_3sg_form()
which given a verb in infinitive form returns its third person singular form. Test your function with words like try, brush, run and fix. Note however that the rules must be regarded as heuristic, in the sense that you must not expect them to work for all cases. Tip: Check out the string methodendswith()
. -
The file
romeo-and-juliet-excerpt.txt
contains an excerpt from Act I of Shakespeare's Romeo and Juliet. Your mission, should you choose to accept it, is to extract lines of dialogue by each character. Your functionextract_dialogue()
should take a file name and return a dictionary mapping names of characters to a list of their lines:{'Samp': ["Gregory, on my word, we'll not carry coals.", ...], ...}
.a. Note that lines in the file are not necessarily "lines" of dialogue, some take up multiple lines. Hint: by splitting on two linebreaks
'\n\n'
you can identify paragraph breaks. b. Except for some lines that are not dialogue which you have to skip over, most lines now start with a character name. Find a way to separate the character and line. Hint: refer to the documentation of split for helpful optional parameters. c. Collect the resulting lines in a dictionary. For each character, check if it's not yet in the dictionary and add an empty list if not. Then, append the line of dialogue. -
(Optional) In English, the present participle is formed by adding the suffix
-ing
to the infinite form:go
->going
. A simple set of heuristic rules can be given as follows:- If the verb ends in
e
, drop thee
and adding
(if not exception: be, see, flee, knee, etc.) - If the verb ends in
ie
, changeie
toy
and adding
- For words consisting of consonant-vowel-consonant, double the final letter before adding
ing
- By default just add
ing
Your task in this exercise is to define a function
make_ing_form()
which given a verb in infinitive form returns its present participle form. Test your function with words such as lie, see, move and hug. However, you must not expect such simple rules to work for all cases. - If the verb ends in
Created
September 17, 2019 09:11
-
-
Save andreasvc/3c60f904fb364489ecc35654b6e29847 to your computer and use it in GitHub Desktop.
More Python exercises
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment