From cebc092a809f0e21371b99e17d6b599325b0c62e Mon Sep 17 00:00:00 2001 From: Zachary Date: Mon, 27 Nov 2017 10:03:56 -0500 Subject: [PATCH 1/2] saving --- src/main/java/io/zipcoder/Problem1.java | 47 +++++++++++++++++++++ src/test/java/io/zipcoder/Problem1Test.java | 15 +++++++ 2 files changed, 62 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..9228100 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,51 @@ package io.zipcoder; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +// Given the following map { ‘f’ : ‘7’, ‘s’:’$’, ‘1’:’!’, ‘a’.:’@’}, +// your method should replace any character represented by a key in the map, with its corresponding value. +// +// Input = “The Farmer went to the store to get 1 dollar’s worth of fertilizer” +// Output = “The 7@rmer went to the $tore to get ! doll@r’$ worth of 7ertilizer” public class Problem1 { + + + + public static String replaceCharsInString(String input) { + HashMap map = populateMap(); + ArrayList charArrary = getArray(input); + + for (char c: charArrary) { + if(charArrary.contains(map.get('f'))); + + } + + + +// input.replace('f','7'); +// input.replace('s','$'); //ignored + return input; + } + + public static HashMap populateMap(){ + HashMap map = new HashMap(); + + map.put('f','7'); + map.put('s','$'); + map.put('1','!'); + map.put('a','@'); + return map; + } + + public static ArrayList getArray(String input){ + ArrayList charArrary = new ArrayList (); + for (char c: input.toCharArray()) { + charArrary.add(c); + } + return charArrary; + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..9751da1 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,19 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem1Test { + + + + @Test + public void Test(){ + String input = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + + String expected = ""; + String actual = Problem1.replaceCharsInString(input); + + Assert.assertEquals(expected,actual); + } } From 798f9effd8e8cb496a68645cb85fa01f2b01762e Mon Sep 17 00:00:00 2001 From: Zachary Date: Fri, 1 Dec 2017 07:34:50 -0500 Subject: [PATCH 2/2] with loop and map --- src/main/java/io/zipcoder/Problem1.java | 40 ++++++--------------- src/test/java/io/zipcoder/Problem1Test.java | 23 ++++++++++-- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 9228100..cd96273 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,9 +1,6 @@ package io.zipcoder; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; import java.util.Map; // Given the following map { ‘f’ : ‘7’, ‘s’:’$’, ‘1’:’!’, ‘a’.:’@’}, @@ -15,37 +12,20 @@ public class Problem1 { - public static String replaceCharsInString(String input) { - HashMap map = populateMap(); - ArrayList charArrary = getArray(input); + public static String replaceCharsInString(String input,Map map) { + StringBuilder builder = new StringBuilder(); - for (char c: charArrary) { - if(charArrary.contains(map.get('f'))); + for(int i = 0; i < input.length(); i++ ) { + Character currentChar = input.charAt(i); + if (map.containsKey(currentChar)) { + currentChar = map.get(currentChar); + //get returns the value of the key + } + builder.append(currentChar); } - - - -// input.replace('f','7'); -// input.replace('s','$'); //ignored - return input; + return builder.toString(); } - public static HashMap populateMap(){ - HashMap map = new HashMap(); - map.put('f','7'); - map.put('s','$'); - map.put('1','!'); - map.put('a','@'); - return map; - } - - public static ArrayList getArray(String input){ - ArrayList charArrary = new ArrayList (); - for (char c: input.toCharArray()) { - charArrary.add(c); - } - return charArrary; - } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index 9751da1..0c41540 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,18 +1,35 @@ package io.zipcoder; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import java.util.HashMap; + public class Problem1Test { + Problem1 problem1; + HashMap map; + String input; + + @Before + public void setUp() { + map = new HashMap(); + map.put('f', '7'); + map.put('s', '$'); + map.put('1', '!'); + map.put('a', '@'); + problem1 = new Problem1(); + input = "The farmer went to the store to get 1 dollar’s worth of fertilizer"; + } @Test public void Test(){ - String input = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; - String expected = ""; - String actual = Problem1.replaceCharsInString(input); + + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = Problem1.replaceCharsInString(input,map); Assert.assertEquals(expected,actual); }