Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree Traversal: Output Standardization #857

Merged
merged 92 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
22926b1
pep8ify python
lazyprop Sep 3, 2021
933d66c
rename Tree_example.py
lazyprop Sep 3, 2021
57d1173
standardize python output
lazyprop Sep 3, 2021
12c02c6
standardize c output
lazyprop Sep 3, 2021
920e333
standardize c++ output
lazyprop Sep 3, 2021
b3ad30c
standardize rust output
lazyprop Sep 3, 2021
ba5824b
standardize javascript output
lazyprop Sep 3, 2021
6726ed3
standardize julia output
lazyprop Sep 3, 2021
9cb22f3
Standardized Coconut output
Amaras Sep 3, 2021
0dde208
fix coconut
lazyprop Sep 3, 2021
bdd40d2
standardize go output
lazyprop Sep 3, 2021
02c6537
standardize common lisp output
lazyprop Sep 3, 2021
b8d4679
Verlet Integration: Output standardization (#855)
stormofice Sep 4, 2021
62d8795
standardize php output
lazyprop Sep 4, 2021
5396b94
standardize swift output
lazyprop Sep 4, 2021
6726137
removed outdated comments in Coconut implementation
Amaras Sep 4, 2021
78246c3
standardize haskell output
jiegillet Sep 6, 2021
84e60b2
Fix asm exit codes (#858)
stormofice Sep 6, 2021
387e437
standardize crystal output
lazyprop Sep 6, 2021
09168eb
Flood fill in Coconut (#836)
Amaras Sep 6, 2021
bd58862
Added Barnsley fern in Coconut (+ .editorconfig for Coconut) (#814)
Amaras Sep 6, 2021
cc457b9
Added the Jarvis march in Coconut (#734)
Amaras Sep 6, 2021
9440cef
Fix minor typo
berquist Sep 7, 2021
bb87921
Change folder structure of convolutions (#861)
stormofice Sep 8, 2021
f395357
standardize csharp output
lazyprop Sep 8, 2021
523e950
standardize java output
lazyprop Sep 8, 2021
32fb947
fix php line numbers and python filename
lazyprop Sep 8, 2021
bb04cf7
fix coconut and python line numbers
lazyprop Sep 8, 2021
0a39290
fix minor bug in C
lazyprop Sep 8, 2021
8fd9364
fix labels and minor bug in go
lazyprop Sep 8, 2021
d1b2ddc
minor label fix in python
lazyprop Sep 8, 2021
122a5a8
Huffman encoding in Coconut (#732)
Amaras Sep 9, 2021
fd90b19
Merge pull request #859 from berquist/barnsley-typo
jiegillet Sep 10, 2021
435ef99
Update README.md
leios Sep 14, 2021
b86d137
Add C# implemenation for 1D Convolutions (#846)
stormofice Sep 14, 2021
f04abdc
IFS Common Lisp implementation (#722)
Trashtalk217 Sep 15, 2021
7bae159
added MonteCarlo in PowerShell (#760)
PaddyKe Sep 15, 2021
4557186
Add Rust implementation for The Barnsley Fern (#818)
ShadowMitia Sep 15, 2021
04be809
changing barnsley.md to read the correct coconut file (#867)
leios Oct 6, 2021
c8a40e3
added approximate counting implementation in python (#866)
pillarxyz Oct 6, 2021
4bb16db
fixing linking issues in convolution chapter (#868)
pillarxyz Oct 8, 2021
448a796
Add initial devcontainer (#862)
ntindle Oct 8, 2021
7c19ec6
small changes to convolutional theorem chapter and code to make thing…
leios Oct 10, 2021
46bf5a3
added convolutional theorem implementation in python (#869)
pillarxyz Oct 10, 2021
39f8c1c
Use `[#]\n` instead of `[#]` for human readable output (#873)
stormofice Oct 10, 2021
f3bd8f4
Add approximate counting algorithm in C (#844)
mahdisarikhani Oct 11, 2021
d81942c
added 1d convolution implementation in python (#874)
pillarxyz Oct 12, 2021
bfc180d
fixing 1d convolution markdown file (#879)
pillarxyz Oct 12, 2021
1c82049
Add racket setup to devcontainer (#875)
ShadowMitia Oct 15, 2021
c206831
Normalize inputs for scheme euclid algorithm (#878)
ShadowMitia Oct 16, 2021
a869114
Add initial setup for Swift in devcontainer (#880)
ShadowMitia Oct 16, 2021
cee1270
Add scheme setup to devcontainer (#876)
ShadowMitia Oct 16, 2021
d631070
Clean up Monte Carlo integration in Racket (#781)
berquist Oct 18, 2021
7f814fa
Add C++ code for Flood Fill algorithm (#860)
ShadowMitia Oct 18, 2021
65599ee
fixing chapter to use split-op code (#888)
leios Oct 20, 2021
c7c05a5
use [#]\n for readability
lazyprop Oct 21, 2021
9eb11d5
Implemented stacks and queues in Java (#897)
PaddyKe Oct 23, 2021
84e9d5d
Java tree traversal: updated dfsRecursiveInOrderBinary (#899)
henrikac Oct 23, 2021
0652a18
Tree traversal in smalltalk (#453)
Neverik Oct 23, 2021
f7e8e60
fixed print statements (#901)
PaddyKe Oct 23, 2021
0fa6381
Fixes in PATH that make dlang, emojicode and factor usable (#890)
ShadowMitia Oct 23, 2021
44d8118
fixing huffman encoding for Julia and adding Test (#828)
leios Oct 23, 2021
cf56e67
JavaScript tree traversal: updated dfsInorder (#902)
henrikac Oct 23, 2021
3358b37
julia: change printf to print
lazyprop Oct 23, 2021
2cb7882
pep8ify python
lazyprop Sep 3, 2021
69cd001
rename Tree_example.py
lazyprop Sep 3, 2021
e4c8ff1
standardize python output
lazyprop Sep 3, 2021
79d8b68
standardize c output
lazyprop Sep 3, 2021
2d5f36e
standardize c++ output
lazyprop Sep 3, 2021
4989ad8
standardize rust output
lazyprop Sep 3, 2021
20cda99
standardize javascript output
lazyprop Sep 3, 2021
236685d
standardize julia output
lazyprop Sep 3, 2021
6251005
Standardized Coconut output
Amaras Sep 3, 2021
bf6c74b
fix coconut
lazyprop Sep 3, 2021
14f9275
standardize go output
lazyprop Sep 3, 2021
1103d8c
standardize common lisp output
lazyprop Sep 3, 2021
ed09f86
standardize php output
lazyprop Sep 4, 2021
e792364
standardize swift output
lazyprop Sep 4, 2021
44fe701
removed outdated comments in Coconut implementation
Amaras Sep 4, 2021
b28b285
standardize haskell output
jiegillet Sep 6, 2021
64a8129
standardize crystal output
lazyprop Sep 6, 2021
1234b83
standardize csharp output
lazyprop Sep 8, 2021
552e45d
standardize java output
lazyprop Sep 8, 2021
8c75161
fix php line numbers and python filename
lazyprop Sep 8, 2021
2fa6a38
fix coconut and python line numbers
lazyprop Sep 8, 2021
b9ebcd2
fix minor bug in C
lazyprop Sep 8, 2021
dd95d0d
fix labels and minor bug in go
lazyprop Sep 8, 2021
f71124a
minor label fix in python
lazyprop Sep 8, 2021
1b83e14
use [#]\n for readability
lazyprop Oct 21, 2021
7a575b5
julia: change printf to print
lazyprop Oct 23, 2021
b0558dc
Merge branch 'tree-traversal-standardize' of github.com:lazyprop/algo…
Amaras Oct 23, 2021
ad99439
update Tree.java. remove MainClass.java
lazyprop Oct 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/ubuntu/.devcontainer/base.Dockerfile

# [Choice] Ubuntu version: bionic, focal
ARG VARIANT="focal"
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk rustc libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch

# Setup Crystal
RUN echo 'deb http://download.opensuse.org/repositories/devel:/languages:/crystal/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/devel:languages:crystal.list
RUN curl -fsSL https://download.opensuse.org/repositories/devel:languages:crystal/xUbuntu_20.04/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/devel_languages_crystal.gpg > /dev/null

# Setup Dart
RUN sudo sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
RUN sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'

# Setup Powershell
RUN sudo sh -c 'wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb'
RUN sudo sh -c 'dpkg -i packages-microsoft-prod.deb'

# Setup Clojure
RUN sudo sh -c 'curl -O https://download.clojure.org/install/linux-install-1.10.3.967.sh'
RUN sudo sh -c 'chmod +x linux-install-1.10.3.967.sh'
RUN sudo sh -c 'sudo ./linux-install-1.10.3.967.sh'

# Setup dotnet
RUN sudo sh -c 'wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb'
RUN sudo sh -c 'sudo dpkg -i packages-microsoft-prod.deb'
RUN sudo sh -c 'rm packages-microsoft-prod.deb'

# Setup D Lang
ENV DLANG_VERSION=2.097.2
RUN mkdir -p ~/dlang && wget https://dlang.org/install.sh -O ~/dlang/install.sh
RUN bash ~/dlang/install.sh dmd-$DLANG_VERSION
ENV PATH=$PATH:~/dlang/dmd-$DLANG_VERSION/linux/bin64/

# Setup Go
RUN sudo sh -c 'wget -c https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local'
ENV PATH=$PATH:/usr/local/go/bin

# Setup Kotlin (doesnt unzip right maybe?)
RUN sudo sh -c 'wget -c https://github.com/JetBrains/kotlin/releases/download/v1.5.30/kotlin-compiler-1.5.30.zip -O /usr/local/kotlinc.zip'
RUN unzip /usr/local/kotlinc.zip
ENV PATH=$PATH:/usr/local/kotlinc/bin

# Setup lolcode
## Use: https://github.com/justinmeza/lci

# Setup Piet
## Use: https://github.com/boothby/repiet


# Setup Matlab
# ?????? This is a licensed language???

# Setup Emojicode
RUN mkdir -p ~/emojicode && wget -c https://github.com/emojicode/emojicode/releases/download/v1.0-beta.2/Emojicode-1.0-beta.2-Linux-x86_64.tar.gz -O ~/emojicode/emojicode.tar.gz && \
tar -xzf ~/emojicode/emojicode.tar.gz -C ~/emojicode --strip-components=1
ENV PATH=$PATH:~/emojicode

# Setup Factor
RUN mkdir -p ~/factor && wget https://downloads.factorcode.org/releases/0.98/factor-linux-x86-64-0.98.tar.gz -O ~/factor/factor.tar.gz && tar -xzf ~/factor/factor.tar.gz -C ~/factor --strip-components=1
ENV PATH=$PATH:~/factor/factor

# Setup R
RUN sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
RUN sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

# Setup Racket and Scheme
# To run scheme files, use `racket -f <file.ss>`
RUN sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D9D33FCD84D82C17288BA03B3C9A6980F827E01E
RUN sudo add-apt-repository 'deb http://ppa.launchpad.net/plt/racket/ubuntu focal main'

# Setup Scratch
## using 1.x right now.... in future checkout snap or adobe air?

# Setup Swift
RUN mkdir -p ~/swift && wget https://swift.org/builds/swift-5.5-release/ubuntu2004/swift-5.5-RELEASE/swift-5.5-RELEASE-ubuntu20.04.tar.gz -O ~/swift/swift.tar.gz && \
tar -xzf ~/swift/swift.tar.gz -C ~/swift --strip-components=1
ENV PATH=$PATH:~/swift/usr/bin

# Setup viml
## ?

# Setup whitespace
## ?

# Setup Elm
## https://github.com/elm/compiler/blob/master/installers/linux/README.md

# Setup V
## https://github.com/vlang/v/blob/master/doc/docs.md


# Install the packages that needed extra help
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends crystal dart nim powershell scala dotnet-sdk-5.0 r-base racket

RUN pip install wheel matplotlib numpy coconut

RUN sudo sh -c 'npm install -g typescript'
26 changes: 26 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/ubuntu
{
"name": "Ubuntu",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick an Ubuntu version: focal, bionic
"args": { "VARIANT": "focal" }
},

// Set *default* container specific settings.json values on container create.
"settings": {},


// Add the IDs of extensions you want installed when the container is created.
"extensions": [],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a",

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,6 @@ indent_size = 4
indent_style = space
indent_size = 2

[*.coco]
indent_style = space
indent_size = 4
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Mahdi Sarikhani
- Ridham177
- Hugo Salou
- Dimitri Belopopsky
+ Henrik Abel Christensen
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Here are some essential links:
- Book / website: <https://www.algorithm-archive.org/>
- GitHub repository: <https://github.com/algorithm-archivists/algorithm-archive>
- YouTube channel (LeiosOS): <https://www.youtube.com/user/LeiosOS>
- Twitch livestream (simuleios): <https://www.twitch.tv/simuleios>
- Twitch livestream: <https://www.twitch.tv/leioslabs>
- Discord server: <https://discord.gg/Pr2E9S6>

Note that this project is essentially a book about algorithms collaboratively written by an online community.
Expand Down
4 changes: 4 additions & 0 deletions contents/IFS/IFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ Here, instead of tracking children of children, we track a single individual tha
[import:5-12, lang:"python"](code/python/IFS.py)
{% sample lang="c" %}
[import:18-29, lang:"c"](code/c/IFS.c)
{% sample lang="lisp" %}
[import:5-14, lang:"lisp"](code/clisp/ifs.lisp)
{% sample lang="coco" %}
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
{% sample lang="java" %}
Expand Down Expand Up @@ -224,6 +226,8 @@ In addition, we have written the chaos game code to take in a set of points so t
[import, lang:"python"](code/python/IFS.py)
{% sample lang="c" %}
[import, lang:"c"](code/c/IFS.c)
{% sample lang="lisp" %}
[import, lang:"lisp"](code/clisp/ifs.lisp)
{%sample lang="coco" %}
[import, lang:"coconut"](code/coconut/IFS.coco)
{%sample lang="java" %}
Expand Down
28 changes: 28 additions & 0 deletions contents/IFS/code/clisp/ifs.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
;;;; Iterated Function System implementation

(defstruct (point (:constructor make-point (x y))) x y)

(defun chaos-game (iterations shape-points)
"Plays a chaos game with a certain shape for a determined amount of iterations"
(loop
repeat iterations
for rand-point = (svref shape-points (random (length shape-points)))
for point = (make-point (random 1.0) (random 1.0)) ; starting point
then (make-point
(* 0.5 (+ (point-x rand-point) (point-x point)))
(* 0.5 (+ (point-y rand-point) (point-y point)))) ; every subsequent point
collect point))

(defparameter *shape-points*
(map
'vector
(lambda (e) (apply #'make-point e))
;; the backquote allows us to selectively evaluate (sqrt 0.75) with the comma
`((0 0) (0.5 ,(sqrt 0.75)) (1 0))))

;; output the data to the "out.dat" file
(with-open-file (out "out.dat" :direction :output :if-exists :supersede)
(flet ((format-point (p)
;; this is not very clean, but it's the simplest way to insert a tab into a string.
(format nil "~f~c~f" (point-x p) #\tab (point-y p))))
(format out "~{~a~%~}" (map 'list #'format-point (chaos-game 10000 *shape-points*)))))
14 changes: 9 additions & 5 deletions contents/approximate_counting/approximate_counting.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Here is a table for the true count, approximate count, and percent error for 10,
| 500,000 | 499,813.2 | 0.037 |
| 1,000,000 | 999,466.0 | 0.053 |

Here, it seems that the percent error is 10 times higher when we count 10,000 items; however,
Here, it seems that the percent error is 10 times higher when we count 10,000 items; however,
with these numbers, I could imagine some people reading this are thinking that we are splitting hairs.
A 0.42% error is still really good, right?
Right.
Expand Down Expand Up @@ -200,7 +200,7 @@ To be clear, here is a table of several values that could be stored in a bitstri
| $$00000100 = 4$$ | $$15$$ |
| $$00010000 = 16$$ | $$65535$$ |
| $$01000000 = 64$$ | $$1.85 \times 10^{19}$$ |
| $$10000000 = 128$$ | $$3.40 \times 10^{38}$$ |
| $$10000000 = 128$$ | $$3.40 \times 10^{38}$$ |
| $$11111111 = 255$$ | $$5.79 \times 10^{76}$$ |

This means that we can hold from $$0$$ to $$2^{255} - 1 \approx 5.79 \times 10^{76}$$ with 8 bits using this new method.
Expand Down Expand Up @@ -250,7 +250,7 @@ In the next section, we will consider how to generalize this logarithmic method
## A slightly more general logarithm

Let's start by considering the differences between base $$2$$ and base $$e$$.
For base $$e$$,
For base $$e$$,

$$
\begin{align}
Expand Down Expand Up @@ -283,14 +283,14 @@ Going one step further, we need to chose a specific base to a logarithm that wil

$$
\begin{align}
v &= \frac{\log(1+n/a)}{\log(1+1/a)}. \\
v &= \frac{\log(1+n/a)}{\log(1+1/a)}. \\
n_v &= a\left(\left(1+\frac{1}{a}\right)^v-1\right).
\end{align}
$$

Here, $$a$$ is an effective tuning parameter and sets the maximum count allowed by the bitstring and the expected error.
The expression $$1+1/a$$ acts as a base for the logarithm and exponents and ensures that the first count of $$n=1$$ will also set the value $$v=1$$.
As an example, if the bitstring can be a maximum of 255 (for 8 bits) and we arbitrarily set
As an example, if the bitstring can be a maximum of 255 (for 8 bits) and we arbitrarily set
$$a=30$$, then the highest possible count with this approach will be $$\approx 130,000$$, which was the number reported in Morris's paper.
If we perform a few counting experiments, we find that this formula more closely tracks smaller numbers than before (when we were not using the logarithm):

Expand Down Expand Up @@ -360,8 +360,12 @@ As we do not have any objects to count, we will instead simulate the counting wi
{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/approximate_counting.jl)
{% sample lang="c" %}
[import, lang:"c"](code/c/approximate_counting.c)
{% sample lang="cpp" %}
[import, lang:"cpp"](code/c++/approximate_counting.cpp)
{% sample lang="python" %}
[import, lang:"python"](code/python/approximate_counting.py)
{% endmethod %}

### Bibliography
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ auto test_approximate_count(
int main() {
std::cout << "Counting Tests, 100 trials\n";

std::cout << "testing 1,000, a = 30, 1% error "
std::cout << "testing 1,000, a = 30, 10% error "
<< test_approximate_count(100, 1000, 30, 0.1) << "\n";
std::cout << "testing 12,345, a = 10, 1% error "
std::cout << "testing 12,345, a = 10, 10% error "
<< test_approximate_count(100, 12345, 10, 0.1) << "\n";
// Note : with a lower a, we need more trials, so a higher % error here.
std::cout << "testing 222,222, a = 0.5, 10% error "
std::cout << "testing 222,222, a = 0.5, 20% error "
<< test_approximate_count(100, 222222, 0.5, 0.2) << "\n";
}
82 changes: 82 additions & 0 deletions contents/approximate_counting/code/c/approximate_counting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// This function returns a pseudo-random number between 0 and 1
double drand()
{
return (double)rand() / RAND_MAX;
}

// This function takes
// - v: value in register
// - a: a scaling value for the logarithm based on Morris's paper
// It returns the approximate count
double n(double v, double a)
{
return a * (pow(1 + 1 / a, v) - 1);
}

// This function takes
// - v: value in register
// - a: a scaling value for the logarithm based on Morris's paper
// It returns a new value for v
double increment(double v, double a)
{
// delta is the probability of incrementing our counter
double delta = 1 / (n(v + 1, a) - n(v, a));

if (drand() <= delta) {
return v + 1;
}
return v;
}

// This function simulates counting and takes
// - n_items: number of items to count and loop over
// - a: a scaling value for the logarithm based on Morris's paper
// It returns n(v, a), the approximate count
double approximate_count(size_t n_items, double a)
{
int v = 0;
for (size_t i = 0; i < n_items; ++i) {
v = increment(v, a);
}

return n(v, a);
}

// This function takes
// - n_trials: the number of counting trials
// - n_items: the number off items to count
// - a: a scaling value for the logarithm based on Morris's paper
// - threshold: the maximum percent error allowed
// It terminates the program on failure
void test_approximation_count(size_t n_trials, size_t n_items, double a,
double threshold)
{
double sum = 0.0;
for (size_t i = 0; i < n_trials; ++i) {
sum += approximate_count(n_items, a);
}
double avg = sum / n_trials;

assert(fabs((avg - n_items) / n_items) < threshold);
}

int main()
{
srand(time(NULL));

printf("Counting Tests, 100 trials\n");
printf("testing 1000, a = 30, 10%% error\n");
test_approximation_count(100, 1000, 30, 0.1);
printf("testing 12345, a = 10, 10%% error\n");
test_approximation_count(100, 12345, 10, 0.1);
printf("testing 222222, a = 0.5, 20%% error\n");
test_approximation_count(100, 222222, 0.5, 0.2);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ function test_approximate_count(n_trials, n_items, a, threshold)
end

@testset "Counting Tests, 100 trials" begin
println("testing 1,000, a = 30, 1% error")
println("testing 1,000, a = 30, 10% error")
test_approximate_count(100, 1000, 30, 0.1)
println("testing 12,345, a = 10, 1% error")
println("testing 12,345, a = 10, 10% error")
test_approximate_count(100, 12345, 10, 0.1)
# Note: with a lower a, we need more trials, so a higher % error here.
println("testing 222,222, a = 0.5, 10% error")
println("testing 222,222, a = 0.5, 20% error")
test_approximate_count(100, 222222, 0.5, 0.2)
end
Loading