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']) >>> list(_) [('A', 'D'), ('A', 'E'), ('B', 'D'), ('B', 'E'), ('C', 'D'), ('C', 'E')]

>>> product([1,2,3,4,5],[True,False]) >>> list(_) [(1, True), (1, False), (2, True), (2, False), (3, True), (3, False), (4, True), (4, False), (5, True), (5, 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

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!