Question: You will write two functions partitions(n,k) that counts in how many ways n distinct elements can be grouped into k (non empty) partitions, and mkCh(a,c)
You will write two functions partitions(n,k) that counts in how many ways n distinct elements can be grouped into k (non empty) partitions, and mkCh(a,c) that counts in how many ways amount a can be paid with coins {1,5,10,25}. Both algorithms are discussed in lecture 15: counting.
counting.py contains some skeleton code.
python3 counting.py 3 2
produces
n: 3 k: 2 partitions: 3 amount: 32 coins: [1, 5, 10, 25] ways: 18
CODE:
import sys
coins = [1,5,10,25]
def partitions(n,k): """ pre 0
if __name__ == "__main__": # partititions d = len(sys.argv)>3 n = int(sys.argv[1]) k = int(sys.argv[2]) p = partitions(n,k) print("n:",n,"k:",k, "partitions:",p) # make change c = len(coins)-1 a = 10*n+k ways = mkCh(a,c) print("amount:", a, "coins:", coins, "ways:", ways)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
