From 38ebe8e320f4db9d11e735efae7726e60dd86661 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 15 Apr 2018 13:56:48 -0400 Subject: [PATCH] prob1 done/with passing tests --- .idea/vcs.xml | 6 --- src/main/java/io/zipcoder/Problem1.java | 41 +++++++++++++++++++++ src/test/java/io/zipcoder/Problem1Test.java | 39 ++++++++++++++++++++ 3 files changed, 80 insertions(+), 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..f85a353 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,45 @@ package io.zipcoder; +import java.util.*; + public class Problem1 { + + /** + * Replace certain characters from a string using iteration + * + * @param inputString string to be altered + * @param swapMap contains char represented by key and it's corresponding replacement value + * @return a String with any character represented by a key in the map replaced with its corresponding value. + */ + public String withIteration(String inputString, Map swapMap) { + String outPutString = ""; + for (HashMap.Entry val : swapMap.entrySet()) { + String pattern = "(?i)" + val.getKey(); + //http://cephas.net/blog/2006/02/09/javalangillegalargumentexception-illegal-group-reference-replaceall-and-dollar-signs/ + String replace = "\\" + val.getValue(); //some of the replacements were regular expression operators, if escape them will be fine + outPutString = inputString.replaceAll(pattern, replace); + inputString = outPutString; + } + return outPutString; + } + + /** + * Replace certain characters from a string using recursion + * + * @param inputString string to be altered + * @param swapMap contains char represented by key and it's corresponding replacement value + * @return a String with any character represented by a key in the map replaced with its corresponding value. + */ + public String withRecursion(String inputString, Map swapMap) { + //law of recursion have a condition to stop, empty string gets appended known as non-function + if(inputString.length() == 0){ + return ""; + } + String current = inputString.substring(0,1); + if (swapMap.containsKey(current.toLowerCase())) { + current = swapMap.get(current.toLowerCase()); + } + return current + withRecursion(inputString.substring(1),swapMap); + } } + diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..6779084 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,43 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; + public class Problem1Test { + + private static Problem1 testProb; + + private Map testMap; + private String inputString; + + @Before + public void setUp() { + testMap = new HashMap(); + testMap.put("f", "7"); + testMap.put("s", "$"); + testMap.put("1", "!"); + testMap.put("a", "@"); + testProb = new Problem1(); + inputString = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + } + + @Test + public void testWithIteration() { + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = testProb.withIteration(inputString, testMap); + Assert.assertEquals(expected, actual); + } + + @Test + public void testWithRecursion() { + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = testProb.withRecursion(inputString, testMap); + Assert.assertEquals(expected, actual); + } + } + +