codewars 8/20

samuel Jo·2023년 8월 20일
0

codewars

목록 보기
45/46

DESCRIPTION:
Once upon a time, on a way through the old wild mountainous 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 right away is a needless effort. Since this is the wild west, with dreadful weather and not much water, it's important to save yourself some energy, otherwise you might die of thirst!

How I crossed a mountainous desert the smart way.
The directions given to the man are, for example, the following (depending on the language):

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"].
or
{ "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" };
or
[North, South, South, East, West, North, West]
You can immediately see that going "NORTH" and immediately "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:

["WEST"]
or
{ "WEST" }
or
[West]
Other examples:
In ["NORTH", "SOUTH", "EAST", "WEST"], the direction "NORTH" + "SOUTH" is going north and coming back right away.

The path becomes ["EAST", "WEST"], now "EAST" and "WEST" annihilate each other, therefore, the final result is [] (nil in Clojure).

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, East, West, South}.
See more examples in "Sample Tests:"
Notes
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"].
if you want to translate, please ask before translating.

function dirReduc(arr) {
    // 빈 배열을 스택으로 활용.
    const stack = [];
    // 방향과 그에 상쇄되는 방향을 매핑한 객체.
    const opposites = { "NORTH": "SOUTH", "SOUTH": "NORTH", "EAST": "WEST", "WEST": "EAST" };
    
    // 주어진 방향 배열을 순회.
    for (const direction of arr) {
        // 스택이 비어있지 않고, 현재 방향과 스택의 맨 위에 있는 방향이 상쇄되는 경우
        if (stack.length > 0 && stack[stack.length - 1] === opposites[direction]) {
            // 스택의 맨 위에 있는 방향을 제거하여 상쇄.
            stack.pop();
        } else {
            // 그렇지 않으면 현재 방향을 스택에 추가.
            stack.push(direction);
        }
    }
    
    // 상쇄되지 않은 방향들로 이루어진 스택을 반환.
    return stack;
}

문제가 장황하지만 결국엔 n s 가 짝이고 e w가 짝이라는 문맥.
이와 비슷한 문제를 풀었는데 codewars에서 풀어본문제 찾아봐야겠다.

비슷한 문제

근래 블로그 작성이 뜸했는데 , 그저 푼문제를 블로그에 쓰는게 맞나 라는 생각이 계속들었고,(현재진행형) 블로그의 방향성을 다시한번 생각해봐야 하는 시기인 것 같아 뜸했다. 공부는 그래도 꾸준히 하는중(+1일 1codewars)
profile
step by step

0개의 댓글