Question: Implement the routine numDerangements in python which takes in the number of people in the derangement problem, and returns the number of ways their coats

Implement the routine numDerangements in python which takes in the number of people in the derangement problem, and returns the number of ways their coats can be returned such that no one receives the right coat.
please use the following format:
from math import factorial
# takes in n >=1
# returns the number of possible derangements of n items
# Examples:
# | Input | Output |
# |---------|----------|
# |1|0|
# |---------|----------|
# |2|1|
# |---------|----------|
# |3|2|
# |---------|----------|
# |4|9|
# |---------|----------|
# |5|44|
# |---------|----------|
def numDerangements( n ):
return 0
# Testing code provided in main():
def main():
testArgs =[[1,0],[2,1],[3,2],[4,9],[5,44]]
for arg in testArgs:
nArg, answer = arg
result = numDerangements(nArg)
if result != answer:
print(f"Failed numDerangements test with arg n={nArg}.
Expected: {answer}, Got: {result}")
else:
print(f"Passed numDerangements test with arg n={nArg}.")
return 0
if __name__=='__main__':
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!