Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python please, don't copy from other places please. The eventual goal of this assignment is to compute the total number of ways of forming

In Python please, don't copy from other places please.

The eventual goal of this assignment is to compute the total number of ways of forming a collection of committees from an academic department of n professors.

The first task is to write a function called committee to compute the number of ways of forming a single committee with r members from a collection of n people. This is the same as computing the number of ways of choosing r people out of n to sit at the first of two tables, and is therefore n! / (r!*(n-r)!) by the reasoning given in the zyLab in section 6.18 on seating a large group at multiple tables. Make sure the function returns an integer, not a floating point number with a decimal point.

In addition to the fact that this must now be a function, there there are now some further enhancements.

First of all, there is the potential that the committee might need a chairperson. If so, there are r choices for the chairperson for each of the possible ways of selecting the committee membership, so there are r*(n! )/ (r!*(n-r)!) = n! / ((r-1)!*(n-r)!) ways of forming such a chaired committee. Another way of thinking how to form such a committee is to choose the chairperson first (there are n possible choices for that), and then for each possible chairperson, choose the remaining r-1 members out of the remaining n-1 people (there are (n-1)! / ((r-1)!*((n-1)-(r-1))!) ways of doing that). So the total number of ways is n*(n-1)!/((r-1)!*((n-1)-(r-1))!). Since n*(n-1)! = n! and (n-1)-(r-1) = n-r, this gives the same final formula n! / ((r-1)!*(n-r)!).

Your function definition must be of a form like

def committee(people, members, chairperson):

where the parameter "people" is an integer giving the number of people to choose from (called "n" in the formulas above), the parameter "members" is an integer giving the number of members in the committee (called "r" in the formulas above) and the parameter "chairperson" is a boolean variable which is True if a chairperson is required, and the formula in the fourth paragraph of this specification must be used, and False if the committee has no chairperson, and the formula in the second paragraph must be used. You must use exactly these names for the parameters, because your function will be be called by keyword arguments using these parameter names in some of the tests, instead of just relying on the parameter order.

Write your function in a file named committee.py, and upload it to zyBooks for the first few tests, which will exercise as it as a module using programs already at zyBooks that import your committee function from it.

(1) The first test is with the following calls:

from committee import committee

committee(10, 3, True)

committee(10, 3, False)

committee(members = 3, people = 10, chairperson = True)

committee(chairperson = False, members = 3, people = 10)

committee(2, 2, True)

committee(3, 3, False)

and the six values returned should be 360, 120, 360, 120, 2, 1 respectively. If they are not all correct, zyBooks will report True for the correct ones, and False for the incorrect ones. For now, you may submit an empty file for the file multiple_committees.py which will be required for later steps.

(2) For the second and third test, modify the "def committee" line so that "chairperson" is an optional variable which takes the value True if it is not supplied. Also, raise two kinds of ValueError exceptions for bad input parameters, setting the reporting string argument inside the parentheses (see figure 10.3.2) to "People count must be positive." if people is less than or equal to zero, and setting it to "Member count must not be greater than people count." if member is greater than people. In addition, the factorial(n) program you prepare for use in committee.py must raise a ValueError if n is less than zero, with the reporting string argument "Cannot take the factorial of a negative number.", but must correctly return 1 for factorial(0). Note that in the case of a negative people count, the ValueError should be raised at the beginning of committee(), so that factorial will not called, and the error message should be "People count must be positive." rather than "Cannot take the factorial of a negative number."

The second test will have the the same calls as in the following program, but instead of printing the output, zyBooks will test that the value returned by each call is correct, and if they are not all correct, will print a list of True or False entries, telling which calls were correct, as for part (1) above. The third test also checks these features, but its parameters and correct output are not revealed here.

from committee import committee

print(committee(10, 3))

print(committee(members = 3, people = 10))

print(committee(2, 2))

The output, if this test program is run with your committee.py module, should be as below:

360

360

2

(3) Now write the main program multiple_committees.py, to compute the number of ways of staffing multiple committees in an academic department. Ask for the total number of professors with the prompt "Enter the number of professors in the department:". (This will be graded without considering white space, so the input can be entered on the same line, or there can be a new line for the input.) Then prompt with the question "Can a professor be on multiple committees? Enter y or n:" If the answer is "y", there is no limit on how many committees a professor can serve on or chair. If the answer is not "y", each professor can serve on at most one committee. After these initial two questions, the program should loop asking the following three questions about each committee: "Enter the name of the committee:", "Does the committee need a chairperson? Enter y or n:", and "Enter the number of members:". The loop should terminate if either the empty string "" is entered for the name of the committee, or if each professor can serve on at most one committee, and there are no unassigned professors left after a committee is formed. If there are not enough professors left to form a committee with the specified number of members, assign all the available professors to that committee instead, and print "Assigning only [professors left] members to the [name] committee.", where [professors left] is the number of available professors, and [name] is the name of the committee. Then call your committee program, which you should import from the module committee.py that you have prepared to compute the number of ways of assigning that committee, and print the answer in a statement of the form "There are [ways] ways to form the [name] "committee.", where [ways] is the number of ways computed. If the professors are not allowed to serve on multiple committees, you will need to subtract the number just assigned from the number of available ones to get the number available for future assignments to committees. When the loop over the committees terminates, print the total number of different ways of forming all the committees in a statement "There are [total ways] ways to form all the committees.", where [total ways] is the number of different possible ways of forming all the multiple committees. You can compute this number by multiplying the number of ways of forming the first committee times the number of ways of forming the seconds committee times the number of ways of forming the third committee, and so forth. A sample session might look like this:

Enter the number of professors in the department:

12

Can a professor be on multiple committees? Enter y or n:

y

Enter the name of the committee:

Admissions

Does the committee need a chairperson? Enter y or n:

y

Enter the number of members:

4

There are 1980 ways to form the Admissions committee.

Enter the name of the committee:

Petitions

Does the committee need a chairperson? Enter y or n:

n

Enter the number of members:

2

There are 66 ways to form the Petitions committee.

Enter the name of the committee:

Student Welfare

Does the committee need a chairperson? Enter y or n:

n

Enter the number of members:

4

There are 495 ways to form the Student Welfare committee.

Enter the name of the committee:

There are 64686600 ways to form all the committees.

(4) Modify the loop over the multiple committees in your main multiple_committees.py program to put the call to committee() in a try block, and follow it with an except block to check for a ValueError and print the string you prepared to identify which one it is of the three types of value errors you raised. Be careful to put any further use of the data for this committee (for example, updating the count of remaining available professors, updating the total count of all multiple committee assignments, or printing the number of ways of forming the current committee) inside the try block, so that if there is an error from committee( ) or factorial(), the user can try again with correct data for that committee and still get a correct output later. A sample session might look like:

Enter the number of professors in the department:

-2

Can a professor be on multiple committees? Enter y or n:

n

Enter the name of the committee:

Compensation

Does the committee need a chairperson? Enter y or n:

n

Enter the number of members:

-2

People count must be positive.

Enter the name of the committee:

There are 1 ways to form all the committees.

Note the the user gave up and entered an empty line after seeing the error, because there is no way of recovering from the error of a negative number of professors. However, in the following case there is a way to recover:

Enter the number of professors in the department:

5

Can a professor be on multiple committees? Enter y or n:

n

Enter the name of the committee:

Compensation

Does the committee need a chairperson? Enter y or n:

y

Enter the number of members:

-3

Cannot take the factorial of a negative number.

Enter the name of the committee:

Compensation

Does the committee need a chairperson? Enter y or n:

y

Enter the number of members:

3

There are 30 ways to form the Compensation committee.

Enter the name of the committee:

Graduate Admissions

Does the committee need a chairperson? Enter y or n:

y

Enter the number of members:

3

Assigning only 2 members to the Graduate Admissions committee.

There are 2 ways to form the Graduate Admissions committee.

There are 60 ways to form all the committees.

In fact, the above two examples are the ones used in the last two tests of your program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions