From 09ba2494548aa66eb215c975e70ace0447b36569 Mon Sep 17 00:00:00 2001 From: katherine Date: Mon, 27 Nov 2017 10:03:39 -0500 Subject: [PATCH] finished --- src/main/java/io/zipcoder/Problem1.java | 45 +++++++++++++++++++++ src/test/java/io/zipcoder/Problem1Test.java | 23 +++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..b759e2e 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,49 @@ package io.zipcoder; +import java.util.HashMap; + public class Problem1 { + + private HashMap replaceChars = new HashMap(); + + public Problem1() { + replaceChars.put('f', '7'); + replaceChars.put('s', '$'); + replaceChars.put('1', '!'); + replaceChars.put('a', '@'); + } + + public String replaceCharsByIteration(String input) { + char[] allChars = input.toCharArray(); + for(int i = 0; i < allChars.length; i++) { + char c = allChars[i]; + if(replaceChars.containsKey(Character.toLowerCase(c))) { + allChars[i] = replaceChars.get(Character.toLowerCase(c)); + } + } + + return new String(allChars); + } + + public String replaceCharsByRecursion(String input) { + char[] allChars = input.toCharArray(); + for(char c : replaceChars.keySet()) { + int index = input.indexOf(Character.toLowerCase(c)); + if(index > -1) { + allChars[index] = replaceChars.get(c); + input = new String(allChars); + replaceCharsByRecursion(input); + } + else { + index = input.indexOf(Character.toUpperCase(c)); + if(index > -1) { + allChars[index] = replaceChars.get(c); + input = new String(allChars); + replaceCharsByRecursion(input); + } + } + } + return input; + } + } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..1d80e2f 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,27 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem1Test { + + @Test + public void replaceCharsByIterationTest() { + Problem1 problem1 = new Problem1(); + 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 = problem1.replaceCharsByIteration(input); + Assert.assertEquals(expected, actual); + } + + @Test + public void replaceCharsByRecursionTest() { + Problem1 problem1 = new Problem1(); + 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 = problem1.replaceCharsByRecursion(input); + Assert.assertEquals(expected, actual); + } }