1.6 KiB
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
DataCaptureobject 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,
- An object capable of gathering an arbitrary amount of numbers, (assumed to be positive, and less than 1001).
- 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).