Skip to content

Commit 1bfcfe7

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

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
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 & 17 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;
@@ -49,68 +49,101 @@ procedure TapAssertExpectionMessage(
4949

5050
// b55c24a9-a98d-4379-a08c-2adcf8ebeee8
5151
procedure BinarySearchTest.finds_a_value_in_an_array_with_one_element;
52+
const
53+
haystack : TExtendedArray = (6);
54+
needle : Extended = 6;
5255
begin
53-
TapAssertTrue(Self, 'finds a value in an array with one element', 0, BinarySearch.find([6], 6));
56+
TapAssertTrue(Self, 'finds a value in an array with one element', 0, BinarySearch.find(haystack, needle));
5457
end;
5558

5659
// 73469346-b0a0-4011-89bf-989e443d503d
5760
procedure BinarySearchTest.finds_a_value_in_the_middle_of_an_array;
61+
const
62+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
63+
needle : Extended = 6;
5864
begin
59-
TapAssertTrue(Self, 'finds a value in the middle of an array', 3, BinarySearch.find([1,3,4,6,8,9,11], 6));
65+
TapAssertTrue(Self, 'finds a value in the middle of an array', 3, BinarySearch.find(haystack, needle));
6066
end;
6167

6268
// 327bc482-ab85-424e-a724-fb4658e66ddb
6369
procedure BinarySearchTest.finds_a_value_at_the_beginning_of_an_array;
70+
const
71+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
72+
needle : Extended = 1;
6473
begin
65-
TapAssertTrue(Self, 'finds a value at the beginning of an array', 0, BinarySearch.find([1,3,4,6,8,9,11], 1));
74+
TapAssertTrue(Self, 'finds a value at the beginning of an array', 0, BinarySearch.find(haystack, needle));
6675
end;
6776

6877
// f9f94b16-fe5e-472c-85ea-c513804c7d59
6978
procedure BinarySearchTest.finds_a_value_at_the_end_of_an_array;
79+
const
80+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
81+
needle : Extended = 11;
7082
begin
71-
TapAssertTrue(Self, 'finds a value at the end of an array', 6, BinarySearch.find([1,3,4,6,8,9,11], 11));
83+
TapAssertTrue(Self, 'finds a value at the end of an array', 6, BinarySearch.find(haystack, needle));
7284
end;
7385

7486
// f0068905-26e3-4342-856d-ad153cadb338
7587
procedure BinarySearchTest.finds_a_value_in_an_array_of_odd_length;
88+
const
89+
haystack : TExtendedArray = (1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634);
90+
needle : Extended = 144;
7691
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));
92+
TapAssertTrue(Self, 'finds a value in an array of odd length', 9, BinarySearch.find(haystack, needle));
7893
end;
7994

8095
// fc316b12-c8b3-4f5e-9e89-532b3389de8c
8196
procedure BinarySearchTest.finds_a_value_in_an_array_of_even_length;
97+
const
98+
haystack : TExtendedArray = (1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377);
99+
needle : Extended = 21;
82100
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));
101+
TapAssertTrue(Self, 'finds a value in an array of even length', 5, BinarySearch.find(haystack, needle));
84102
end;
85103

86104
// da7db20a-354f-49f7-a6a1-650a54998aa6
87105
procedure BinarySearchTest.identifies_that_a_value_is_not_included_in_the_array;
106+
const
107+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
108+
needle : Extended = 7;
88109
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);
110+
TapAssertExpectionMessage(Self, 'identifies that a value is not included in the array', 'value not in array', haystack, needle);
90111
end;
91112

92113
// 95d869ff-3daf-4c79-b622-6e805c675f97
93-
procedure BinarySearchTest.a_value_smaller_than_the_array_s_smallest_value_is_not_found;
114+
procedure BinarySearchTest.a_value_smaller_than_the_arrays_smallest_value_is_not_found;
115+
const
116+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
117+
needle : Extended = 0;
94118
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);
119+
TapAssertExpectionMessage(Self, 'a value smaller than the arrays smallest value is not found', 'value not in array', haystack, needle);
96120
end;
97121

98122
// 8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba
99-
procedure BinarySearchTest.a_value_larger_than_the_array_s_largest_value_is_not_found;
123+
procedure BinarySearchTest.a_value_larger_than_the_arrays_largest_value_is_not_found;
124+
const
125+
haystack : TExtendedArray = (1, 3, 4, 6, 8, 9, 11);
126+
needle : Extended = 13;
100127
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);
128+
TapAssertExpectionMessage(Self, 'a value larger than the arrays largest value is not found', 'value not in array', haystack, needle);
102129
end;
103130

104131
// f439a0fa-cf42-4262-8ad1-64bf41ce566a
105132
procedure BinarySearchTest.nothing_is_found_in_an_empty_array;
133+
const
134+
haystack : TExtendedArray = ();
135+
needle : Extended = 1;
106136
begin
107-
TapAssertExpectionMessage(Self, 'nothing is found in an empty array', 'value not in array', [], 1);
137+
TapAssertExpectionMessage(Self, 'nothing is found in an empty array', 'value not in array', haystack, needle);
108138
end;
109139

110140
// 2c353967-b56d-40b8-acff-ce43115eed64
111141
procedure BinarySearchTest.nothing_is_found_when_the_left_and_right_bounds_cross;
142+
const
143+
haystack : TExtendedArray = (1, 2);
144+
needle : Extended = 0;
112145
begin
113-
TapAssertExpectionMessage(Self, 'nothing is found when the left and right bounds cross', 'value not in array', [1,2], 0);
146+
TapAssertExpectionMessage(Self, 'nothing is found when the left and right bounds cross', 'value not in array', haystack, needle);
114147
end;
115148

116149
initialization

0 commit comments

Comments
 (0)