Here is more than enough sample code to get you started, you will have to add the code to generate combinations of 3 or more tuples:

import itertools def getLightestDesiredSet(d, a): '' '(set, list of 2-tuples(float,set)) --> 2-tuple(float,set) d is the desired set, a is the list of available sets and their weights '' ' '' 'Part 1 (referencing arocks' s answer) '' ' s = set() for l in range(0, len(a)): for comb in itertools.combinations(a, l): result_list.append((sum([weight for weight, dset in comb ]), s.union( * [dset for weight, dset in comb ]))) '' 'Part 2' '' sorted_result_list = sorted(result_list, key = lambda my_tuple: my_tuple[0]) '' 'Part 3' '' for a_tuple in sorted_result_list: if (a_tuple[1].issuperset(desired_set)): return a_tuple '' 'if no possible set found: return whatever you want' '' return (0.0, set()) '' 'end function' '' desired_set = set(['item1', 'item2', 'item3']) list_of_tuples = [ (12.0, set(['item1', 'item3'])), (3.5, set(['item3'])), (5.0, set(['item2'])), (7.2, set(['item1'])), (10.0, set(['item2', 'item3'])) ] print(getLightestDesiredSet(desired_set, list_of_tuples))

You can use permutations from itertools library to find all combinations. Here is an attempt:

```
from itertools
import permutations
result = []
for l in range(1, len(list_of_tuples)):
for i in permutations(list_of_tuples, l):
result.append((sum([weight
for weight, dset in i
]),
set.union( * [dset
for weight, dset in i
])))
```

Last Updated : 14 Jun, 2021,GATE CS 2021 Syllabus

2

min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.,Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.,The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).,Normally ZRANGEBYSCORE is simply used in order to get range of items where the score is the indexed integer key, however it is possible to do less obvious things with the command.

`ZRANGEBYSCORE key min max[WITHSCORES][LIMIT offset count]`

By default, the interval specified by `min`

and `max`

is closed (inclusive).
It is possible to specify an open interval (exclusive) by prefixing the score
with the character `(`

.
For example:

ZRANGEBYSCORE zset(1 5

Will return all elements with `1 < score <= 5`

while:

ZRANGEBYSCORE zset(5(10

This means that you set:

```
A to score 0.16
B to score .5
C to score 1
```

At this point, each time you want to get a weighted random element,
just compute a random number between 0 and 1 (which is like calling
`rand()`

in most languages), so you can just do:

RANDOM_ELE = ZRANGEBYSCORE key RAND() + inf LIMIT 0 1

If even, compare the elements and set min to the smaller value and max to the bigger value,Problem Description: Given an array A[] of size n, you need to find the maximum and minimum element present in the array. Your algorithm should make the minimum number of comparisons.,We initialize both minimum and maximum element to the first element and then traverse the array, comparing each element and update minimum and maximum whenever necessary.,We have initialized maximum and minimum with the first element of the array - why?

Pseudo-Code

```
int[] getMinMax(int A[], int n) {
int max = A[0]
int min = A[0]
for (i = 1 to n - 1) {
if (A[i] > max)
max = A[i]
else if (A[i] < min)
min = A[i]
}
// By convention, let ans[0] = maximum and ans[1] = minimum
int ans[2] = {
max,
min
}
return ans
}
```

Pseudo Code

```
int[] findMinMax(int A[], int start, int end) {
int max;
int min;
if (start == end) {
max = A[start]
min = A[start]
} else if (start + 1 == end) {
if (A[start] < A[end]) {
max = A[end]
min = A[start]
} else {
max = A[start]
min = A[end]
}
} else {
int mid = start + (end - start) / 2
int left[] = findMinMax(A, start, mid)
int right[] = findMinMax(A, mid + 1, end)
if (left[0] > right[0])
max = left[0]
else
max = right[0]
if (left[1] < right[1])
min = left[1]
else
min = right[1]
}
// By convention, we assume ans[0] as max and ans[1] as min
int ans[2] = {
max,
min
}
return ans
}
```

For counting the number of comparisons, since this is a recursive function, let us define the recurrence relation :

```
T(n) = 2 T(n / 2) + 2
T(2) = 1
T(1) = 0
We can solve this recurrence relation by master method / recursion tree method.
if n is a power of 2
T(n) = 3n / 2 - 2
```