Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
More Python exercises
  1. 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 like char_freq("abbabcbdbabdbdbabababcbcbab").

  2. 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.

  3. 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 add ies b. If the verb ends in o, ch, s, sh, x or z, add es c. By default just add s

    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 method endswith().

  4. 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 function extract_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.

  5. (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:

    1. If the verb ends in e, drop the e and add ing (if not exception: be, see, flee, knee, etc.)
    2. If the verb ends in ie, change ie to y and add ing
    3. For words consisting of consonant-vowel-consonant, double the final letter before adding ing
    4. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment