For PYTHON programming. There is a program that starts with a function definitio
ID: 3834987 • Letter: F
Question
For PYTHON programming. There is a program that starts with a function definition for factorial(n), which returns the number n! Then the main program should call that function three times to compute the number n! / ( r! * s!) of different table assignments.
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x-1)
n = int(input ("Enter the number of people "))
r = int(input ("Enter the number of chairs at table 1 "))
s = int(input ("Enter the number of chairs at table 2 "))
table_assignments = factorial(n) / (factorial(r) * factorial(n-r))
print ("The number of table assignments is" , int(table_assignments) )
The output looks like as follows
Modify this program such that:
*enhance the program for more than two tables. Keep prompting for the number of chairs at tables until the number equals n. If the numbers of chairs at the tables are r, s, t, ...., x, the reasoning above shows that the number of different assignments is n!/ (r! * s! * t! * ... * x!). So an example interaction would be:
*enhance the program so that if the number of chairs r + s + t + .... + x after the latest user input exceeds n, the program will print "Seating only c people at table d" where the integer d is the last table index, and the integer c = n - ( r + s + t + .... + x) is the number of people actually needing seats at the last table, and then proceed as before, using c instead of the user-entered number x in the computation. So an example interaction would be:
Enter the number of people Enter the number of chairs at table 1 Enter the number of chairs at table 2 The number of table assignments is 20Explanation / Answer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x-1)
n = int(input ("Enter the number of people "))
i = 1
mult = 1
total = 0
while total < n:
r = int(input ("Enter the number of chairs at table " + str(i) + " "))
if total + r > n:
r = n - total
print("Seating only " + str(r) + " people at table " + str(i))
total += r
i += 1
mult *= factorial(r)
table_assignments = factorial(n) / mult
print ("The number of table assignments is" , int(table_assignments) )
# code link: https://paste.ee/p/L3bGa
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.