diff --git a/pom.xml b/pom.xml index 2a6372d..746d4ec 100644 --- a/pom.xml +++ b/pom.xml @@ -18,4 +18,9 @@ + + + 1.8 + 1.8 + \ 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..4cbf939 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,34 @@ package io.zipcoder; +import java.util.HashMap; +import java.util.Set; + public class Problem1 { + + String encodeIterate (String raw, HashMap cypher){ + Set keyChars = cypher.keySet(); + for (Character keyChar : + keyChars) { + raw = raw.replace(keyChar, cypher.get(keyChar)); + } + return raw; + } + + public String encodeRecursive (String raw, HashMap cypher){ + return encodeRecursive(raw, cypher, 0); + + } + + private String encodeRecursive (String raw, HashMap cypher, int index){ + Character keyChar = raw.charAt(index); + if(cypher.containsKey(keyChar)) { + raw = raw.replace(keyChar, cypher.get(keyChar)); + } + if(index == raw.length()-1) { + return raw; + } else { + index++; + return encodeRecursive(raw, cypher, index); + } + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..abfa7b1 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,38 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; + public class Problem1Test { + + private HashMap cypher = new HashMap(){{ + put('f', '7'); + put('s', '$'); + put('1', '!'); + put('a', '@'); + }}; + + @Test + public void encodeIterateTest(){ + Problem1 problem1 = new Problem1(); + String raw = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + + String expected = "The F@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = problem1.encodeIterate(raw, cypher); + + Assert.assertEquals(expected, actual); + } + + @Test + public void encodeRecursiveTest(){ + Problem1 problem1 = new Problem1(); + String raw = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + + String expected = "The F@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = problem1.encodeRecursive(raw, cypher); + + Assert.assertEquals(expected, actual); + } }