Question: Using Python, The product functions from the itertools library allows us to take cartesian products of a set. For example: >>> from itertools import product
Using Python,
The product functions from the itertools library allows us to take cartesian products of a set. For example:
>>> from itertools import product
>>> product(['A','B','C'], ['D','E'])
>>> product([1,2,3,4,5],[True,False])
We can also have it take products of any length for a set with itself.
>>> list(product(['A','B','C'], repeat=2)) [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
>>> list(product([0,1], repeat=3)) [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
1. Use product() to make a function which counts the number of ways you can place unlabeled balls in labeled urns given lower and upper bounds on how many balls each urn can have. Your function should take in four integers
n - number of balls
k - number of urns
lower - the smallest number of balls allowed in an urn
upper - the largest number of balls allowed in an urn
It should spit out a list of all the possible configurations.
bounded_urns(n,k,lower,upper)
>>> bounded_urns(13,4,2,4) [(2, 3, 4, 4), (2, 4, 3, 4), (2, 4, 4, 3), (3, 2, 4, 4), (3, 3, 3, 4), (3, 3, 4, 3), (3, 4, 2, 4), (3, 4, 3, 3), (3, 4, 4, 2), (4, 2, 3, 4), (4, 2, 4, 3), (4, 3, 2, 4), (4, 3, 3, 3), (4, 3, 4, 2), (4, 4, 2, 3), (4, 4, 3, 2)]
>>> bounded_urns(5,7,0,1) [(0, 0, 1, 1, 1, 1, 1), (0, 1, 0, 1, 1, 1, 1), (0, 1, 1, 0, 1, 1, 1), (0, 1, 1, 1, 0, 1, 1), (0, 1, 1, 1, 1, 0, 1), (0, 1, 1, 1, 1, 1, 0), (1, 0, 0, 1, 1, 1, 1), (1, 0, 1, 0, 1, 1, 1), (1, 0, 1, 1, 0, 1, 1), (1, 0, 1, 1, 1, 0, 1), (1, 0, 1, 1, 1, 1, 0), (1, 1, 0, 0, 1, 1, 1), (1, 1, 0, 1, 0, 1, 1), (1, 1, 0, 1, 1, 0, 1), (1, 1, 0, 1, 1, 1, 0), (1, 1, 1, 0, 0, 1, 1), (1, 1, 1, 0, 1, 0, 1), (1, 1, 1, 0, 1, 1, 0), (1, 1, 1, 1, 0, 0, 1), (1, 1, 1, 1, 0, 1, 0), (1, 1, 1, 1, 1, 0, 0)]
>>> len(bounded_urns(20,6,0,4))
126
2. Use recursion to create a function recursive_urns() which does the same thing. It can return either a list of tuples, or a list of lists for the answer. Pick whichever is easier for you.
>>> recursive_urns(13,4,2,4) [[4, 4, 3, 2], [4, 3, 4, 2], [3, 4, 4, 2], [4, 4, 2, 3], [4, 3, 3, 3], [3, 4, 3, 3], [4, 2, 4, 3], [3, 3, 4, 3], [2, 4, 4, 3], [4, 3, 2, 4], [3, 4, 2, 4], [4, 2, 3, 4], [3, 3, 3, 4], [2, 4, 3, 4], [3, 2, 4, 4], [2, 3, 4, 4]]
>>> recursive_urns(5,7,0,1) [[1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 0, 1, 0], [1, 1, 1, 0, 1, 1, 0], [1, 1, 0, 1, 1, 1, 0], [1, 0, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 0, 1], [1, 1, 1, 0, 1, 0, 1], [1, 1, 0, 1, 1, 0, 1], [1, 0, 1, 1, 1, 0, 1], [0, 1, 1, 1, 1, 0, 1], [1, 1, 1, 0, 0, 1, 1], [1, 1, 0, 1, 0, 1, 1], [1, 0, 1, 1, 0, 1, 1], [0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 0, 1, 1, 1], [1, 0, 1, 0, 1, 1, 1], [0, 1, 1, 0, 1, 1, 1], [1, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1]]
>>> recursive_urns(30,4,2,5) []
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
