diff options
author | Arne Döring <arne.doering@gmx.net> | 2017-03-21 12:03:41 +0100 |
---|---|---|
committer | Arne Döring <arne.doering@gmx.net> | 2017-07-27 17:40:33 +0200 |
commit | c6417515232b496952e12948cbb787d93084d3eb (patch) | |
tree | a7b31bf1ab8d0c65171896d6d73c5955336545ea /lib | |
parent | c6c3ea599013d178f2df707f0b1fb833ff20b232 (diff) | |
download | Nim-c6417515232b496952e12948cbb787d93084d3eb.tar.gz |
fixes for feedback
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/algorithm.nim | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 189e96593..25d03a356 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -343,7 +343,7 @@ proc prevPermutation*[T](x: var openarray[T]): bool {.discardable.} = swap x[i-1], x[j] result = true - + when isMainModule: # Tests for lowerBound var arr = @[1,2,3,5,6,7,8,9] @@ -373,12 +373,14 @@ when isMainModule: assert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i] -proc rotate*[T](arg: var openarray[T]; first, middle, last: int): int {.discardable.} = +proc rotate*[T](arg: var openarray[T]; first, middle, last: int): int = ## Performs a left rotation on a range of elements. ## Specifically, ``rotate`` swaps the elements in the range [first, last) in such a way ## that the element at index ``middle`` becomes the first element of the sub range and ## ``middle - 1`` becomes the last element of the sub range. The time complexity is ## linear to ``last - first``. returns ``first + (last - middle)``. ``T`` must support swap operation + ## returs ``first + last - middle``, the new index, where the element this was at index ``last`` will + ## be ater the rotation. ## ## ``first`` ## the index of the first element that should be rotated. @@ -388,7 +390,7 @@ proc rotate*[T](arg: var openarray[T]; first, middle, last: int): int {.discarda ## ## ``last`` ## the index of the first element that should not be rotated anymore (exclusive upper bound). - ## + ## ## .. code-block:: nim ## var list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ## list.rotate(1,4,9) @@ -431,15 +433,15 @@ proc rotate*[T](arg: var openarray[T]; first, middle, last: int): int {.discarda elif next == last: next = mMiddle -proc rotate[T](arg: var openarray[T]; middle: int): int {.discardable.} = - ## default arguments for first and last, so that this procedure operates on the entire - ## argument, and not just on a portion of it. +proc rotate*[T](arg: var openarray[T]; middle: int): int = + ## default arguments for first and last, so that this procedure operates on entire + ## ``arg``, and not just on a part of it. arg.rotate(0, middle, arg.len) proc rotated*[T](arg: openarray[T]; first, middle, last: int): seq[T] = ## same as ``rotate``, just with the difference that it does - ## not modify the argument, but writes into a new ``seq`` instead - + ## not modify the argument. It creates a new ``seq`` instead + result = newSeq[T](arg.len) for i in 0 ..< first: @@ -459,14 +461,15 @@ proc rotated*[T](arg: openarray[T]; first, middle, last: int): seq[T] = proc rotated*[T](arg: openarray[T]; middle: int): seq[T] = ## same as ``rotate``, just with the difference that it does - ## not modify the argument, but writes into a new ``seq`` instead + ## not modify the argument. It creates a new ``seq`` instead + arg.rotated(0, middle, arg.len) - + when isMainModule: var list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let list2 = list.rotated(1,4,9) let expected = [0, 4, 5, 6, 7, 8, 1, 2, 3, 9, 10] - + doAssert list.rotate(1,4,9) == 6 doAssert list == expected doAssert list2 == @expected @@ -479,5 +482,3 @@ when isMainModule: doAssert s1 == "xxxcdefgabxxx" doAssert s2.rotate(3, 7, 10) == 6 doassert s2 == "xxxefgabcdxxx" - - |