|
1 | 1 | # AlgoKit: Small Algorithm Practice |
2 | 2 |
|
3 | | -A dependency-free collection of beginner-friendly algorithms with tests and clear input contracts. |
| 3 | +A dependency-free collection of beginner-friendly algorithms with unit and deterministic randomized tests. |
4 | 4 |
|
5 | 5 | ## Run |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | python3 algokit.py |
9 | | -python3 -m unittest -v |
| 9 | +python3 -m unittest discover -v |
| 10 | +python3 benchmark_search.py |
10 | 11 | ``` |
11 | 12 |
|
12 | | -Run the commands from this directory. Python 3.8+ is recommended. |
| 13 | +Run these commands from this directory. Python 3.8+ is recommended. |
13 | 14 |
|
14 | 15 | ## Included algorithms |
15 | 16 |
|
16 | 17 | | Function | Purpose | Typical time | |
17 | 18 | |---|---|---| |
18 | 19 | | `linear_search(values, target)` | Find the first matching item | O(n) | |
19 | 20 | | `binary_search(values, target)` | Search ascending sorted values | O(log n) | |
| 21 | +| `two_sum(values, target)` | Find two indices whose values sum to target | O(n) average | |
20 | 22 | | `frequencies(items)` | Count occurrences of hashable items | O(n) average | |
21 | 23 | | `brackets_are_balanced(text)` | Check nesting of (), [], {} | O(n) | |
22 | 24 |
|
23 | | -Binary search requires ascending sorted input. The bracket checker ignores non-bracket characters and is not a parser for programming-language strings or comments. |
| 25 | +Binary search requires ascending sorted input. `two_sum` returns a pair of distinct indices or `None`. The bracket checker ignores non-bracket characters and is not a parser for programming-language strings or comments. |
24 | 26 |
|
25 | | -## Suggested extensions |
| 27 | +## Benchmark |
26 | 28 |
|
27 | | -- Add a benchmark comparing linear and binary search on sorted lists. |
28 | | -- Generate randomized test cases and compare both search functions. |
29 | | -- Add a function that returns the first matching index in binary search. |
30 | | -- Measure memory usage as well as runtime. |
| 29 | +Compare the worst-case successful linear-search position with binary search: |
| 30 | + |
| 31 | +```bash |
| 32 | +python3 benchmark_search.py --sizes 1000 10000 100000 --repeats 20 |
| 33 | +``` |
| 34 | + |
| 35 | +Benchmark timings vary by machine and should be interpreted as observations, not guarantees. The script verifies the answers before printing timings. |
| 36 | + |
| 37 | +## Test coverage |
| 38 | + |
| 39 | +The unit tests include edge cases, repeated values, invalid bracket nesting, and fixed-seed randomized comparisons against a reference search. CI runs on Python 3.11 and 3.13. |
0 commit comments