From 839b34fcb7a51a0fadf622cd23aa6dd0a9992595 Mon Sep 17 00:00:00 2001 From: pavel Date: Mon, 27 Nov 2017 10:04:10 -0500 Subject: [PATCH 1/2] "Iterate almost working" --- pom.xml | 5 ++ src/main/java/io/zipcoder/Problem1.java | 48 ++++++++++++++++++ src/test/java/io/zipcoder/Problem1Test.java | 56 +++++++++++++++++++++ 3 files changed, 109 insertions(+) diff --git a/pom.xml b/pom.xml index 2a6372d..746d4ec 100644 --- a/pom.xml +++ b/pom.xml @@ -18,4 +18,9 @@ + + + 1.8 + 1.8 + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..7c8cf67 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,52 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + public class Problem1 { + + private HashMap cypher = new HashMap(){{ + put('f', '7'); + put('s', '$'); + put('1', '!'); + put('a', '@'); + }}; + + //step1 read character + //step2 decide if needs replacing + //step3 replace + + Character getCharAt(String raw, int index){ + return raw.charAt(index); + } + + Boolean decideIfReplace(Character character){//assume .toLowerCase() has already been called on input String + Boolean output = false; + ArrayList wanted = new ArrayList<>(cypher.keySet()); + for (Character wantChar : + wanted) { + if(character.equals(wantChar)) { + output = true; + } + } + return output; + } + + String replaceChar(String raw, Character character, int index){ + Character replacement = cypher.get(character); + return raw.replace(character, replacement); + } + + public String encodeIterate(String raw){ + String flattened = raw.toLowerCase(); + String output = raw; + for (int i = 0; i < raw.length(); i++) { + Character character = getCharAt(flattened, i); + if (decideIfReplace(character)){ + output = replaceChar(output, character, i); + } + } + return output; + } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..2afeb40 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,60 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem1Test { + + @Test + public void getCharTest(){ + Problem1 problem1 = new Problem1(); + String raw = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; + + Character expected = 'F'; + Character actual = problem1.getCharAt(raw, 4); + + Assert.assertEquals(expected, actual); + } + + @Test + public void decideIfReplaceTrueTest(){ + Problem1 problem1 = new Problem1(); + String raw = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; + + Boolean actual = problem1.decideIfReplace(problem1.getCharAt(raw, 5)); + + Assert.assertTrue(actual); + } + + @Test + public void decideIfReplaceFalseTest(){ + Problem1 problem1 = new Problem1(); + String raw = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; + + Boolean actual = problem1.decideIfReplace(problem1.getCharAt(raw, 6)); + + Assert.assertFalse(actual); + } + + @Test + public void replaceCharTest(){ + Problem1 problem1 = new Problem1(); + String raw = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; + + String expected = "the 7armer went to the store to get 1 dollar’s worth o7 7ertilizer"; + String actual = problem1.replaceChar(raw, 'f', 4); + + Assert.assertEquals(expected, actual); + } + + @Test + public void encodeIterateTest(){ + Problem1 problem1 = new Problem1(); + String raw = "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 of 7ertilizer"; + String actual = problem1.encodeIterate(raw); + + Assert.assertEquals(expected, actual); + } } From 24714e5b6d483319355bb9f4caf442b92152f250 Mon Sep 17 00:00:00 2001 From: pavel Date: Thu, 30 Nov 2017 22:32:48 -0500 Subject: [PATCH 2/2] "Curse you, recursion" --- src/main/java/io/zipcoder/Problem1.java | 58 +++++++-------------- src/test/java/io/zipcoder/Problem1Test.java | 52 ++++++------------ 2 files changed, 35 insertions(+), 75 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 7c8cf67..4cbf939 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,52 +1,34 @@ package io.zipcoder; -import java.util.ArrayList; import java.util.HashMap; -import java.util.Map; +import java.util.Set; public class Problem1 { - private HashMap cypher = new HashMap(){{ - put('f', '7'); - put('s', '$'); - put('1', '!'); - put('a', '@'); - }}; - - //step1 read character - //step2 decide if needs replacing - //step3 replace - - Character getCharAt(String raw, int index){ - return raw.charAt(index); - } - - Boolean decideIfReplace(Character character){//assume .toLowerCase() has already been called on input String - Boolean output = false; - ArrayList wanted = new ArrayList<>(cypher.keySet()); - for (Character wantChar : - wanted) { - if(character.equals(wantChar)) { - output = true; - } + String encodeIterate (String raw, HashMap cypher){ + Set keyChars = cypher.keySet(); + for (Character keyChar : + keyChars) { + raw = raw.replace(keyChar, cypher.get(keyChar)); } - return output; + return raw; } - String replaceChar(String raw, Character character, int index){ - Character replacement = cypher.get(character); - return raw.replace(character, replacement); + public String encodeRecursive (String raw, HashMap cypher){ + return encodeRecursive(raw, cypher, 0); + } - public String encodeIterate(String raw){ - String flattened = raw.toLowerCase(); - String output = raw; - for (int i = 0; i < raw.length(); i++) { - Character character = getCharAt(flattened, i); - if (decideIfReplace(character)){ - output = replaceChar(output, character, i); - } + private String encodeRecursive (String raw, HashMap cypher, int index){ + Character keyChar = raw.charAt(index); + if(cypher.containsKey(keyChar)) { + raw = raw.replace(keyChar, cypher.get(keyChar)); + } + if(index == raw.length()-1) { + return raw; + } else { + index++; + return encodeRecursive(raw, cypher, index); } - return output; } } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index 2afeb40..abfa7b1 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -3,57 +3,35 @@ import org.junit.Assert; import org.junit.Test; -public class Problem1Test { - - @Test - public void getCharTest(){ - Problem1 problem1 = new Problem1(); - String raw = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; - - Character expected = 'F'; - Character actual = problem1.getCharAt(raw, 4); - - Assert.assertEquals(expected, actual); - } - - @Test - public void decideIfReplaceTrueTest(){ - Problem1 problem1 = new Problem1(); - String raw = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; +import java.util.HashMap; - Boolean actual = problem1.decideIfReplace(problem1.getCharAt(raw, 5)); - - Assert.assertTrue(actual); - } - - @Test - public void decideIfReplaceFalseTest(){ - Problem1 problem1 = new Problem1(); - String raw = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; - - Boolean actual = problem1.decideIfReplace(problem1.getCharAt(raw, 6)); +public class Problem1Test { - Assert.assertFalse(actual); - } + private HashMap cypher = new HashMap(){{ + put('f', '7'); + put('s', '$'); + put('1', '!'); + put('a', '@'); + }}; @Test - public void replaceCharTest(){ + public void encodeIterateTest(){ Problem1 problem1 = new Problem1(); - String raw = "the farmer went to the store to get 1 dollar’s worth of fertilizer"; + String raw = "The Farmer went to the store to get 1 dollar’s worth of fertilizer"; - String expected = "the 7armer went to the store to get 1 dollar’s worth o7 7ertilizer"; - String actual = problem1.replaceChar(raw, 'f', 4); + String expected = "The F@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = problem1.encodeIterate(raw, cypher); Assert.assertEquals(expected, actual); } @Test - public void encodeIterateTest(){ + public void encodeRecursiveTest(){ Problem1 problem1 = new Problem1(); String raw = "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 of 7ertilizer"; - String actual = problem1.encodeIterate(raw); + String expected = "The F@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = problem1.encodeRecursive(raw, cypher); Assert.assertEquals(expected, actual); }