1
0
Go to file
2022-08-12 09:47:46 -07:00
.gitignore Added basic tests. 2022-08-10 08:38:33 -07:00
app.py Correted sorted 2022-08-12 09:47:46 -07:00
README.md Added basic tests. 2022-08-10 08:38:33 -07:00
tests.py Added basic tests. 2022-08-10 08:38:33 -07:00

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 its 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 well 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 its represented a list where the nth element of the list, is the evaluation for the number (n-1).