Phone calls

There are one or more files that contain a sequence of numbers corresponding to the telephone calls of some office. Each line of the file contains a single telephone number, corresponding to a call.

As an example, you can download the file calls.txt that has the following content:

934780245
012
618776611
972399700
11811
972399700
012
607334455
938765454
012
11811
012
11811

Save all functions into the same file named phone.py.

  1. Write a function ncalls1() that takes the name of a file (string) with the given content, and returns how many times the number 012 has received a call and how many the number 11811. Examples:

    >>> ncalls1('calls.txt')
    (4, 3)
    

    Note

    More tests are provided in file ncalls1.txt

  2. Write a function ncalls2() that takes the name of a file (string) with the given content and the name of an output file, and computes how many times the number 012 has received a call and how many the number 11811. The function has to create a new file with the name given as second parameter and with two lines. Each line will contain the telephone number (012 or 11811) and the number of times it has received a call, separated by one whitespace. Examples:

    >>> ncalls2('calls.txt', 'result.txt')
    >>> with open('result.txt', 'r') as f:
    ...     a = f.read()
    >>> a
    '012 4\n11811 3\n'
    

    Note

    More tests are provided in file ncalls2.txt

  3. Write a function consecutive_numbers() that takes the name of a file (string) with the decribed content, and returns True if there are two equal consecutive numbers in the file and False otherwise. Examples:

    >>> consecutive_numbers('calls1.txt')
    False
    

    Note

    More tests are provided in file consecutive_numbers.txt

  4. Write a function prefix() that takes the name of a file (string) with the described content, the name of an output file (string) and another string corresponding to a prefix. The function has to write in the second file those telephone numbers in the first file with the given prefix. Examples:

    >>> prefix('calls.txt', 'result1.txt', '93')
    >>> with open ('result1.txt', 'r') as f:
    ...    a1 = f.read()
    >>> a1
    '934780245\n938765454\n'
    
    >>> prefix('calls.txt', 'result2.txt', '972')
    >>> with open ('result2.txt', 'r') as f:
    ...    a2 = f.read()
    >>> a2
    '972399700\n972399700\n'
    
    
    

    Note

    More tests are provided in file prefix.txt

Solution

A solution of these functions is provided in the phone.py file.