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);
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末滴铅,一起剝皮案震驚了整個濱河市知押,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌特纤,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件实牡,死亡現(xiàn)場離奇詭異褂萧,居然都是意外死亡,警方通過查閱死者的電腦和手機况毅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尔艇,“玉大人尔许,你說我怎么就攤上這事≈胀蓿” “怎么了味廊?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我毡们,道長迅皇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任衙熔,我火速辦了婚禮,結果婚禮上搅荞,老公的妹妹穿的比我還像新娘红氯。我一直安慰自己,他們只是感情好咕痛,可當我...
    茶點故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布痢甘。 她就那樣靜靜地躺著,像睡著了一般茉贡。 火紅的嫁衣襯著肌膚如雪塞栅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天腔丧,我揣著相機與錄音放椰,去河邊找鬼。 笑死愉粤,一個胖子當著我的面吹牛砾医,可吹牛的內容都是我干的。 我是一名探鬼主播衣厘,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼如蚜,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了影暴?” 一聲冷哼從身側響起错邦,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎型宙,沒想到半個月后撬呢,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡早歇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年倾芝,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箭跳。...
    茶點故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡晨另,死狀恐怖,靈堂內的尸體忽然破棺而出谱姓,到底是詐尸還是另有隱情借尿,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站路翻,受9級特大地震影響狈癞,放射性物質發(fā)生泄漏。R本人自食惡果不足惜茂契,卻給世界環(huán)境...
    茶點故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一蝶桶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧掉冶,春花似錦真竖、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至璧亚,卻和暖如春讨韭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背癣蟋。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工透硝, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人梢薪。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓蹬铺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親秉撇。 傳聞我的和親對象是個殘疾皇子甜攀,可洞房花燭夜當晚...
    茶點故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內容

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