From 8856c1de256a009c5547e729550de67a34c84fd1 Mon Sep 17 00:00:00 2001 From: andrea Date: Mon, 27 Nov 2017 10:05:58 -0500 Subject: [PATCH 1/2] ugh, realized halfway through to use maps --- src/main/java/io/zipcoder/Main.java | 8 ++++++++ src/main/java/io/zipcoder/Problem1.java | 17 +++++++++++++++++ src/test/java/io/zipcoder/Problem1Test.java | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/main/java/io/zipcoder/Main.java diff --git a/src/main/java/io/zipcoder/Main.java b/src/main/java/io/zipcoder/Main.java new file mode 100644 index 0000000..070269b --- /dev/null +++ b/src/main/java/io/zipcoder/Main.java @@ -0,0 +1,8 @@ +package io.zipcoder; + +public class Main { + + public static void main(String[] args){ + + } +} diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..b341fef 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,21 @@ package io.zipcoder; +import java.util.HashMap; +import java.util.Map; + public class Problem1 { + + String = "The Farmer went to the store to get 1 dollar's worth of fertilizer"; + + public static String iterationMethod(){ + Map newMap = new HashMap(); + for(Map.Entry entry : map.entrySet()) + newMap.put(entry.getKey().replace(), entry.getValue()); + map = newMap; + + } + + public static String recursionMethod(){ + + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..df494e5 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,23 @@ package io.zipcoder; +import org.junit.Test; +import org.junit.Assert; + public class Problem1Test { + + Problem1 problem1 = new Problem1(); + + @Test + public void iterationMethod(){ + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth of 7ertilizer"; + //String actual = problem1. + Assert.assertEquals(expected,actual); + } + + @Test + public void recursionMethod(){ + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth of 7ertilizer"; + //String actual = problem1. + Assert.assertEquals(expected,actual); + } } From cfde0950fdd0c5e17fc338d7af5c9835a1afd742 Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 2 Dec 2017 14:32:43 -0500 Subject: [PATCH 2/2] re-did problem --- src/main/java/io/zipcoder/Main.java | 8 ------ src/main/java/io/zipcoder/Problem1.java | 30 +++++++++++++-------- src/test/java/io/zipcoder/Problem1Test.java | 26 ++++++++++++++---- 3 files changed, 40 insertions(+), 24 deletions(-) delete mode 100644 src/main/java/io/zipcoder/Main.java diff --git a/src/main/java/io/zipcoder/Main.java b/src/main/java/io/zipcoder/Main.java deleted file mode 100644 index 070269b..0000000 --- a/src/main/java/io/zipcoder/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.zipcoder; - -public class Main { - - public static void main(String[] args){ - - } -} diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index b341fef..eda2063 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,21 +1,29 @@ package io.zipcoder; import java.util.HashMap; -import java.util.Map; public class Problem1 { - String = "The Farmer went to the store to get 1 dollar's worth of fertilizer"; - - public static String iterationMethod(){ - Map newMap = new HashMap(); - for(Map.Entry entry : map.entrySet()) - newMap.put(entry.getKey().replace(), entry.getValue()); - map = newMap; - + 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 static String recursionMethod(){ - + 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 df494e5..1712cef 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,23 +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 = new Problem1(); + 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 of 7ertilizer"; - //String actual = problem1. + 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 of 7ertilizer"; - //String actual = problem1. + 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); } }