30 lines
1.6 KiB
Markdown
30 lines
1.6 KiB
Markdown
Solution for a code challenge:
|
|
|
|
- The challenge is to create a program that computes some basic statistics on a collection of small positive integers. You can assume all values will be less than 1000.
|
|
- The `DataCapture` object accepts numbers and returns an object for querying statistics about the inputs. Specifically, the returned object supports querying how many numbers in the collection are less than a value, greater than a value, or within a range.
|
|
|
|
# Executing
|
|
|
|
The solution is implemented in a single file, and it's targeting `python39`.
|
|
|
|
To execute the example you can just run `python app.py` assuming you are using the correct version, and
|
|
that you are located in the directory for this project.
|
|
|
|
To execute tests you can run `python -m unittest tests.py` or `pytest tests.py` (provided you installed `pytest`).
|
|
|
|
# About the solution
|
|
|
|
The problem can be divided in two parts,
|
|
|
|
1. An object capable of _gathering_ an arbitrary amount of numbers, (assumed to be positive, and less than 1001).
|
|
2. Generate an object that can query in constant time, counts numbers in certain ranges.
|
|
|
|
So, with the first object we construct a _frequency function_ that will tell us how many repetition of a given
|
|
number we have.
|
|
|
|
Then we define a function that would be the _integral_ of the frequency function, such that whenever we _evaluate it_
|
|
we'll get how many numbers exist that are less than _it_.
|
|
|
|
When we say function, we are not talking about a function in programming terms, we are talking about a discrete function as a _rule of correspondence_, and internally in this code it's represented a list where the nth element of the
|
|
list, is the evaluation for the number (n-1).
|