Created
March 7, 2016 01:32
-
-
Save chrisshroba/178548573b914cc1f8b4 to your computer and use it in GitHub Desktop.
473 Homework 5 #1a Pseudocode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GetKSamples(stream S, int k): | |
samples = list of length k // samples is the list we'll return | |
for i gets 1...k: | |
samples[i] = next item in S // Add the first five items in the | |
// stream to samples | |
l <- k // Count of things seen so far | |
while S is not done | |
x <- next item in S | |
l <- l+1 // Increment count | |
if Random(l) <= k // With 5/l probability... | |
samples[Random(k)] = x // Replace a random item with x | |
return samples |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python code with testing: https://gist.github.com/chrisshroba/2f94fc85b8f14d0a137c