Directions Reduction
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}
. - The OCaml version takes a list of
type direction = | North | South | East | West
.
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.
Similar Kata:
Stats:
Created | Mar 22, 2015 |
Published | Mar 22, 2015 |
Warriors Trained | 217870 |
Total Skips | 44818 |
Total Code Submissions | 549845 |
Total Times Completed | 89526 |
Ruby Completions | 2697 |
JavaScript Completions | 29218 |
Haskell Completions | 1108 |
Python Completions | 32338 |
Java Completions | 7795 |
CoffeeScript Completions | 70 |
C# Completions | 5272 |
Clojure Completions | 289 |
C++ Completions | 3711 |
PHP Completions | 1622 |
Crystal Completions | 28 |
F# Completions | 119 |
Rust Completions | 1200 |
C Completions | 864 |
TypeScript Completions | 1647 |
Swift Completions | 584 |
Shell Completions | 106 |
R Completions | 129 |
Objective-C Completions | 15 |
OCaml Completions | 56 |
Elixir Completions | 189 |
Lua Completions | 160 |
Julia Completions | 68 |
Scala Completions | 368 |
PowerShell Completions | 55 |
Go Completions | 1249 |
Nim Completions | 16 |
Racket Completions | 32 |
Reason Completions | 4 |
Kotlin Completions | 553 |
Prolog Completions | 27 |
Haxe Completions | 9 |
Pascal Completions | 12 |
Perl Completions | 20 |
Elm Completions | 7 |
COBOL Completions | 10 |
D Completions | 6 |
Erlang Completions | 13 |
Total Stars | 5193 |
% of votes with a positive feedback rating | 89% of 8303 |
Total "Very Satisfied" Votes | 6750 |
Total "Somewhat Satisfied" Votes | 1285 |
Total "Not Satisfied" Votes | 268 |