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
53 changes: 53 additions & 0 deletions src/main/java/org/fundacionjala/coding/abel/TenMinWalk.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
51 changes: 51 additions & 0 deletions src/test/java/org/fundacionjala/coding/abel/TenMinuteWalkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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<TenMinWalk> 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'}));
assertEquals("Should return false", false,
TenMinWalk.isValid(new char[]{'w', 'w', 'w', 'e', 'e', 'e', 'w', 'w', 'w', 'e'}));
}
}