From 1ab0f53d22a1e334ee342621854631ddc0077d39 Mon Sep 17 00:00:00 2001 From: Brian He Date: Fri, 13 Apr 2018 15:20:24 -0400 Subject: [PATCH 1/2] iteration --- src/main/java/io/zipcoder/Problem1.java | 11 ++++++ src/test/java/io/zipcoder/Problem1Test.java | 38 +++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..75ef70c 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,15 @@ package io.zipcoder; +import java.util.HashMap; +import java.util.Set; + public class Problem1 { + + public static String replaceWithIteration(HashMap map, String input) { + Set keys = map.keySet(); + for(String key : keys) { + input = input.replaceAll(key, "\\" + map.get(key)); + } + return input; + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..78d63e3 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,42 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; + public class Problem1Test { + + @Test + public void replaceTest() { + + HashMap map = new HashMap<>(); + map.put("f", "7"); + map.put("s", "$"); + map.put("1", "!"); + map.put("a", "@"); + + 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.replaceWithIteration(map, input); + + Assert.assertEquals(expected, actual); + } + + @Test + public void replaceTest2() { + HashMap map = new HashMap<>(); + map.put("f", "7"); + map.put("s", "$"); + map.put("1", "!"); + map.put("a", "@"); + + String input = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; + String expected = "f"; + + String actual = Problem1.replaceWithIteration(map, input); + + Assert.assertEquals(expected, actual); + } } From 4e1eb95e071277c378b1ad6b2cf5a74f66a6bade Mon Sep 17 00:00:00 2001 From: Brian He Date: Fri, 13 Apr 2018 15:41:17 -0400 Subject: [PATCH 2/2] recursion done too --- src/main/java/io/zipcoder/Problem1.java | 17 +++++++++++++++-- src/test/java/io/zipcoder/Problem1Test.java | 5 +++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 75ef70c..1e5e83e 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,7 +1,6 @@ package io.zipcoder; -import java.util.HashMap; -import java.util.Set; +import java.util.*; public class Problem1 { @@ -12,4 +11,18 @@ public static String replaceWithIteration(HashMap map, String in } return input; } + + public static String replaceWithRecursion(HashMap map, String input) { + Set keys = map.keySet(); + + if(keys.isEmpty()) { + return input; + } else { + String s = keys.iterator().next(); + input = input.replaceAll(s, "\\" + map.get(s)); + map.remove(s); + } + + return replaceWithRecursion(map, input); + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index 78d63e3..a42b3c5 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -33,9 +33,10 @@ public void replaceTest2() { map.put("a", "@"); String input = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; - String expected = "f"; + String expected = "the 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; - String actual = Problem1.replaceWithIteration(map, input); + + String actual = Problem1.replaceWithRecursion(map, input); Assert.assertEquals(expected, actual); }