Foundation

A foundation wants to carry out a control of the telephone calls of its executives. There is a file of calls such that each line corresponds to an executive and has the following data: the DNI of the executive and the phone numbers he called in the last week, separated by a space. All these data are strings (str). An example of this kind of file is file data_foundation.txt with the following content:

38999000 91056987 0041555666777 938777666 0041999666777
37888777 91056987 938876666
36787656 91056988 0041555666777 938876545 0041999666555 0041999666777
36999000 91056984 0041555666777 938777666 0041999666777 91454323565
38888777 91056988 0041999666777 938876666 0041999666444 0041999666555 0041666555444
35787656 91056987 938876545 913456543
39999000 91056987 0041666555444 938777666 0041999666777 0041999666444
36546567 91056987 0041555666777 938876666 0041999666444 0041555666777 918786664
36778654 91056987 938876545 937679875

Save all functions in file foundation.py.

  1. Write the function executive() that takes a line of a file of calls (str) with all the data corresponding to an executive, and returns a tuple with the following information: DNI (str), total number of telephone numbers they have called (int) and number of times they have called to Switzerland (int). The telephone numbers corresponding to Switzerland begin with ‘0041’. Examples:

    >>> executive ('38999000 91056987 0041555666777 938777666 0041999666777')
    ('38999000', 4, 2)
    >>> executive ('35787656 91056987 938876545 913456543')
    ('35787656', 3, 0)
    >>> a = '38888777 91056988 0041999666777 938876666 0041999666444 '
    >>> b = '0041999666555 0041666555444'
    >>> executive (a+b)
    ('38888777', 6, 4)
    

    Note

    More tests are provided in file test-executive.txt

  2. Write the function foundation() that takes the name of a file of calls (str) and the name of another file (str), and writes, in this second file, a line for each executive of the first file that has called to Switzerland. Each line has the executive DNI (str) and the number of times he/she has called to Switzerland (str), separated by a space. This function must call the previous one.

    Example: the following statement:

    >>> foundation('data_foundation.txt', 'tswitzerland.txt')
    

    uses the given data file data_foundation.txt, and will create a file with the following content:

    38999000 2
    36787656 3
    36999000 2
    38888777 4
    39999000 3
    36546567 3
    

    Note

    More tests are provided in file test-foundation.txt

Solution

A solution of these functions is provided in file foundation.py