Directions Reduction

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:

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"].

or

{ "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" };

or (haskell)

[North, South, South, East, West, North, West]

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:

["WEST"]

or

{ "WEST" }

or (haskell)

[West]

or (rust)

[WEST];

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 [] (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, SOUTH, EAST, WEST}.
Examples

dirReduc(@[@"NORTH", @"SOUTH", @"SOUTH", @"EAST", @"WEST", @"NORTH", @"WEST"]); // => @[@"WEST"]
dirReduc(@[@"NORTH", @"SOUTH", @"SOUTH", @"EAST", @"WEST", @"NORTH"]); // => @[]

See more examples in "Example Tests"
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"].

Good Solution1:

public class DirReduction {
public static String[] dirReduc(String[] arr) {
        
        
        String strDirc = "";
        
        for(int i = 0; i<arr.length; i++){
            strDirc = strDirc+","+arr[i];
            strDirc = strDirc.replace(",EAST,WEST", "")
            .replace(",WEST,EAST", "")
            .replace(",SOUTH,NORTH", "")
            .replace(",NORTH,SOUTH", "");
            
        }
        
        return strDirc.length()==0?new String[]{} : strDirc.substring(1).split(",");
        
    }
}

Good Solution2:

import java.util.Stack;

public class DirReduction {
    public static String[] dirReduc(String[] arr) {
        final Stack<String> stack = new Stack<>();

        for (final String direction : arr) {
            final String lastElement = stack.size() > 0 ? stack.lastElement() : null;

            switch(direction) {
                case "NORTH": if ("SOUTH".equals(lastElement)) { stack.pop(); } else { stack.push(direction); } break;
                case "SOUTH": if ("NORTH".equals(lastElement)) { stack.pop(); } else { stack.push(direction); } break;
                case "EAST":  if ("WEST".equals(lastElement)) { stack.pop(); } else { stack.push(direction); } break;
                case "WEST":  if ("EAST".equals(lastElement)) { stack.pop(); } else { stack.push(direction); } break;
            }
        }
        return stack.stream().toArray(String[]::new);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 10,998評論 0 23
  • 產(chǎn)后四周,是新媽咪恢復(fù)身體的關(guān)鍵時(shí)期,如果這個(gè)時(shí)期疏忽大意了,很容易落下病根的哦!為您推薦產(chǎn)后四周養(yǎng)身關(guān)鍵字:排、...
    ded5c8b65735閱讀 573評論 0 1
  • 如果 我們對生活有一百個(gè)抱怨 那么 就會(huì)有一萬個(gè)去愛的理由 你的樣子 就是世界的樣子~~傾城
    傾城_ab99閱讀 504評論 0 0
  • 別離匆匆,別離匆匆,莫道傷情。莫道傷情,滴滴淚落,輕輕嗚咽,盡往去角落里罷;微微目赤,顫顫唇啟,也盡皆無語凝噎,...
    彳云閱讀 363評論 0 0
  • 南方姑娘 你是否習(xí)慣北方的秋涼 南方姑娘 你是否喜歡北方人的直爽 日子過的就像那些不眠的晚上 她嚼著口香糖對墻滿談...
    souvenirso閱讀 371評論 2 3

友情鏈接更多精彩內(nèi)容