Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/main/java/org/fundacionjala/coding/abel/DirReduction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.fundacionjala.coding.abel;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
* 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.
* 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"]) => []
* 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"].
*/
public final class DirReduction {

/**
* Private constructor.
*/
private DirReduction() {

}

/**
* Reduce the directions.
*
* @param arr Array with directions.
* @return Array with only neccesary directions.
*/
public static String[] dirReduc(String[] arr) {
List<String> actuaList = new LinkedList<>(Arrays.asList(arr));
int i = 0;
while (i < actuaList.size() - 1) {
if (("NORTH".equals(actuaList.get(i)) && "SOUTH".equals(actuaList.get(i + 1)))
|| ("SOUTH".equals(actuaList.get(i)) && "NORTH".equals(actuaList.get(i + 1)))
|| ("EAST".equals(actuaList.get(i)) && "WEST".equals(actuaList.get(i + 1)))
|| ("WEST".equals(actuaList.get(i)) && "EAST".equals(actuaList.get(i + 1)))) {
actuaList.remove(i + 1);
actuaList.remove(i);
if (i > 0) {
i--;
}
} else {
i++;
}
}
return actuaList.toArray(new String[actuaList.size()]);
}
}
50 changes: 50 additions & 0 deletions src/test/java/org/fundacionjala/coding/abel/DirReductionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.fundacionjala.coding.abel;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Test class for Dir Reduction.
*/
public class DirReductionTest {

/**
* Test for Private Constructors.
*
* @throws NoSuchMethodException Thrown when a particular method cannot be found.
* @throws IllegalAccessException Thrown when an application tries to reflectively create an instance.
* @throws InvocationTargetException thrown by an invoked method or constructor.
* @throws InstantiationException Thrown when an application tries to create an instance of a class
* using the {@code newInstance} method in class.
*/
@Test
public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
Constructor<DirReduction> constructor = DirReduction.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}

/**
* Tests provided.
*
* @throws Exception for any exception.
*/
@Test
public void testSimpleDirReduc() throws Exception {
assertEquals("\"NORTH\", \"SOUTH\", \"SOUTH\", \"EAST\", \"WEST\", \"NORTH\", \"WEST\"",
new String[]{"WEST"},
DirReduction.dirReduc(new String[]{"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"}));

assertEquals("\"NORTH\", \"WEST\", \"SOUTH\", \"EAST\"",
new String[]{"NORTH", "WEST", "SOUTH", "EAST"},
DirReduction.dirReduc(new String[]{"NORTH", "WEST", "SOUTH", "EAST"}));
}
}