diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..eda2063 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,29 @@ package io.zipcoder; +import java.util.HashMap; + public class Problem1 { + + public String iterationMethod(String string, HashMap swapCharacter) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < string.length(); i++){ + Character current = string.charAt(i); + if (swapCharacter.containsKey(Character.toLowerCase(current))) { + current = swapCharacter.get(Character.toLowerCase(current)); + } + stringBuilder.append(current); + } + return stringBuilder.toString(); + } + + public String recursionMethod(String string, HashMap swapCharacter){ + if(string.length() == 0) { + return ""; + } + Character current = string.charAt(0); + if(swapCharacter.containsKey(Character.toLowerCase(current))){ + current = swapCharacter.get(Character.toLowerCase(current)); + } + return current + recursionMethod(string.substring(1), swapCharacter); + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..1712cef 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,39 @@ package io.zipcoder; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assert; + +import java.util.HashMap; + public class Problem1Test { + + Problem1 problem1; + HashMap swapCharacter; + String input; + + @Before + public void setUp(){ + swapCharacter = new HashMap(); + swapCharacter.put('f', '7'); + swapCharacter.put('s', '$'); + swapCharacter.put('1', '!'); + swapCharacter.put('a', '@'); + problem1 = new Problem1(); + input = "The Farmer went to the store to get 1 dollar's worth of fertilizer"; + } + + @Test + public void iterationMethod(){ + String expected = "The 7@rmer went to the $tore to get ! doll@r'$ worth o7 7ertilizer"; + String actual = problem1.iterationMethod(input, swapCharacter); + Assert.assertEquals(expected,actual); + } + + @Test + public void recursionMethod(){ + String expected = "The 7@rmer went to the $tore to get ! doll@r'$ worth o7 7ertilizer"; + String actual = problem1.recursionMethod(input, swapCharacter); + Assert.assertEquals(expected,actual); + } }