Skip to content

Commit

Permalink
Track access count in binary-search (#362)
Browse files Browse the repository at this point in the history
* Track access count in binary-search

* Move example out of stub

* Remove instance variable from stub

* Add traced-array to editor
  • Loading branch information
BNAndras authored Nov 10, 2024
1 parent fe69920 commit 766922c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
3 changes: 3 additions & 0 deletions exercises/practice/binary-search/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
],
"example": [
".meta/example.coffee"
],
"editor": [
"traced-array.coffee"
]
},
"blurb": "Implement a binary search algorithm.",
Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/binary-search/.meta/example.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class BinarySearch
constructor: (@values) ->
constructor: (@array) ->

find: (value) ->
start = 0
end = @values.length - 1
end = @array.length - 1
while start <= end
mid = (start + end) // 2
item = @values[mid]
item = @array.get mid
if value == item
return mid
else if value <= item
Expand All @@ -16,4 +16,4 @@ class BinarySearch

throw new Error 'value not in array'

module.exports = BinarySearch
module.exports = BinarySearch
2 changes: 1 addition & 1 deletion exercises/practice/binary-search/binary-search.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BinarySearch
constructor: (values) ->
constructor: (array) ->

find: (value) ->

Expand Down
47 changes: 24 additions & 23 deletions exercises/practice/binary-search/binary-search.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
BinarySearch = require './binary-search'
TracedArray = require './traced-array'

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

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

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

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

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

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

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

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

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

xit 'nothing is found in an empty array', ->
array = []
array = new TracedArray
expect ->
new BinarySearch(array).find 1
.toThrow new Error 'value not in array'

xit 'nothing is found when the left and right bounds cross', ->
array = [1, 2]
array = new TracedArray 1, 2
expect ->
new BinarySearch(array).find 0
.toThrow new Error 'value not in array'
13 changes: 13 additions & 0 deletions exercises/practice/binary-search/traced-array.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class TracedArray
constructor: (values...) ->
data = values
@accessCount = 0

@get = (index) =>
@accessCount += 1
data[index]

Object.defineProperty @, 'length',
get: -> data.length

module.exports = TracedArray

0 comments on commit 766922c

Please sign in to comment.