GeezerVison is showing github.com — Added random sample algorithm. · code3002/coding-problems@0c6ea28
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Random Sample | | 3 | +
| | 4 | +Given an array and length of the sample, find a random sample from that array. | | 5 | +
| | 6 | +Input: [1, 2, 3, 4], 2 | | 7 | +Output: This is a nondeterministic algorithm, C(N, K) combinations exist. | | 8 | + In this case 4! / (2! * (4 - 2)!) = 6. All combinations are a valid solution. | | 9 | + [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4] | | 10 | +
| | 11 | +========================================= | | 12 | +Simple solution in one pass, Reservoir sampling. Maybe the solution looks like the elements don't have an equal probability | | 13 | +to be chosen, but there is a proof that they have equal probability https://en.wikipedia.org/wiki/Reservoir_sampling | | 14 | + Time Complexity: O(N) | | 15 | + Space Complexity: O(K) | | 16 | +Another simple solution in one pass, the probability for each element to be choosen in thee. | | 17 | + Time Complexity: O(N) | | 18 | + Space Complexity: O(K) | | 19 | +''' | | 20 | + | | 21 | + | | 22 | +############## | | 23 | +# Solution 1 # | | 24 | +############## | | 25 | + | | 26 | +from random import randint | | 27 | + | | 28 | +def reservoir_sampling(arr, k): | | 29 | + # fill the reservoir array | | 30 | + sample = [] | | 31 | + for i in range(k): | | 32 | + sample.append(arr[i]) | | 33 | + | | 34 | + # replace elements with gradually decreasing probability | | 35 | + n = len(arr) | | 36 | + for i in range(k, n): | | 37 | + # randint(a, b) generates a uniform integer from the inclusive range {a, ..., b} (a <= X <= b) | | 38 | + j = randint(0, i) | | 39 | + if j < k: | | 40 | + sample[j] = arr[i] | | 41 | + | | 42 | + return sample | | 43 | + | | 44 | + | | 45 | +############## | | 46 | +# Solution 2 # | | 47 | +############## | | 48 | + | | 49 | +from random import random | | 50 | + | | 51 | +def probabilistic_sampling(arr, k): | | 52 | + sample = [] | | 53 | + n = len(arr) | | 54 | + | | 55 | + for el in arr: | | 56 | + # random() generates a uniform double in this range (0 <= X < 1) | | 57 | + # (k / n) is the probability for this element to be choosen (0 <= X <= 1) | | 58 | + if random() < (k / n): | | 59 | + sample.append(el) | | 60 | + k -= 1 # left elements to be choosen | | 61 | + n -= 1 # left elements for choosing | | 62 | + | | 63 | + return sample | | 64 | + | | 65 | + | | 66 | +########### | | 67 | +# Testing # | | 68 | +########### | | 69 | + | | 70 | +# Test 1 | | 71 | +# Correct result => One of these: [1, 2], [1, 3], [1, 4], [2, 3], [2, 4] | | 72 | +arr = [1, 2, 3, 4] | | 73 | +k = 2 | | 74 | +print(reservoir_sampling(arr, k)) | | 75 | +print(probabilistic_sampling(arr, k)) |
Collapse file Copy file name to clipboardExpand all lines: Arrays/shuffle_array.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | Here shuffle means that every permutation of array element should equally likely. | 8 | 8 |
| 9 | 9 | Input: [1, 2, 3] | 10 | | -Output: This is a nondeterministic algorithm, N! solutions/permutations exists. | | 10 | +Output: This is a nondeterministic algorithm, N! solutions/permutations exist. | 11 | 11 | In this case 3! = 6. All permutations are a valid solution. | 12 | 12 | [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] | 13 | 13 |
| 14 | 14 | ========================================= | 15 | 15 | Easy in-place solution, choose an random index/position from I (I is the current position) to N-1 | 16 | 16 | (or choose from 0 to I, it's same), swap the element at random position with the element at I position. | 17 | 17 | Increase I and continue with the same algorithm. | | 18 | +This algorithm is called Fisher–Yates shuffle (https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). | 18 | 19 | Note: In Python there is already implemented shuffle (in random module "from random import shuffle") method | 19 | | - which works in similar way. But it's easy to be implemented and I wanted to show how to implement it. | | 20 | +which works in similar way. But it's easy to be implemented and I wanted to show how to implement it. | 20 | 21 | Time Complexity: O(N) | 21 | 22 | Space Complexity: O(1) | 22 | 23 | ''' | |
|