From cf2bd144a1c2742db409ae0786369481a75a458c Mon Sep 17 00:00:00 2001 From: Abel Barrientos Date: Wed, 23 Aug 2017 08:53:52 -0400 Subject: [PATCH 1/2] Solving the kata --- .../fundacionjala/coding/abel/TenMinWalk.java | 53 +++++++++++++++++++ .../coding/abel/TenMinuteWalkTest.java | 49 +++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/abel/TenMinWalk.java create mode 100644 src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java diff --git a/src/main/java/org/fundacionjala/coding/abel/TenMinWalk.java b/src/main/java/org/fundacionjala/coding/abel/TenMinWalk.java new file mode 100644 index 0000000..11752c5 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/TenMinWalk.java @@ -0,0 +1,53 @@ +package org.fundacionjala.coding.abel; + +/** + * CYou live in the city of Cartesia where all roads are laid out in a perfect grid. + * You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. + * The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button + * it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). + * You know it takes you one minute to traverse one city block, so create a function that will return true if + * the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, + * of course, return you to your starting point. Return false otherwise. + */ +public final class TenMinWalk { + + /** + * Private Constructor. + */ + private TenMinWalk() { + + } + + /** + * Returns if the Walk is valid. + * + * @param walk char[]. + * @return boolean. + */ + public static boolean isValid(char[] walk) { + if (walk.length != 10) { + return false; + } + int y = 0; + int x = 0; + for (char move : walk) { + switch (move) { + case 'n': + y++; + break; + case 's': + y--; + break; + case 'w': + x++; + break; + case 'e': + x--; + break; + default: + break; + } + } + return y == 0 && x == 0; + } +} diff --git a/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java b/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java new file mode 100644 index 0000000..42ee4e8 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java @@ -0,0 +1,49 @@ +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; + +/** + * This class test the Ten Minute Walk class. + */ +public class TenMinuteWalkTest { + + /** + * 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 = TenMinWalk.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * All provided tests. + */ + @Test + public void test() { + assertEquals("Should return true", true, + TenMinWalk.isValid(new char[]{'n', 's', 'n', 's', 'n', 's', 'n', 's', 'n', 's'})); + assertEquals("Should return false", false, + TenMinWalk.isValid(new char[]{'w', 'e', 'w', 'e', 'w', 'e', 'w', 'e', 'w', 'e', 'w', 'e'})); + assertEquals("Should return false", false, + TenMinWalk.isValid(new char[]{'w'})); + assertEquals("Should return false", false, + TenMinWalk.isValid(new char[]{'n', 'n', 'n', 's', 'n', 's', 'n', 's', 'n', 's'})); + } +} From a5c598a2eb1c01637e5efb4f1f218d8e9a36ee0a Mon Sep 17 00:00:00 2001 From: Abel Barrientos Date: Wed, 23 Aug 2017 10:27:19 -0400 Subject: [PATCH 2/2] Improving coverage --- .../java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java b/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java index 42ee4e8..5e137c8 100644 --- a/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java +++ b/src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java @@ -45,5 +45,7 @@ public void test() { TenMinWalk.isValid(new char[]{'w'})); assertEquals("Should return false", false, TenMinWalk.isValid(new char[]{'n', 'n', 'n', 's', 'n', 's', 'n', 's', 'n', 's'})); + assertEquals("Should return false", false, + TenMinWalk.isValid(new char[]{'w', 'w', 'w', 'e', 'e', 'e', 'w', 'w', 'w', 'e'})); } }