Code for random sampling without replacement with a reservoir. It randomly choose a sample of k items from a file containing n items. Typically n is large enough that it doesn't fit into the main memory. This algorithm is also suitable for streaming data
The python and java code are independent from each other and any of them can be used separately. The java code uses a randomized queue
For the python code: python 3.4 or grater
For the java code: java 8 or grater and the library algs4.jar
Python code:
Ready to work using your favorite IDE. If you are in the folder containing the code, you can run it using:
python reservoirSampling.py k file_name
where k is the size of the sample and file_name is the file where you can extract the sample. You must put your file inside the directory called TestData. If no arguments are provided, default arguments takes place (k = 50 file_name = Moby-Dick.txt). You can also hardcode your file name in the code. To avoid incompatibilities between OS the pathlib was used to set the path to the files.
Java code:
You can build the project using your favorite IDE. You must add the algs4.jar library to your classpath.
To run the code you must provide the size of the sample and the file names as arguments.
Licensed under the MIT License - see the LICENSE.md file for details
- This short project was inspire from the Princeton course Algorithms I