diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..24d2a09 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,64 @@ package io.zipcoder; +import java.util.HashMap; +import java.util.Map; + public class Problem1 { + + private Map map; + + public Problem1() { + map = new HashMap(); + initializeMap(); + } + + public Map getMap() { + return map; + } + + public void initializeMap() { + map.put('f', '7'); + map.put('s', '$'); + map.put('1', '!'); + map.put('a', '@'); + } + + public String replaceCharsWithIteration(String originalString) { + char[] charArray = originalString.toCharArray(); + for (int i = 0; i < charArray.length; i++) { + char c = Character.toLowerCase(charArray[i]); + if (map.containsKey(c)) { + charArray[i] = map.get(c); + } + } + return new String(charArray); + } + + public String replaceCharsWithRecursion(String originalString, int index) { + char[] charArray = originalString.toCharArray(); + char c = Character.toLowerCase(charArray[index]); + if (map.containsKey(c)) { + charArray[index] = map.get(c); + } + if (index < originalString.length() - 1) { + index++; + return replaceCharsWithRecursion(new String(charArray), index); + } + return new String(charArray); + } + + public static void main(String[] args) { + Problem1 problem1 = new Problem1(); + + String input = "The Farmer went to the store to get 1 dollar\'s worth of fertilizer"; + String output = "The 7@rmer went to the $tore to get ! doll@r\'$ worth o7 7ertilizer"; + + String test = problem1.replaceCharsWithRecursion(input, 0); + + System.out.println(input); + System.out.println(output); + System.out.println(test); + } + + } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..1e6518d 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,51 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + public class Problem1Test { + + private Problem1 problem1; + private String input = "The Farmer went to the store to get 1 dollar\'s worth of fertilizer"; + private String output = "The 7@rmer went to the $tore to get ! doll@r\'$ worth o7 7ertilizer"; + + @Before + public void setup() { + problem1 = new Problem1(); + } + + @Test + public void initializeMapTest() { + // Given + char key = 'f'; + char expectedValue = '7'; + // When + char actualValue = problem1.getMap().get('f'); + // Then + Assert.assertEquals(expectedValue, actualValue); + } + + @Test + public void replaceCharsWithIterationTest() { + // Given + String expectedOutput = output; + // When + String actualOutput = problem1.replaceCharsWithIteration(input); + // Then + Assert.assertEquals(expectedOutput, actualOutput); + } + + @Test + public void replaceCharsWithRecursionTest() { + // Given + int startingIndex = 0; + String expectedOutput = output; + // When + String actualOutput = problem1.replaceCharsWithRecursion(input, startingIndex); + // Then + Assert.assertEquals(expectedOutput, actualOutput); + } + + }