Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Created April 11, 2017 18:34
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 bjhaid/66c12c43ad2747c5a0c434829b3b398b to your computer and use it in GitHub Desktop.
Save bjhaid/66c12c43ad2747c5a0c434829b3b398b to your computer and use it in GitHub Desktop.
HASH = {
"NORTH" => "SOUTH",
"EAST" => "WEST",
"SOUTH" => "NORTH",
"WEST" => "EAST",
}
def dirReduc(arr)
res = []
idx = 0
while idx < arr.size
if HASH[arr[idx]] == res.last
res.pop
else
res.push(arr[idx])
end
idx += 1
end
res
end
p dirReduc(["NORTH", "SOUTH", "EAST", "WEST"])
p dirReduc( ["NORTH", "EAST", "WEST", "SOUTH", "WEST", "WEST"])
p dirReduc( ["NORTH", "WEST", "SOUTH", "EAST"])
p dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"])
p dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment