Skip to content

Commit 6e2a926

Browse files
dlesnoffSatinWukerORIGZoomRmc
committed
Fix linear search (#33)
* Use a dynamic allocated array (sequence) for strings * Run nimpretty * Add requested changes * Fix documentation generation * Fix test title according to changes Co-authored-by: Satin Wuker <[email protected]> * Update comments to warn about indexing issues Modified a bit the suggestions of comments. Please check them before approving. Co-authored-by: Zoom <[email protected]> --------- Co-authored-by: Dimitri LESNOFF <[email protected]> Co-authored-by: Satin Wuker <[email protected]> Co-authored-by: Zoom <[email protected]>
1 parent 41915d6 commit 6e2a926

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

searches/linear_search.nim

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
## Therefore the recursive linear search is less efficient than the for-loop-based one.
1717

1818
runnableExamples:
19+
import std/options
20+
1921
var arr1 = [0, 3, 1, 4, 5, 6]
2022
doAssert linearSearch(arr1, 5) == some(Natural(4))
2123
doAssert recursiveLinearSearch(arr1, 5) == some(Natural(4))
@@ -28,23 +30,25 @@ runnableExamples:
2830
doAssert linearSearch(arr3, 7) == none(Natural)
2931
doAssert recursiveLinearSearch(arr3, 7) == none(Natural)
3032

31-
3233
import std/options
3334

34-
type
35-
Nat = Natural
36-
OptNat = Option[Natural]
37-
38-
func linearSearch*[T](arr: openArray[T], key: T): OptNat =
39-
# key is the value we are searching for in the array.
35+
func linearSearch*[T](arr: openArray[T], key: T): Option[Natural] =
36+
## Searches for the `key` in the array `arr` and returns its absolute index (counting from 0)
37+
## in the array.
38+
## .. Note:: For arrays indexed with a range type or an enum the returned value
39+
## may not be consistent with the indexing of the initial array.
4040
for i, val in arr.pairs():
4141
if val == key:
4242
return some(Natural(i))
4343
none(Natural) # `key` not found
4444

45-
func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat): OptNat=
46-
# Recursion is another method for linear search.
47-
# Recursive calls replace the for loop.
45+
func recursiveLinearSearch*[T](
46+
arr: openArray[T], key: T, idx: Natural = Natural(0)): Option[Natural] =
47+
## Searches for the `key` in `arr` and returns its absolute index (counting from 0)
48+
## in the array. Search is performed in a recursive manner.
49+
## .. Note:: For arrays indexed with a range type or an enum the returned value
50+
## is not consistent with the indexing of the parameter array if `arr.low` is not 0.
51+
# Recursive calls replace the `for` loop in `linearSearch`.
4852

4953
# `none(Natural)` is returned when the array is traversed completely
5054
# and no key is matched, or when `arr` is empty.
@@ -58,7 +62,8 @@ func recursiveLinearSearch*[T](arr: openArray[T], key: T, idx: Nat = arr.low.Nat
5862
when isMainModule:
5963
import unittest
6064

61-
template checkLinearSearch[T](arr: openArray[T], key: T, expectedIdx: OptNat): untyped =
65+
template checkLinearSearch[T](arr: openArray[T], key: T,
66+
expectedIdx: Option[Natural]): untyped =
6267
check linearSearch(arr, key) == expectedIdx
6368
check recursiveLinearSearch(arr, key) == expectedIdx
6469

@@ -79,8 +84,8 @@ when isMainModule:
7984
var arr = ['0', 'c', 'a', 'u', '5', '7']
8085
checkLinearSearch(arr, '5', some(Natural(4)))
8186

82-
test "Search in a string array matching with a string matching value":
83-
var arr = ["0", "c", "a", "u", "5", "7"]
87+
test "Search in a string sequence matching with a string matching value":
88+
var arr = @["0", "c", "a", "u", "5", "7"]
8489
checkLinearSearch(arr, "5", some(Natural(4)))
8590

8691
test "Search in an int array with a valid key at the end":

0 commit comments

Comments
 (0)