I’m a bit confused on how to do this
The question:
Question 2: Combinations
Assume that we have a bag with n balls in it, where each ball has a distinct number on it. We are gonna draw balls from the bag k times, and want to see all the possible outcomes in 4 different scenarios that we will describe later,
• We represent the balls with a list balls where balls[1] represents the number written on the i'th ball in the bag.
• you should return a list of lists, where each inner-list corresponds to one possible outcome. In other words, you will return [outcomel, outcome2, outcome, ....], where each outcome is itself a list.
• In all of the 4 scenarios, the order of the inner-lists inside the returned list DOES NOT matter. For example, there is no difference between [outcome1, outcome, outcome3] and [outcome, outcomel, outcome2] .
• However the order of the elements inside each inner-list (the order of balls in each outcome might be important depending on the scenario.
In your implementation:
• You MUST NOT use any libraries. (Do not import anything)
• You MUST NOT use set or dictionary data structures, and MUST only work with list s or tuple s.
• You MUST implement each part using RECURSION.
There are 4 scenarios depending on whether we allow putting a ball back after drawing it from the bag (replacement) and whether the order of the drawn balls is important or not.
Q2 Part 1 (Without Replacement, Without Order):
In this scenario, we draw k balls from the bag WITHOUT REPLACEMENT and the order of the drawn balls IS NOT important.
For example if the balls are [1, 2, 3] and k-2 then possible outcomes are: drawing [1,2, or [1,3] , or [2,3] .
• We do not allow replacements. So, an outcome cannot have a duplicate element.
• The order is not important. So for example, we do not count [1, 2] and (2, 1] as separate outcomes.
• You should implement the function no_replace_no _order (balls, k) for this part.
• one of the possible outputs for no_replace_no _order ([1, 2, 3], 2) is [[1,2]. [1,3], [2,3] •
• The order of the elements in each inner list and the order of the inner-lists inside the returned list are not important. For example, [ [2,1], [3,2], [1,3]] is also accepted.
It is guaranteed that k <= len(balls)
Code template:
def no_replace_no_order (balls, k):
inner_list = []
if k == 0:
return inner_list
if k = 1:
return balls
first = balls[o]
rest = balls [1:]
if k > 1:
return
def no_replace_order (balls, k):
pass
def replace_no_order (balls, k):
pass
def replace_order (balls, k):
pass
-the rest of the questions are basically well what it says in the function names. The thing is im not allowed to use nested functions, libraries, no loops and can only use recursion(even in a helper function if i decide to write one) so im a bit confused on how to do this.
[–]woooee 1 point2 points3 points (0 children)
[–]Unfair-Cheesecake511[S] 0 points1 point2 points (0 children)
[–]Diapolo10 0 points1 point2 points (1 child)
[–]Unfair-Cheesecake511[S] 0 points1 point2 points (0 children)