Skip to content

Commit

Permalink
Track access count in binary-search
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Nov 9, 2024
1 parent fe69920 commit 2174a1d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
15 changes: 14 additions & 1 deletion exercises/practice/binary-search/binary-search.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
class BinarySearch
constructor: (values) ->
constructor: (@array) ->

find: (value) ->
start = 0
end = @array.length - 1
while start <= end
mid = (start + end) // 2
item = @array.get mid
if value == item
return mid
else if value <= item
end = mid - 1
else if value >= item
start = mid + 1

throw new Error 'value not in array'

module.exports = BinarySearch
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 2174a1d

Please sign in to comment.