Skip to content

Instantly share code, notes, and snippets.

@zeitnot
Created September 28, 2018 19:02
Show Gist options
  • Save zeitnot/62e4778a43623d7fc47e284cef8bf355 to your computer and use it in GitHub Desktop.
Save zeitnot/62e4778a43623d7fc47e284cef8bf355 to your computer and use it in GitHub Desktop.
Ruby array partition
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