diff --git a/src/main/java/org/fundacionjala/coding/abel/DirReduction.java b/src/main/java/org/fundacionjala/coding/abel/DirReduction.java new file mode 100644 index 0000000..7244e74 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/DirReduction.java @@ -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 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()]); + } +} diff --git a/src/test/java/org/fundacionjala/coding/abel/DirReductionTest.java b/src/test/java/org/fundacionjala/coding/abel/DirReductionTest.java new file mode 100644 index 0000000..c3b8b2e --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/DirReductionTest.java @@ -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 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"})); + } +}