In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of December 14th
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #09 exercise sheet]
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
(in this class you are expected to write a solution that uses recursion - mooshak will not force it, but that should be your goal)
Hermes Conrad, the meticulous bureaucrat, is planning his next k weekends. He has a list of planets he's considering visiting, but as a stickler for thoroughness, he wants to explore all possible ways to spend those weekends. Since Hermes is open to visiting the same planet multiple times, he’s specifically interested in generating all possible combinations with repetition of k items from the list of planets.
Hermes doesn't have time to do this himself (his forms won't stamp themselves!), so heis delegated the task to you.
Write a function weekends(k, planets) that given an integer k and a list of strings planets, returns a list containing all possible combinations of k planets. A combination may repeat the same planet and it should be represented by a list of strings. The overall return value should therefore be a list of lists.
The following limits are guaranteed in all the test cases that will be given to your program:
1 ≤ k ≤ 8 | number of weekends | |
1 ≤ |planets| ≤ 8 | number of planets |
Example Function Calls | Example Output |
output = weekends(2, ["earth", "neptune"]) print(sorted(output)) print() output = weekends(3, ["earth", "mars", "saturn"]) print("output is of type", type(output)) print("output has size:", len(output)) for x in sorted(output): print(x) |
[['earth', 'earth'], ['earth', 'neptune'], ['neptune', 'earth'], ['neptune', 'neptune']] output is of type <class 'list'> output has size: 27 ['earth', 'earth', 'earth'] ['earth', 'earth', 'mars'] ['earth', 'earth', 'saturn'] ['earth', 'mars', 'earth'] ['earth', 'mars', 'mars'] ['earth', 'mars', 'saturn'] ['earth', 'saturn', 'earth'] ['earth', 'saturn', 'mars'] ['earth', 'saturn', 'saturn'] ['mars', 'earth', 'earth'] ['mars', 'earth', 'mars'] ['mars', 'earth', 'saturn'] ['mars', 'mars', 'earth'] ['mars', 'mars', 'mars'] ['mars', 'mars', 'saturn'] ['mars', 'saturn', 'earth'] ['mars', 'saturn', 'mars'] ['mars', 'saturn', 'saturn'] ['saturn', 'earth', 'earth'] ['saturn', 'earth', 'mars'] ['saturn', 'earth', 'saturn'] ['saturn', 'mars', 'earth'] ['saturn', 'mars', 'mars'] ['saturn', 'mars', 'saturn'] ['saturn', 'saturn', 'earth'] ['saturn', 'saturn', 'mars'] ['saturn', 'saturn', 'saturn'] |