omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / exercises / binary-search / .meta / example.om
| 1 | note Exercism-style exercise "binary-search": https://exercism.org/tracks/python/exercises/binary-search |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note find is the index of v in the ascending array a, found by binary search, or -1 when v is not |
| 4 | note in a. |
| 5 | note Check it with: sh exercise.sh exercises/binary-search |
| 6 | om2 |
| 7 | fn find a view ulong * v ulong to long |
| 8 | lo = ulong 0 |
| 9 | hi = len a |
| 10 | while lt lo hi bound 64 |
| 11 | m = add lo div sub hi lo 2 |
| 12 | x = get a m |
| 13 | if eq x v |
| 14 | ret long m |
| 15 | end |
| 16 | if lt x v |
| 17 | lo = add m 1 |
| 18 | else |
| 19 | hi = m |
| 20 | end |
| 21 | end |
| 22 | ret -1 |
| 23 | end |