# Exercises¶

1. Given the function:

def function(arg):
return arg


what is the type of the result of the following calls?

1. function(1)
2. function({"1A3A": 123, "2B1F": 66})
3. function([2*x for x in range(10)])
4. function(2**-2)
2. Given the function:

def sum(a, b):
return a + b


what is the type of the result of the following calls?

1. sum(2, 2)
2. sum(range(10), range(10, 20))
3. sum("I am a", "string")
3. Create a function print_even_odd() that, given an integer, prints to screen "even" if the number is even, and "odd" otherwise.

What is the result if we write:

result = print_even_odd(99)
print result

4. Create a function determine_even_odd() that takes an integer and returns the string "even" if the number is even, and the string "odd" otherwise.

What is the result if we write:

determine_even_odd(99)

5. Create a function check_alphanumeric() that takes a string and returns True if the string is alphanumeric (contains only alphabetic or numeric characters) and False otherwise.

To check whether a character is alphanumeric, use in and the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".

Hint. Lower case characters can be alphanumeric too!

6. Create a function question() that doesn’t take any argument, asks the user for a file path and prints to screen the contents of the file. (Test the function with e.g. the file data/aatable.)

7. Create a function wc() that takes a string and returns a triplet (tuple) of values:

1. The first element should be the length of the string.
2. The second element should be the number of newline characters in the string.
3. The third element should be the number of words (separated by spaces or newlines) in the string.
8. Create a function print_dictionary() that takes a dictionary and prints to screen the key-value pairs of the dictionary, formatted as in the example below.

Example: when applying print_dictionary() to:

dictionary = {
"Arg": 0.7,
"Lys": 0.1,
"Cys": 0.1,
"His": 0.1,
}


a histogram of frequencies, the result should be:

>>> print_dictionary(dictionary)
His -> 10.0%
Cys -> 10.0%
Arg -> 70.0%
Lys -> 10.0%


Here the order in which lines are printed is not relevant.

9. As before, but keys should be ordered alphanumerically:

>>> print_ordered_dictionary(dictionary)
Arg -> 70%
Cys -> 10%
His -> 10%
Lys -> 10%


Hint: it would be convenient to extract dictionary keys, order them alphanumerically, then iterate on ordered keys printing each time the corresponding line.

10. Create a function create_list_of_factorials() that takes an integer n, and returns a list of n elements.

The i -th element has to be the factorial of i.

For example:

>>> list = create_list_of_factorials(5)
>>> print len(list)
5                                       # 5 elements, as requested
>>> print list[0]
1                                       # the factorial of 0
>>> print list[1]
1                                       # the factorial of 1
>>> print list[2]
2                                       # the factorial of 2
>>> print list[3]
6                                       # the factorial of 3
>>> print list[4]
24                                      # the factorial of 4


Hint: it would be convenient to use the function factorial() defined in one of the previous examples, to calculate the values of the list.

11. Create a function count_character() that takes two strings, the first representing text, the second representing a character.

The function has to return the number of occurrences of the character in the text.

For example:

>>> print count_character("abbaa", "a")
3                                           # "a" appears 3 times
>>> print count_character("abbaa", "b")
2                                           # "b" appears 2 times
>>> print count_character("abbaa", "?")
0                                           # "?" never appears

12. Create a function count_characters() that takes two strings, the first representing text, the second representing a set of characters.

The function has to return a dictionary, where the characters to be found are the keys, and their occurrences in the text are the corresponding values.

For example:

>>> print count_characters("abbaa", "ab?")
{"a": 3, "b": 2, "?": 0}

13. Create a function distance() that takes two pairs of values (x1,y1) and (x2,y2) representing the two-dimensional coordinates of two points, and returns their Euclidean distance.

Hint. Euclidean distance is calculated as $$\sqrt{(x1-x2)^2 + (y1-y2)^2}$$

14. Create a function substring() that, given two strings, returns True if the second is a substring of the first.

15. Create a function non_empty_substrings() that, given one string, returns a list of its non empty substrings.

16. Create a function count_substrings() that, given two strings haystack and needle, returns the number of occurrences of needle in haystack.

17. Create a function longest_substring() that, given two strings, returns their longer common substring.

Hint. You can solve this using the previous exercise!

# Exercises¶

1. Write a program that takes:

1. a path to a PDB file, which describes the atomic structure of a

multi-chain protein)

2. two protein chain identifiers (chain1 and chain2)

3. a distance threshold threshold

The program should print the positions, coordinates, and distance of the atoms in the two chains whose Euclidean distance is closer than threshold.

Use the (modified) PDB file of insulin linked here to test your program.

Extract of the insulin PDB file:

#         atom    chain          x       y       z
#          v        v            v       v       v
ATOM     1 N    GLY A   1      -8.932  16.943  14.339  1.00  15.82  N
ATOM     2 CA   GLY A   1      -9.595  17.000  13.020  1.00  13.75  C
ATOM     3 C    GLY A   1      -9.764  15.600  12.498  1.00  13.02  C
ATOM     4 O    GLY A   1      -9.598  14.645  13.247  1.00  11.89  O
...
ATOM    10 N    ILE A   2     -10.091  15.513  11.206  1.00  11.41  N
ATOM    11 CA   ILE A   2     -10.396  14.228  10.611  1.00  10.30  C
ATOM    12 C    ILE A   2      -9.248  13.293  10.677  1.00   9.25  C
ATOM    13 O    ILE A   2      -9.434  12.049  10.899  1.00  10.34  O
...
ATOM   298 N    ASN A  21     -12.001  17.750  -2.988  1.00  11.08  N
ATOM   299 CA   ASN A  21     -12.128  19.024  -3.668  0.58  13.56  C
ATOM   300 CA   ASN A  21     -11.958  19.077  -3.600  0.42  11.76  C
ATOM   301 C    ASN A  21     -13.435  19.106  -4.464  0.58  18.94  C
ATOM   302 C    ASN A  21     -13.065  19.211  -4.640  0.42  15.32  C
ATOM   303 O    ASN A  21     -13.993  18.044  -4.817  0.58  17.75  O
ATOM   304 O    ASN A  21     -14.121  18.561  -4.467  0.42  16.85  O
...
ATOM   327 N    PHE B   1     -21.703   1.132   3.507  1.00  14.54  N
ATOM   328 CA   PHE B   1     -20.263   1.256   3.871  1.00  11.12  C
ATOM   329 C    PHE B   1     -20.090   1.071   5.370  1.00  12.49  C
ATOM   330 O    PHE B   1     -21.062   1.257   6.129  1.00  20.68  O
...