fixes for feedback
This commit is contained in:
parent
c6c3ea5990
commit
c641751523
1 changed files with 14 additions and 13 deletions
|
|
@ -373,12 +373,14 @@ when isMainModule:
|
||||||
assert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i]
|
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.
|
## Performs a left rotation on a range of elements.
|
||||||
## Specifically, ``rotate`` swaps the elements in the range [first, last) in such a way
|
## 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
|
## 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
|
## ``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
|
## 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``
|
## ``first``
|
||||||
## the index of the first element that should be rotated.
|
## the index of the first element that should be rotated.
|
||||||
|
|
@ -431,14 +433,14 @@ proc rotate*[T](arg: var openarray[T]; first, middle, last: int): int {.discarda
|
||||||
elif next == last:
|
elif next == last:
|
||||||
next = mMiddle
|
next = mMiddle
|
||||||
|
|
||||||
proc rotate[T](arg: var openarray[T]; middle: int): int {.discardable.} =
|
proc rotate*[T](arg: var openarray[T]; middle: int): int =
|
||||||
## default arguments for first and last, so that this procedure operates on the entire
|
## default arguments for first and last, so that this procedure operates on entire
|
||||||
## argument, and not just on a portion of it.
|
## ``arg``, and not just on a part of it.
|
||||||
arg.rotate(0, middle, arg.len)
|
arg.rotate(0, middle, arg.len)
|
||||||
|
|
||||||
proc rotated*[T](arg: openarray[T]; first, middle, last: int): seq[T] =
|
proc rotated*[T](arg: openarray[T]; first, middle, last: int): seq[T] =
|
||||||
## same as ``rotate``, just with the difference that it does
|
## 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)
|
result = newSeq[T](arg.len)
|
||||||
|
|
||||||
|
|
@ -459,7 +461,8 @@ proc rotated*[T](arg: openarray[T]; first, middle, last: int): seq[T] =
|
||||||
|
|
||||||
proc rotated*[T](arg: openarray[T]; middle: int): seq[T] =
|
proc rotated*[T](arg: openarray[T]; middle: int): seq[T] =
|
||||||
## same as ``rotate``, just with the difference that it does
|
## 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)
|
arg.rotated(0, middle, arg.len)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
@ -479,5 +482,3 @@ when isMainModule:
|
||||||
doAssert s1 == "xxxcdefgabxxx"
|
doAssert s1 == "xxxcdefgabxxx"
|
||||||
doAssert s2.rotate(3, 7, 10) == 6
|
doAssert s2.rotate(3, 7, 10) == 6
|
||||||
doassert s2 == "xxxefgabcdxxx"
|
doassert s2 == "xxxefgabcdxxx"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue