Created
September 28, 2018 19:02
-
-
Save zeitnot/62e4778a43623d7fc47e284cef8bf355 to your computer and use it in GitHub Desktop.
Ruby array partition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def partition(arr, int) | |
list = [[],[]] | |
return list if arr.empty? | |
if int == 0 | |
list[1] = arr | |
return list | |
end | |
if int.negative? | |
left = arr.length - int.abs | |
left = 0 if left.negative? | |
right = int.abs | |
else | |
right = arr.length - int | |
right = 0 if right.negative? | |
left = int | |
end | |
list[0] = arr.first(left) | |
list[1] = arr.last(right) | |
list | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment