Python

Instructions

The following exercises require you to use functions, types and classes from the Python standard library.

Exercise 1

Given a string str, we define its ciao (characters in alphabetical order) as the string having the same length of str and containing all the characters of str in lower case and alphabetical order. A ciao string is a string that is equal to its ciao.

  1. Write a function ciao_word that given a string returns its ciao. For example,
ciao_word("Hello") = "ehllo"
  1. Implement the function add_ciao_dict that takes as arguments a dictionary ciao_dict and a string word. The dictionary ciao_dict is assumed to have ciao strings as keys and sets of strings as values: it maps a ciao string str into a set of strings all having str as their ciao. The function add_ciao_dict must add string word to the ciao_dict dictionary.

    Note: You have to correctly handle the case when the ciao of word does not exist as key in ciao_dict. Note that obviously all strings having same ciao are anagrams of each other.

  2. Write the function create_dict that takes as argument the path of a text file f containing a list of words (you can assume that each line contains a single word). The function returns a dictionary mapping a ciao string str to the set of words of f having str as ciao.

    Note: You may use file anagram.txt to test your function. You have to correctly handle white spaces characters like new lines and tabs. Refer to the documentation for information about reading and writing files.

  3. Finally, write the function replace_anagrams that takes as arguments a dictionary dict computed through create_dict and a string line representing a line of text (i.e. a sequence of words separated by spaces). This function returns a text line where each word w1 of line is replaced by a different word w2 having the same ciao of w1 if this ciao exists in dict; otherwise, w1 is left unchanged.

Goals: Fun with Python and warming up!

Expected output: Properly commented Python scripts along with the required implementations.

Exercise 2

This exercise consists in writing a decorator in Python. For a very good introduction to decorators, read the Primer on Python Decorators.

  1. Write a generator fib_gen that returns the Fibonacci sequence.
  1. Implement a higher-order function (decorator) block_ten_dec that given a generator g1 as argument returns a new generator g2. The generator g2 returns the same elements of g1 but in blocks of 10 elements collected in a list.
  1. Decorate generator fib_gen with block_ten_dec. Use the just defined generator to print the first 100 fibonacci numbers, 10 per line.
  1. Following the schema of block_ten_dec implement a decorator block_dec that does the same but it is parametric on the size of the block. Decorate fib_gen with block_dec to get blocks of 5 numbers, and print the first 100 fibonacci numbers, 5 per line.

Expected output: Properly commented Python scripts along with the required implementations.

Author: Andrea Corradini

Created: 2024-11-30 Sat 23:57

Validate