|
| 1 | +# Binary Search |
| 2 | + |
| 3 | +Implement a binary search algorithm. |
| 4 | + |
| 5 | +Searching a sorted collection is a common task. A dictionary is a sorted |
| 6 | +list of word definitions. Given a word, one can find its definition. A |
| 7 | +telephone book is a sorted list of people's names, addresses, and |
| 8 | +telephone numbers. Knowing someone's name allows one to quickly find |
| 9 | +their telephone number and address. |
| 10 | + |
| 11 | +If the list to be searched contains more than a few items (a dozen, say) |
| 12 | +a binary search will require far fewer comparisons than a linear search, |
| 13 | +but it imposes the requirement that the list be sorted. |
| 14 | + |
| 15 | +In computer science, a binary search or half-interval search algorithm |
| 16 | +finds the position of a specified input value (the search "key") within |
| 17 | +an array sorted by key value. |
| 18 | + |
| 19 | +In each step, the algorithm compares the search key value with the key |
| 20 | +value of the middle element of the array. |
| 21 | + |
| 22 | +If the keys match, then a matching element has been found and its index, |
| 23 | +or position, is returned. |
| 24 | + |
| 25 | +Otherwise, if the search key is less than the middle element's key, then |
| 26 | +the algorithm repeats its action on the sub-array to the left of the |
| 27 | +middle element or, if the search key is greater, on the sub-array to the |
| 28 | +right. |
| 29 | + |
| 30 | +If the remaining array to be searched is empty, then the key cannot be |
| 31 | +found in the array and a special "not found" indication is returned. |
| 32 | + |
| 33 | +A binary search halves the number of items to check with each iteration, |
| 34 | +so locating an item (or determining its absence) takes logarithmic time. |
| 35 | +A binary search is a dichotomic divide and conquer search algorithm. |
| 36 | + |
| 37 | +## Exception messages |
| 38 | + |
| 39 | +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to |
| 40 | +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not |
| 41 | +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include |
| 42 | +a message. |
| 43 | + |
| 44 | +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of |
| 45 | +`raise Exception`, you should write: |
| 46 | + |
| 47 | +```python |
| 48 | +raise Exception("Meaningful message indicating the source of the error") |
| 49 | +``` |
| 50 | + |
| 51 | +## Running the tests |
| 52 | + |
| 53 | +To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): |
| 54 | + |
| 55 | +- Python 2.7: `py.test binary_search_test.py` |
| 56 | +- Python 3.4+: `pytest binary_search_test.py` |
| 57 | + |
| 58 | +Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): |
| 59 | +`python -m pytest binary_search_test.py` |
| 60 | + |
| 61 | +### Common `pytest` options |
| 62 | + |
| 63 | +- `-v` : enable verbose output |
| 64 | +- `-x` : stop running tests on first failure |
| 65 | +- `--ff` : run failures from previous test before running other test cases |
| 66 | + |
| 67 | +For other options, see `python -m pytest -h` |
| 68 | + |
| 69 | +## Submitting Exercises |
| 70 | + |
| 71 | +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/binary-search` directory. |
| 72 | + |
| 73 | +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. |
| 74 | + |
| 75 | +For more detailed information about running tests, code style and linting, |
| 76 | +please see [Running the Tests](http://exercism.io/tracks/python/tests). |
| 77 | + |
| 78 | +## Source |
| 79 | + |
| 80 | +Wikipedia [http://en.wikipedia.org/wiki/Binary_search_algorithm](http://en.wikipedia.org/wiki/Binary_search_algorithm) |
| 81 | + |
| 82 | +## Submitting Incomplete Solutions |
| 83 | + |
| 84 | +It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
0 commit comments