Skip to content

Commit

Permalink
Add binary-search exercise (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 30, 2024
1 parent 924e6bb commit 9d9a713
Show file tree
Hide file tree
Showing 13 changed files with 3,633 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "binary-search",
"name": "Binary Search",
"uuid": "ee094601-1e0a-44dc-b885-a2caf92491f9",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "luhn",
"name": "Luhn",
Expand Down
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)
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": [
"keiravillekode"
],
"files": {
"solution": [
"binary_search.s"
],
"test": [
"binary_search_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Implement a binary search algorithm.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Binary_search_algorithm"
}
37 changes: 37 additions & 0 deletions exercises/practice/binary-search/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.text
.globl find

/* extern ptrdiff_t find(int16_t value, int16_t *array, size_t count); */
find:
mov x3, #0 /* lower bound, as byte offset */
lsl x4, x2, #1 /* upper bound, as byte offset */

.search:
cmp x3, x4
beq .absent

add x5, x3, x4
lsr x5, x5, #2 /* midpoint, as index */
lsl x5, x5, #1 /* midpoint, as byte offset */
ldrh w6, [x1, x5]

cmp w6, w0
bgt .earlier

cmp w6, w0
blt .later

lsr x0, x5, #1 /* midpoint, as index */
ret

.absent:
mov x0, #-1
ret

.earlier:
mov x4, x5 /* update upper bound */
b .search

.later:
add x3, x5, #2 /* update lower bound */
b .search
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"
36 changes: 36 additions & 0 deletions exercises/practice/binary-search/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/binary-search/binary_search.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl find

find:
ret
101 changes: 101 additions & 0 deletions exercises/practice/binary-search/binary_search_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "vendor/unity.h"

#include <stddef.h>
#include <stdint.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

extern ptrdiff_t find(int16_t value, int16_t *array, size_t count);

void setUp(void) {
}

void tearDown(void) {
}

void test_finds_a_value_in_an_array_with_one_element(void) {
int16_t array[] = {6};
TEST_ASSERT_EQUAL_INT(0, find(6, array, ARRAY_SIZE(array)));
}

void test_finds_a_value_in_the_middle_of_an_array(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 4, 6, 8, 9, 11};
TEST_ASSERT_EQUAL_INT(3, find(6, array, ARRAY_SIZE(array)));
}

void test_finds_a_value_at_the_beginning_of_an_array(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 4, 6, 8, 9, 11};
TEST_ASSERT_EQUAL_INT(0, find(1, array, ARRAY_SIZE(array)));
}

void test_finds_a_value_at_the_end_of_an_array(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 4, 6, 8, 9, 11};
TEST_ASSERT_EQUAL_INT(6, find(11, array, ARRAY_SIZE(array)));
}

void test_finds_a_value_in_an_array_of_odd_length(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634};
TEST_ASSERT_EQUAL_INT(9, find(144, array, ARRAY_SIZE(array)));
}

void test_finds_a_value_in_an_array_of_even_length(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377};
TEST_ASSERT_EQUAL_INT(5, find(21, array, ARRAY_SIZE(array)));
}

void test_identifies_that_a_value_is_not_included_in_the_array(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 4, 6, 8, 9, 11};
TEST_ASSERT_EQUAL_INT(-1, find(7, array, ARRAY_SIZE(array)));
}

void test_a_value_smaller_than_the_arrays_smallest_value_is_not_found(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 4, 6, 8, 9, 11};
TEST_ASSERT_EQUAL_INT(-1, find(0, array, ARRAY_SIZE(array)));
}

void test_a_value_larger_than_the_arrays_largest_value_is_not_found(void) {
TEST_IGNORE();
int16_t array[] = {1, 3, 4, 6, 8, 9, 11};
TEST_ASSERT_EQUAL_INT(-1, find(13, array, ARRAY_SIZE(array)));
}

void test_nothing_is_found_in_an_empty_array(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(-1, find(1, NULL, 0));
}

void test_nothing_is_found_when_the_left_and_right_bounds_cross(void) {
TEST_IGNORE();
int16_t array[] = {1, 2};
TEST_ASSERT_EQUAL_INT(-1, find(0, array, ARRAY_SIZE(array)));
}

void test_five_digits(void) {
TEST_IGNORE();
int16_t array[] = {11638, 18389, 21454, 29416, 32039};
TEST_ASSERT_EQUAL_INT(2, find(21454, array, ARRAY_SIZE(array)));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_finds_a_value_in_an_array_with_one_element);
RUN_TEST(test_finds_a_value_in_the_middle_of_an_array);
RUN_TEST(test_finds_a_value_at_the_beginning_of_an_array);
RUN_TEST(test_finds_a_value_at_the_end_of_an_array);
RUN_TEST(test_finds_a_value_in_an_array_of_odd_length);
RUN_TEST(test_finds_a_value_in_an_array_of_even_length);
RUN_TEST(test_identifies_that_a_value_is_not_included_in_the_array);
RUN_TEST(test_a_value_smaller_than_the_arrays_smallest_value_is_not_found);
RUN_TEST(test_a_value_larger_than_the_arrays_largest_value_is_not_found);
RUN_TEST(test_nothing_is_found_in_an_empty_array);
RUN_TEST(test_nothing_is_found_when_the_left_and_right_bounds_cross);
RUN_TEST(test_five_digits);
return UNITY_END();
}
Loading

0 comments on commit 9d9a713

Please sign in to comment.