Skip to content

Commit 766922c

Browse files
authored
Track access count in binary-search (#362)
* Track access count in binary-search * Move example out of stub * Remove instance variable from stub * Add traced-array to editor
1 parent fe69920 commit 766922c

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
],
1212
"example": [
1313
".meta/example.coffee"
14+
],
15+
"editor": [
16+
"traced-array.coffee"
1417
]
1518
},
1619
"blurb": "Implement a binary search algorithm.",
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class BinarySearch
2-
constructor: (@values) ->
2+
constructor: (@array) ->
33

44
find: (value) ->
55
start = 0
6-
end = @values.length - 1
6+
end = @array.length - 1
77
while start <= end
88
mid = (start + end) // 2
9-
item = @values[mid]
9+
item = @array.get mid
1010
if value == item
1111
return mid
1212
else if value <= item
@@ -16,4 +16,4 @@ class BinarySearch
1616

1717
throw new Error 'value not in array'
1818

19-
module.exports = BinarySearch
19+
module.exports = BinarySearch

exercises/practice/binary-search/binary-search.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class BinarySearch
2-
constructor: (values) ->
2+
constructor: (array) ->
33

44
find: (value) ->
55

Original file line numberDiff line numberDiff line change
@@ -1,62 +1,63 @@
11
BinarySearch = require './binary-search'
2+
TracedArray = require './traced-array'
23

34
describe 'Binary Search', ->
45
it 'finds a value in an array wxith one element', ->
5-
array = [6]
6-
result = new BinarySearch(array).find 6
7-
expect(result).toBe 0
6+
array = new TracedArray 6
7+
expect(new BinarySearch(array).find 6).toBe 0
8+
expect(array.accessCount).toBe 1
89

910
xit 'finds a value in the middle of an array', ->
10-
array = [1, 3, 4, 6, 8, 9, 11]
11-
result = new BinarySearch(array).find 6
12-
expect(result).toBe 3
11+
array = new TracedArray 1, 3, 4, 6, 8, 9, 11
12+
expect(new BinarySearch(array).find 6).toBe 3
13+
expect(array.accessCount).toBe 1
1314

1415
xit 'finds a value at the beginning of an array', ->
15-
array = [1, 3, 4, 6, 8, 9, 11]
16-
result = new BinarySearch(array).find 1
17-
expect(result).toBe 0
16+
array = new TracedArray 1, 3, 4, 6, 8, 9, 11
17+
expect(new BinarySearch(array).find 1).toBe 0
18+
expect(array.accessCount).toBe 3
1819

1920
xit 'finds a value at the end of an array', ->
20-
array = [1, 3, 4, 6, 8, 9, 11]
21-
result = new BinarySearch(array).find 11
22-
expect(result).toBe 6
21+
array = new TracedArray 1, 3, 4, 6, 8, 9, 11
22+
expect(new BinarySearch(array).find 11).toBe 6
23+
expect(array.accessCount).toBe 3
2324

2425
xit 'finds a value in an array of odd length', ->
25-
array = [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634]
26-
result = new BinarySearch(array).find 144
27-
expect(result).toBe 9
26+
array = new TracedArray 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634
27+
expect(new BinarySearch(array).find 144).toBe 9
28+
expect(array.accessCount).toBe 2
2829

2930
xit 'finds a value in an array of even length', ->
30-
array = [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
31-
result = new BinarySearch(array).find 21
32-
expect(result).toBe 5
31+
array = new TracedArray 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
32+
expect(new BinarySearch(array).find 21).toBe 5
33+
expect(array.accessCount).toBe 1
3334

3435
xit 'identifies that a value is not included in the array', ->
35-
array = [1, 3, 4, 6, 8, 9, 11]
36+
array = new TracedArray 1, 3, 4, 6, 8, 9, 11
3637
expect ->
3738
new BinarySearch(array).find 7
3839
.toThrow new Error 'value not in array'
3940

4041
xit "a value smaller than the array's smallest value is not found", ->
41-
array = [1, 3, 4, 6, 8, 9, 11]
42+
array = new TracedArray 1, 3, 4, 6, 8, 9, 11
4243
expect ->
4344
new BinarySearch(array).find 0
4445
.toThrow new Error 'value not in array'
4546

4647
xit "a value larger than the array's largest value is not found", ->
47-
array = [1, 3, 4, 6, 8, 9, 11]
48+
array = new TracedArray 1, 3, 4, 6, 8, 9, 11
4849
expect ->
4950
new BinarySearch(array).find 13
5051
.toThrow new Error 'value not in array'
5152

5253
xit 'nothing is found in an empty array', ->
53-
array = []
54+
array = new TracedArray
5455
expect ->
5556
new BinarySearch(array).find 1
5657
.toThrow new Error 'value not in array'
5758

5859
xit 'nothing is found when the left and right bounds cross', ->
59-
array = [1, 2]
60+
array = new TracedArray 1, 2
6061
expect ->
6162
new BinarySearch(array).find 0
6263
.toThrow new Error 'value not in array'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class TracedArray
2+
constructor: (values...) ->
3+
data = values
4+
@accessCount = 0
5+
6+
@get = (index) =>
7+
@accessCount += 1
8+
data[index]
9+
10+
Object.defineProperty @, 'length',
11+
get: -> data.length
12+
13+
module.exports = TracedArray

0 commit comments

Comments
 (0)