Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ node_modules

tmp
lib
**/*.res.js
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "binary-search",
"name": "Binary Search",
"uuid": "75286043-b11b-4653-bfe8-6854ef9fac23",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/acronym/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
**/*.res.js
4 changes: 2 additions & 2 deletions exercises/practice/acronym/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"scripts": {
"res:build": "rescript",
"res:start": "rescript build -w",
"test": "retest tmp/tests/*.js",
"ci": "npm run res:build && npm test"
"test": "rescript && retest **/tests/*.js",
"ci": "npm test"
}
}
1 change: 0 additions & 1 deletion exercises/practice/acronym/tests/Acronym_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ test("apostrophes", () => {
test("underscore emphasis", () => {
stringEqual(~message="underscore emphasis", abbreviate("The Road _Not_ Taken"), "TRNT")
})

29 changes: 29 additions & 0 deletions exercises/practice/binary-search/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Instructions

Your task is to implement a binary search algorithm.

A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we're looking for.
It allows us to quickly narrow down the possible locations of our item until we find it, or until we've eliminated all possible locations.

~~~~exercism/caution
Binary search only works when a list has been sorted.
~~~~

The algorithm looks like this:

- Find the middle element of a _sorted_ list and compare it with the item we're looking for.
- If the middle element is our item, then we're done!
- If the middle element is greater than our item, we can eliminate that element and all the elements **after** it.
- If the middle element is less than our item, we can eliminate that element and all the elements **before** it.
- If every element of the list has been eliminated then the item is not in the list.
- Otherwise, repeat the process on the part of the list that has not been eliminated.

Here's an example:

Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`.

- We start by comparing 23 with the middle element, 16.
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`.
- We then compare 23 with the new middle element, 28.
- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`.
- We've found our item.
13 changes: 13 additions & 0 deletions exercises/practice/binary-search/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Introduction

You have stumbled upon a group of mathematicians who are also singer-songwriters.
They have written a song for each of their favorite numbers, and, as you can imagine, they have a lot of favorite numbers (like [0][zero] or [73][seventy-three] or [6174][kaprekars-constant]).

You are curious to hear the song for your favorite number, but with so many songs to wade through, finding the right song could take a while.
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about.

You realize that you can use a binary search algorithm to quickly find a song given the title.

[zero]: https://en.wikipedia.org/wiki/0
[seventy-three]: https://en.wikipedia.org/wiki/73_(number)
[kaprekars-constant]: https://en.wikipedia.org/wiki/6174_(number)
2 changes: 2 additions & 0 deletions exercises/practice/binary-search/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
**/*.res.js
20 changes: 20 additions & 0 deletions exercises/practice/binary-search/.meta/BinarySearch.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let find = (array: array<int>, value: int): option<int> => {
let rec go = (lo, hi) =>
if lo > hi {
None
} else {
let mid = lo + (hi - lo) / 2
// Use getUnsafe because our bounds logic ensures 'mid' is valid
let midVal = Array.getUnsafe(array, mid)

if midVal < value {
go(mid + 1, hi)
} else if midVal > value {
go(lo, mid - 1)
} else {
Some(mid)
}
}

go(0, Array.length(array) - 1)
}
1 change: 1 addition & 0 deletions exercises/practice/binary-search/.meta/BinarySearch.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let find: (array<int>, int) => option<int>
24 changes: 24 additions & 0 deletions exercises/practice/binary-search/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"authors": [
"stevejb71",
"therealowenrees"
],
"files": {
"solution": [
"src/BinarySearch.res"
],
"test": [
"tests/BinarySearch_test.res"
],
"example": [
".meta/BinarySearch.res",
".meta/BinarySearch.resi"
],
"editor": [
"src/BinarySearch.resi"
]
},
"blurb": "Implement a binary search algorithm.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Binary_search_algorithm"
}
26 changes: 26 additions & 0 deletions exercises/practice/binary-search/.meta/testTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { assertEqual } from "../../../../test_generator/assertions.js";
import { generateTests } from '../../../../test_generator/testGenerator.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const slug = path.basename(path.resolve(__dirname, '..'))

// EDIT THIS WITH YOUR ASSERTIONS
export const assertionFunctions = [ assertEqual ]

// EDIT THIS WITH YOUR TEST TEMPLATES
export const template = (c) => {
const { array, value } = c.input
const expected = c.expected

const arrayStr = `[${array.join(', ')}]`

const expectedStr = (typeof expected === 'number')
? `Some(${expected})`
: "None"

return `assertEqual(~message="${c.description}", find(${arrayStr}, ${value}), ${expectedStr})`
}

generateTests(__dirname, slug, assertionFunctions, template)
43 changes: 43 additions & 0 deletions exercises/practice/binary-search/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[b55c24a9-a98d-4379-a08c-2adcf8ebeee8]
description = "finds a value in an array with one element"

[73469346-b0a0-4011-89bf-989e443d503d]
description = "finds a value in the middle of an array"

[327bc482-ab85-424e-a724-fb4658e66ddb]
description = "finds a value at the beginning of an array"

[f9f94b16-fe5e-472c-85ea-c513804c7d59]
description = "finds a value at the end of an array"

[f0068905-26e3-4342-856d-ad153cadb338]
description = "finds a value in an array of odd length"

[fc316b12-c8b3-4f5e-9e89-532b3389de8c]
description = "finds a value in an array of even length"

[da7db20a-354f-49f7-a6a1-650a54998aa6]
description = "identifies that a value is not included in the array"

[95d869ff-3daf-4c79-b622-6e805c675f97]
description = "a value smaller than the array's smallest value is not found"

[8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba]
description = "a value larger than the array's largest value is not found"

[f439a0fa-cf42-4262-8ad1-64bf41ce566a]
description = "nothing is found in an empty array"

[2c353967-b56d-40b8-acff-ce43115eed64]
description = "nothing is found when the left and right bounds cross"
21 changes: 21 additions & 0 deletions exercises/practice/binary-search/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading
Loading