Python assignments
1. Define a procedure histogram() that takes a list of integers and prints a histogram to the
screen. For
example, histogram([4, 9, 7]) should print the following:
****
*********
*******
2. Write a version of a palindrome recognizer that also accepts phrase palindromes such as
"Go hang a salami I'm a lasagna hog.", "Was it a rat I saw?", "Step on no pets", "Sit on a
potato pan, Otis", "Lisa Bonet ate no basil", "Satan, oscillate my metallic sonatas", "I roamed
under it as a tired nude Maori", "Rise to vote sir", or the exclamation "Dammit, I'm mad!".
Note that punctuation, capitalization, and spacing are usually ignored.
3. A pangram is a sentence that contains all the letters of the English alphabet at least once, for
example: The quick brown fox, jumps over the lazy dog!!!!.
Your task here is to write a function to check a sentence to see if it is a pangram or not.
4. Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's
language").
That is, double every consonant and place an occurrence of "o" in between.
For example, translate("this is fun") should return the string "tothohisos isos fofunon".
5. Write a program that contains a function that has one parameter, n, representing an integer
greater than 0. The function should return n! (n factorial). Then write a main function that calls
this function with the values 1 through 20, one at a time, printing the returned results. This is
what your output should look like:
11
22
36
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 628800
6. Write a recursive sum from 1 to x (i.e. 1 + 2 + ... + x) recursively as follows for integer x ≥ 1:
1, if x = 1
x + sum from 1 to x-1 if x > 1
Complete the following Python program to compute the sum 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
+ 10
def main():
# compute and print 1 + 2 + ... + 10
print sum(10)
def sum(x):
# you complete this function recursively
main()
7. Define a function overlapping() that takes two lists and returns True if they have at least one
member in common, False otherwise.
8. Write a function find_longest_word() that takes a list of words and returns the length of the
longest one.
9. Write a function filter_long_words() that takes a list of words and an integer n and returns the
list of words that are longer than n
10. Define a simple "spelling correction" function correct() that takes a string and sees to it that
1) two or more occurrences of the space character is compressed into one, and
2) inserts an extra space after a period if the period is directly followed by a letter.
e.g. correct("This is very funny and [Link]!")
should return "This is very funny and cool. Indeed!"
11. In English, present participle is formed by adding suffix -ing to infinite form: go -> going. A
simple set of heuristic rules can be given as follows:
If the verb ends in e, drop the e and add ing (if not exception: be, see, flee, knee, etc.)
If the verb ends in ie, change ie to y and add ing
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.