Skip to content

Instantly share code, notes, and snippets.

@masukomi
Created May 4, 2016 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masukomi/7400e3253c762741ea7a255a8f8f7ebf to your computer and use it in GitHub Desktop.
Save masukomi/7400e3253c762741ea7a255a8f8f7ebf to your computer and use it in GitHub Desktop.
# I need your help geeks. ;)
# I have an array of items
# Some items have a number on both sides
# I'll call these "matched pairs".
# The matched pairs are the keystones of this problem.
# I need to sort the items between the matched pairs
# in a particular way.
# There's a "from number" and a "to number"
# There may be more from -> nil than
# nil-> from. It may be the other way around.
# There may also be NO items
# I have this
# v from number
# v to number
[
[nil, 1],
[1, 2], # a matched pair
[2, nil],
[3, nil],
[4, nil],
[5, nil],
[nil, 3], # note that there are more
[nil, 4], # nil -> to number than
[nil, 5], # from number -> nil
[nil, 6],
[nil, 7],
[6, 8], # another matched pair
[7, 9], # nothing in between
[8, nil],# no nil->to number to pair it with
[9, 10]
]
# i need to transform it to this
[
[nil, 1],
[1, 2],
[2, nil],
[nil, 3],
[3, nil],
[nil, 4],
[4, nil],
[nil, 5],
[5, nil],
[nil, 6],
[nil, 7],
[6, 8],
[7, 9],
[8, nil],
[9, 10]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment