diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..938aefd 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,39 @@ package io.zipcoder; +import java.util.Map; + public class Problem1 { + + + public String replaceIteration(String expected, Map aMap) { + + String[] stringArray = expected.split(""); + String toReturn = ""; + + for (int i = 0; i < stringArray.length; i++) { + if (aMap.keySet().contains(stringArray[i].toLowerCase())) { + stringArray[i] = aMap.get(stringArray[i].toLowerCase()); + } + toReturn += stringArray[i]; + } + + return toReturn; + } + + + public String replaceRecursion(String input, Map testMap) { + String lower = input.toLowerCase(); + + if (testMap.keySet().size() == 0) { + return input.substring(0,1).toUpperCase() + input.substring(1); + } else { + String toReturn = lower.replaceAll(testMap.keySet().iterator().next(), "\\" + testMap.get(testMap.keySet().iterator().next())); + testMap.remove(testMap.keySet().iterator().next()); + return replaceRecursion(toReturn, testMap); + } + + + } + + } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..fdbc536 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,45 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + public class Problem1Test { + + private Problem1 tester; + + @Before + public void setup() { + tester = new Problem1(); + } + + @Test + public void replaceIterationTest1() { + Map testMap = new HashMap(); + testMap.put("f", "7"); + testMap.put("s", "$"); + testMap.put("1", "!"); + testMap.put("a", "@"); + String input = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = tester.replaceIteration(input, testMap); + Assert.assertEquals(expected, actual); + } + + @Test + public void replaceRecursionTest1() { + Map testMap = new HashMap(); + testMap.put("f", "7"); + testMap.put("s", "$"); + testMap.put("1", "!"); + testMap.put("a", "@"); + String input = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = tester.replaceRecursion(input, testMap); + Assert.assertEquals(expected, actual); + } + }