Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

define a Python function def construct_grid(params). The input params will be a dictionary mapping strings to arrays (or array-like data structures such as lists). Consider

define a Python function def construct_grid(params). The input params will be a dictionary mapping strings to arrays (or array-like data structures such as lists). Consider each string a parameter name and each value in the corresponding array to be a candidate value for that parameter.
The function should return a list of dictionaries which satisfies the following:
1. Each dictionary maps each parameter name to exactly one candidate value for that parameter.
2. Exactly one dictionary exists for each combination of parameters.
3. The list is sorted by the values associated with each key. The sort priority for each key should be based on their lexographical order.
For example, construct_grid({'b': [1, 2], 'z': [3, 5, 6], 'a': [100, 10]}) should return as output:
[{'b': 1, 'z': 3, 'a': 10},
{'b': 1, 'z': 5, 'a': 10},
{'b': 1, 'z': 6, 'a': 10},
{'b': 2, 'z': 3, 'a': 10},
{'b': 2, 'z': 5, 'a': 10},
{'b': 2, 'z': 6, 'a': 10},
{'b': 1, 'z': 3, 'a': 100},
{'b': 1, 'z': 5, 'a': 100},
{'b': 1, 'z': 6, 'a': 100},
{'b': 2, 'z': 3, 'a': 100},
{'b': 2, 'z': 5, 'a': 100},
{'b': 2, 'z': 6, 'a': 100}]
Notice the following:
In this example, there are 3 parameters. The length of the lists assigned to the parameters are 2, 3, and 2.
Thus there are 12 (2*3*2)possible combinations.
Each possible combination is represented exactly once in the list.
The list is sorted first by the 'a' value, then by the 'b' value, and finally by the 'z' value.
Keep in mind that the function needs to work for an arbitrary dictionary (the number of keys, the keys themselves, and the values can be anything which meets the which has the structure given earlier in the prompt).
Note:  the functions itertools.product or numpy.meshgrid might be helpful in solving this problem.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Java Programs A Back To Basics Approach

Authors: Stuart Reges, Marty Stepp

5th Edition

013547194X, 978-0135471944

Students also viewed these Algorithms questions