Directions Reduction
Last updated
Was this helpful?
Last updated
Was this helpful?
Once upon a time, on a way through the old wild west,β¦
β¦ a man was given directions to go from one point to another. The directions were "NORTH", "SOUTH", "WEST", "EAST". Clearly "NORTH" and "SOUTH" are opposite, "WEST" and "EAST" too. Going to one direction and coming back the opposite direction is a needless effort. Since this is the wild west, with dreadfull weather and not much water, it's important to save yourself some energy, otherwise you might die of thirst!
How I crossed the desert the smart way.
The directions given to the man are, for example, the following:
You can immediatly see that going "NORTH" and then "SOUTH" is not reasonable, better stay to the same place! So the task is to give to the man a simplified version of the plan. A better plan in this case is simply:
Other examples:
In["NORTH", "SOUTH", "EAST", "WEST"]
, the direction "NORTH" + "SOUTH"
is going north and coming back right away. What a waste of time! Better to do nothing.
The path becomes ["EAST", "WEST"]
, now "EAST"
and "WEST"
annihilate each other, therefore, the final result is [] .
In ["NORTH", "EAST", "WEST", "SOUTH", "WEST", "WEST"], "NORTH" and "SOUTH" are not directly opposite but they become directly opposite after the reduction of "EAST" and "WEST" so the whole path is reducible to ["WEST", "WEST"].
Task
Write a function dirReduc
which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).
The Haskell version takes a list of directions with data Direction = North | East | West | South
. The Clojure version returns nil when the path is reduced to nothing. The Rust version takes a slice of enum Direction {NORTH, SOUTH, EAST, WEST}
.
Examples
Note
Not all paths can be made simpler. The path ["NORTH", "WEST", "SOUTH", "EAST"] is not reducible. "NORTH" and "WEST", "WEST" and "SOUTH", "SOUTH" and "EAST" are not directly opposite of each other and can't become such. Hence the result path is itself : ["NORTH", "WEST", "SOUTH", "EAST"].
λ¬Έμ μ€λͺ μ΄ λ§€μ° μΉμ νλ€.
λ°°μ΄μ μ νλλ° "WEST" <-> "EAST"
, NORTH<->SOUTH
κ° κ²ΉμΉλ©΄ λ μμλ₯Ό μ κ±°νλ€. κ·Έλ¦¬κ³ λ€μ μ²μλΆν° 체ν¬ν΄μ μ κ±°νκ³ λ€μ μ²μλΆν° 체ν¬ν΄μ λ μ κ±°νλ€.
κ·Έλ¦¬κ³ μ΅μ’ λ¨μ μμλ₯Ό return νλ€.
λμλ¨λΆμ λ΄μ λ°°μ΄μ λ§λ λ€.
λ°°μ΄ μμ νλνλμ μ κ·Όνλ€.
λ°°μ΄μμμ λμλ¨λΆμ λ΄μ λ°°μ΄κ³Ό λ§€μΉν΄ κ·Έ λ€μ μμλ‘ μ€λ©΄ μλλκ² μλ μ§ νμΈνλ€.
μλ€λ©΄ νμ¬ μμμ λ€μ μμλ₯Ό μμ νλ€.
λ€μ 2λ‘ λμκ°λ€.
κ²ΉμΉλ μμκ° μλ€λ©΄ return νλ€.
random ν μ€νΈ ν΅κ³Ό
λ³μλͺ μ΄ λ§μμ λ λ€. opposite elseλ νμ€λ‘ μΈ μ μꡬλ.
opposite κ°μ²΄λ‘ λ§λ κ² ν¨μ¬ κΉλνκ³ κ°λ μ±μ΄ μ’λ€.
pop()
μ λ°°μ΄μμ λ§μ§λ§ μμλ₯Ό μ κ±° νλ€.
λ΄ μ½λμ λ¨μ μ
νμΈν μμλ₯Ό λ€μ μ²μλΆν° νμΈν΄μΌ νλ€.
μ¬κ·ν¨μλ₯Ό μ΄μ©νκΈ° λλ¬Έμ μλ‘μ΄ λ°°μ΄μ λ΄μ μ μλ€.
λ₯Ό λ¨μ μΌλ‘ κ°μ‘λλ° μ΄ solutionμμ μ΄λ¬ν μ μ΄ λ³΄μλμ΄μλ€.
λ°°μ΄μ κ²μνλ μΌμ reduceλ₯Ό μ¬μ©ν΄ μλ‘μ΄ λ°°μ΄μ λ§λ€μλ€.
λ§μ½ μ΄λ°μμκ° μλ€λ©΄,
μ΄μ μμκ° μκΈ° λλ¬Έμ 맨μ²μ dirsμλ "NORTH"κ° λ΄κΈ΄λ€.
dirsμ λ§μ§λ§ μμμ λ°°μ΄μ νμ¬μμμ λΉκ΅
"NORTH" === opposite["SOUTH"] λ κ°λ€
dirsμμ "NORTH"λ μ κ±° λλ€.
μ΄μ μμκ° μκΈ° λλ¬Έμ "SOUTH"κ° λ΄κΈ΄λ€.
"SOUTH" === opposite["EAST"] λ κ°μ§ μκΈ° λλ¬Έμ "EAST"λ dirsμ λ΄κΈ΄λ€.
"EAST" === opposite["WEST"] μ κ°κΈ° λλ¬Έμ dirsμμ "EAST"λ μ κ±° λλ€.
dirsλ°°μ΄μ "SOUTH"λ§ λ¨λλ€.
"SOUTH" === opposite["NORTH"] κ°κΈ° λλ¬Έμ "SOUTH" μ κ±° λλ€.
λΉ λ°°μ΄λ§ return νλ€.
λ΄κ° μ§ μ½λλ νμ¬μμμ λ€μμμλ§μ λΉκ΅νμ¬ μ κ±°νκΈ° λλ¬Έμ κ²μκ° μλ²½νλ€κ³ ν μ μλ€. μ΄μ μμκ° μλ‘μ΄ νκ²½μ΄ λ μ μλ κ°λ₯μ±μ μ²μλΆν° λ€μ λ°°μ΄μ κ²μ νλ€.
νμ§λ§ μ΄ μ½λλ μ΄μ μμμ νμ¬ μμλ₯Ό λΉκ΅νμ¬ μ΄μ μμλ₯Ό μμ νκΈ° λλ¬Έμ μ΄μ μμκ° μλ‘μ΄ νκ²½μ λ°λ₯Έ μν©μ ν¨μ¬ λμνκΈ° νΈν μ½λμ΄λ€.
, , , , , 's Solution