Created
April 11, 2017 18:34
-
-
Save bjhaid/66c12c43ad2747c5a0c434829b3b398b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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