Company vote 2

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

The file votes.txt contains the result of the vote held in a company. In each line there is the name of a shareholder, the number of shares she has, a Boolean that contains True if she is a partner founder of the company, and another boolean that contains True if she approves the reform proposed. The file contains the following lines:

Dani 100 True True
Manel 5 False True
Maria 30 True True
Joan 20 True True
Nuria 150 False False

The first two lines correspond to the partner founder Dani, who has 100 shares and to the partner (not-founder) Manel, with 5 shares. Both have approved the reform.

  1. Write the function person() that takes a string with a line like that previously described and returns a tuple with a string representing the name, an integer representing the number of shares and two Booleans representing if she is a partner founder and if she approves the reform. Examples:

    >>> person('Dani 100 True True') 
    ('Dani', 100, True, True)
    >>> person('Manel 5 False True') 
    ('Manel', 5, False, True)
    

    Note

    More tests are provided in file person.txt

  2. Write the function read() that takes the name of a file with the same format as the file described in the beginning, and returns a list with tuples like those described in the previous paragraph, one for each person of the file. This function has to use the previous one. Example:

    >>> v1 = ['Dani 100 True True', 'Manel 5 False True', 'Maria 30 True True', 'Joan 20 True True', 'Nuria 150 False False']
    >>> with open ('votes.txt', 'w') as f:
    ...    for e in v1:
    ...        a = f.write (e + '\n')
    >>> read('votes.txt')
    [('Dani', 100, True, True), ('Manel', 5, False, True), ('Maria', 30, True, True), ('Joan', 20, True, True), ('Nuria', 150, False, False)]
    

    Note

    More tests are provided in file read.txt

  • The partner founders have the right to veto. That is, if a partner

    founder votes against the reform, it is rejected. Write the function veto() that takes a list of tuples like those described in the previous paragraph and returns True if any partner founder has voted against and False otherwise. Examples:

    >>> veto( [('Dani', 100, True, True), ('Manel', 5, False, True)])
    False
    >>> veto( [('Manel', 5, False, False), ('Dani', 100, True, False)])
    True
    

    Note

    More tests are provided in file veto.txt

  • The vote of each shareholder is weighted with the number of shares

    she has. Thus, the vote of a shareholder has a value of 200 if she has 200 shares. Write the function percentage() that takes a list of tuples like those previously described, and returns the percentage of votes that have accepted the proposal. Examples:

    >>> round(percentage([('Dani', 100, True, True),('Manel', 25, False, False)]), 2)
    80.0
    >>> round(percentage( [('Dani', 100, True, True), ('Manel', 5, False, True), ('Nuria', 95, False, False)]), 2)
    52.5
    

    Note

    More tests are provided in file percentage.txt

  1. Write the function company() that takes the name of a file with the same format as the file described in the beginning, and returns one of these two strings: The reform has been approved or The reform has not been approved. The reform will be approved if nobody with a right to veto has voted against it AND if more than half of the (weighted) votes are favourable. This function has to use the three previous functions. In the given example, the reform would have been approved since no partner founder has issued a veto and there are 155 votes in favour and 150 against.

    Note

    More tests are provided in file company.txt

Solution

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