Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary search to Nim track #492

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@
"strings"
]
},
{
"slug": "binary-search",
"name": "Binary Search",
"uuid": "1b8f461e-8f44-4de2-be57-f3c2934cecf7",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"topics": [
"arrays",
"control_flow_if_else_statements",
"control_flow_loops",
"searching"
]
},
{
"slug": "word-count",
"name": "Word Count",
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/binary-search/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Implement a binary search algorithm.

Searching a sorted collection is a common task.
A dictionary is a sorted list of word definitions.
Given a word, one can find its definition.
A telephone book is a sorted list of people's names, addresses, and telephone numbers.
Knowing someone's name allows one to quickly find their telephone number and address.

If the list to be searched contains more than a few items (a dozen, say) a binary search will require far fewer comparisons than a linear search, but it imposes the requirement that the list be sorted.

In computer science, a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value.

In each step, the algorithm compares the search key value with the key value of the middle element of the array.

If the keys match, then a matching element has been found and its index, or position, is returned.

Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right.

If the remaining array to be searched is empty, then the key cannot be found in the array and a special "not found" indication is returned.

A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time.
A binary search is a dichotomic divide and conquer search algorithm.
19 changes: 19 additions & 0 deletions exercises/practice/binary-search/.meta/.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"razvbir-646"
],
"files": {
"solution": [
"binary_search.nim"
],
"test": [
"test_binary_search.nim"
],
"example": [
".meta/example.nim"
]
},
"blurb": "Implement a binary search algorithm.",
"source": "Wikipedia",
"source_url": "http://en.wikipedia.org/wiki/Binary_search_algorithm"
}
5 changes: 5 additions & 0 deletions exercises/practice/binary-search/.meta/example.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import std/algorithm

func binarySearch*(haystack: openArray[int], needle: int): int =
binarySearch(haystack, needle, system.cmp[int])

44 changes: 44 additions & 0 deletions exercises/practice/binary-search/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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"
include = false
4 changes: 4 additions & 0 deletions exercises/practice/binary-search/binary_search.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

func binarySearch*(haystack: openArray[int], needle: int): int =
discard

36 changes: 36 additions & 0 deletions exercises/practice/binary-search/test_binary_search.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest
import binary_search

suite "Binary Search":
test "finds a value in an array with one element":
check binarySearch([6], 6) == 0

test "finds a value in the middle of the array":
check binarySearch([1, 3, 4, 6, 8, 9, 11], 6) == 3

test "finds a value at the beginning of the array":
check binarySearch([1, 3, 4, 6, 8, 9, 11], 1) == 0

test "finds a value at the end of the array":
check binarySearch([1, 3, 4, 6, 8, 9, 11], 11) == 6

test "finds a value in an array of odd length":
check binarySearch([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144) == 9

test "finds a value in an array of even length":
check binarySearch([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21) == 5

test "identifies that a value is not included in the array":
check binarySearch([1, 3, 4, 6, 8, 9, 11], 7) == -1

test "a value smaller than the array's smallest value is not found":
check binarySearch([1, 3, 4, 6, 8, 9, 11], 0) == -1

test "a value larger than the array's largest value is not found":
check binarySearch([1, 3, 4, 6, 8, 9, 11], 13) == -1

test "nothing is found in an empty array":
check binarySearch([], 1) == -1

test "nothing is found when the left and right bounds cross":
check binarySearch([1, 2], 0) == -1