GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added random sample algorithm. · danasmile/coding-problems@0c6ea28

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Arrays
    • random_sample.py
    • shuffle_array.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Arrays/random_sample.py‎

Copy file name to clipboard
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

‎Arrays/shuffle_array.py‎

Copy file name to clipboardExpand all lines: Arrays/shuffle_array.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
Here shuffle means that every permutation of array element should equally likely.
88
99
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.
1111
In this case 3! = 6. All permutations are a valid solution.
1212
[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
1313
1414
=========================================
1515
Easy in-place solution, choose an random index/position from I (I is the current position) to N-1
1616
(or choose from 0 to I, it's same), swap the element at random position with the element at I position.
1717
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).
1819
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.
2021
Time Complexity: O(N)
2122
Space Complexity: O(1)
2223
'''

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.