Skip to content

Commit 99a59e1

Browse files
binary-search uses Extended
We improve the exercise suite's coverage of numeric types. resolves #41
1 parent df6e765 commit 99a59e1

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

exercises/practice/binary-search/.meta/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"authors": [
33
"jimmytty"
44
],
5+
"contributors": [
6+
"keiravillekode"
7+
],
58
"files": {
69
"solution": [
710
"BinarySearch.pas"

exercises/practice/binary-search/.meta/example.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
interface
66

77
type
8-
TIntArray = Array Of Integer;
8+
TExtendedArray = Array Of Extended;
99

10-
function find(const AArray: TIntArray; const AValue : integer) : integer;
10+
function find(const AArray: TExtendedArray; const AValue : Extended) : Integer;
1111

1212
implementation
1313

1414
uses SysUtils;
1515

16-
function find(const AArray: TIntArray; const AValue : integer) : integer;
16+
function find(const AArray: TExtendedArray; const AValue : Extended) : Integer;
1717
var
1818
i, j, mid : integer;
1919
WasFound : boolean;

exercises/practice/binary-search/BinarySearch.pas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
interface
66

77
type
8-
TIntArray = Array Of Integer;
8+
TExtendedArray = Array Of Extended;
99

10-
function find(const AArray: TIntArray; const AValue : integer) : integer;
10+
function find(const AArray: TExtendedArray; const AValue : Extended) : Integer;
1111

1212
implementation
1313

1414
uses SysUtils;
1515

16-
function find(const AArray: TIntArray; const AValue : integer) : integer;
16+
function find(const AArray: TExtendedArray; const AValue : Extended) : Integer;
1717
begin
1818

19-
raise ENotImplemented.Create('Please implement your solution.'); result := length(AArray) * Avalue;
19+
raise ENotImplemented.Create('Please implement your solution.'); result := Round(length(AArray) * Avalue);
2020

2121
end;
2222

exercises/practice/binary-search/TestCases.pas

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ BinarySearchTest = class(TTestCase)
1616
procedure finds_a_value_in_an_array_of_odd_length;
1717
procedure finds_a_value_in_an_array_of_even_length;
1818
procedure identifies_that_a_value_is_not_included_in_the_array;
19-
procedure a_value_smaller_than_the_array_s_smallest_value_is_not_found;
20-
procedure a_value_larger_than_the_array_s_largest_value_is_not_found;
19+
procedure a_value_smaller_than_the_arrays_smallest_value_is_not_found;
20+
procedure a_value_larger_than_the_arrays_largest_value_is_not_found;
2121
procedure nothing_is_found_in_an_empty_array;
2222
procedure nothing_is_found_when_the_left_and_right_bounds_cross;
2323
end;
@@ -30,8 +30,8 @@ procedure TapAssertExpectionMessage(
3030
ACase : TTestCase ;
3131
const AMessage : string ;
3232
const Expected : string ;
33-
const AArray : TIntArray ;
34-
const AValue : integer
33+
const AArray : TExtendedArray ;
34+
const AValue : Extended
3535
);
3636
var
3737
JsonMsg : string;
@@ -46,71 +46,103 @@ procedure TapAssertExpectionMessage(
4646
JsonMsg := EncodeJsonMessage(AMessage, expected, actual);
4747
ACase.AssertTrue(JsonMsg, expected = actual);
4848
end;
49-
5049
// b55c24a9-a98d-4379-a08c-2adcf8ebeee8
5150
procedure BinarySearchTest.finds_a_value_in_an_array_with_one_element;
51+
const
52+
haystack : TExtendedArray = (6);
53+
needle : Extended = 6;
5254
begin
53-
TapAssertTrue(Self, 'finds a value in an array with one element', 0, BinarySearch.find([6], 6));
55+
TapAssertTrue(Self, 'finds a value in an array with one element', 0, BinarySearch.find(haystack, needle));
5456
end;
5557

5658
// 73469346-b0a0-4011-89bf-989e443d503d
5759
procedure BinarySearchTest.finds_a_value_in_the_middle_of_an_array;
60+
const
61+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
62+
needle : Extended = 6;
5863
begin
59-
TapAssertTrue(Self, 'finds a value in the middle of an array', 3, BinarySearch.find([1,3,4,6,8,9,11], 6));
64+
TapAssertTrue(Self, 'finds a value in the middle of an array', 3, BinarySearch.find(haystack, needle));
6065
end;
6166

6267
// 327bc482-ab85-424e-a724-fb4658e66ddb
6368
procedure BinarySearchTest.finds_a_value_at_the_beginning_of_an_array;
69+
const
70+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
71+
needle : Extended = 1;
6472
begin
65-
TapAssertTrue(Self, 'finds a value at the beginning of an array', 0, BinarySearch.find([1,3,4,6,8,9,11], 1));
73+
TapAssertTrue(Self, 'finds a value at the beginning of an array', 0, BinarySearch.find(haystack, needle));
6674
end;
6775

6876
// f9f94b16-fe5e-472c-85ea-c513804c7d59
6977
procedure BinarySearchTest.finds_a_value_at_the_end_of_an_array;
78+
const
79+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
80+
needle : Extended = 11;
7081
begin
71-
TapAssertTrue(Self, 'finds a value at the end of an array', 6, BinarySearch.find([1,3,4,6,8,9,11], 11));
82+
TapAssertTrue(Self, 'finds a value at the end of an array', 6, BinarySearch.find(haystack, needle));
7283
end;
7384

7485
// f0068905-26e3-4342-856d-ad153cadb338
7586
procedure BinarySearchTest.finds_a_value_in_an_array_of_odd_length;
87+
const
88+
haystack : TExtendedArray = (1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634);
89+
needle : Extended = 144;
7690
begin
77-
TapAssertTrue(Self, 'finds a value in an array of odd length', 9, BinarySearch.find([1,3,5,8,13,21,34,55,89,144,233,377,634], 144));
91+
TapAssertTrue(Self, 'finds a value in an array of odd length', 9, BinarySearch.find(haystack, needle));
7892
end;
7993

8094
// fc316b12-c8b3-4f5e-9e89-532b3389de8c
8195
procedure BinarySearchTest.finds_a_value_in_an_array_of_even_length;
96+
const
97+
haystack : TExtendedArray = (1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377);
98+
needle : Extended = 21;
8299
begin
83-
TapAssertTrue(Self, 'finds a value in an array of even length', 5, BinarySearch.find([1,3,5,8,13,21,34,55,89,144,233,377], 21));
100+
TapAssertTrue(Self, 'finds a value in an array of even length', 5, BinarySearch.find(haystack, needle));
84101
end;
85102

86103
// da7db20a-354f-49f7-a6a1-650a54998aa6
87104
procedure BinarySearchTest.identifies_that_a_value_is_not_included_in_the_array;
105+
const
106+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
107+
needle : Extended = 7;
88108
begin
89-
TapAssertExpectionMessage(Self, 'identifies that a value is not included in the array', 'value not in array', [1,3,4,6,8,9,11], 7);
109+
TapAssertExpectionMessage(Self, 'identifies that a value is not included in the array', 'value not in array', haystack, needle);
90110
end;
91111

92112
// 95d869ff-3daf-4c79-b622-6e805c675f97
93-
procedure BinarySearchTest.a_value_smaller_than_the_array_s_smallest_value_is_not_found;
113+
procedure BinarySearchTest.a_value_smaller_than_the_arrays_smallest_value_is_not_found;
114+
const
115+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
116+
needle : Extended = 0;
94117
begin
95-
TapAssertExpectionMessage(Self, 'a value smaller than the array''s smallest value is not found', 'value not in array', [1,3,4,6,8,9,11], 0);
118+
TapAssertExpectionMessage(Self, 'a value smaller than the array''s smallest value is not found', 'value not in array', haystack, needle);
96119
end;
97120

98121
// 8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba
99-
procedure BinarySearchTest.a_value_larger_than_the_array_s_largest_value_is_not_found;
122+
procedure BinarySearchTest.a_value_larger_than_the_arrays_largest_value_is_not_found;
123+
const
124+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
125+
needle : Extended = 13;
100126
begin
101-
TapAssertExpectionMessage(Self, 'a value larger than the array''s largest value is not found', 'value not in array', [1,3,4,6,8,9,11], 13);
127+
TapAssertExpectionMessage(Self, 'a value larger than the array''s largest value is not found', 'value not in array', haystack, needle);
102128
end;
103129

104130
// f439a0fa-cf42-4262-8ad1-64bf41ce566a
105131
procedure BinarySearchTest.nothing_is_found_in_an_empty_array;
132+
const
133+
haystack : TExtendedArray = ();
134+
needle : Extended = 1;
106135
begin
107-
TapAssertExpectionMessage(Self, 'nothing is found in an empty array', 'value not in array', [], 1);
136+
TapAssertExpectionMessage(Self, 'nothing is found in an empty array', 'value not in array', haystack, needle);
108137
end;
109138

110139
// 2c353967-b56d-40b8-acff-ce43115eed64
111140
procedure BinarySearchTest.nothing_is_found_when_the_left_and_right_bounds_cross;
141+
const
142+
haystack : TExtendedArray = (1, 2);
143+
needle : Extended = 0;
112144
begin
113-
TapAssertExpectionMessage(Self, 'nothing is found when the left and right bounds cross', 'value not in array', [1,2], 0);
145+
TapAssertExpectionMessage(Self, 'nothing is found when the left and right bounds cross', 'value not in array', haystack, needle);
114146
end;
115147

116148
initialization

0 commit comments

Comments
 (0)