Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

41 changes: 41 additions & 0 deletions src/main/java/io/zipcoder/Problem1.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
package io.zipcoder;

import java.util.*;

public class Problem1 {

/**
* Replace certain characters from a string using iteration
*
* @param inputString string to be altered
* @param swapMap contains char represented by key and it's corresponding replacement value
* @return a String with any character represented by a key in the map replaced with its corresponding value.
*/
public String withIteration(String inputString, Map<String, String> swapMap) {
String outPutString = "";
for (HashMap.Entry<String, String> val : swapMap.entrySet()) {
String pattern = "(?i)" + val.getKey();
//http://cephas.net/blog/2006/02/09/javalangillegalargumentexception-illegal-group-reference-replaceall-and-dollar-signs/
String replace = "\\" + val.getValue(); //some of the replacements were regular expression operators, if escape them will be fine
outPutString = inputString.replaceAll(pattern, replace);
inputString = outPutString;
}
return outPutString;
}

/**
* Replace certain characters from a string using recursion
*
* @param inputString string to be altered
* @param swapMap contains char represented by key and it's corresponding replacement value
* @return a String with any character represented by a key in the map replaced with its corresponding value.
*/
public String withRecursion(String inputString, Map<String, String> swapMap) {
//law of recursion have a condition to stop, empty string gets appended known as non-function
if(inputString.length() == 0){
return "";
}
String current = inputString.substring(0,1);
if (swapMap.containsKey(current.toLowerCase())) {
current = swapMap.get(current.toLowerCase());
}
return current + withRecursion(inputString.substring(1),swapMap);
}
}

39 changes: 39 additions & 0 deletions src/test/java/io/zipcoder/Problem1Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

public class Problem1Test {

private static Problem1 testProb;

private Map<String, String> testMap;
private String inputString;

@Before
public void setUp() {
testMap = new HashMap<String, String>();
testMap.put("f", "7");
testMap.put("s", "$");
testMap.put("1", "!");
testMap.put("a", "@");
testProb = new Problem1();
inputString = "The Farmer went to the store to get 1 dollar’s worth of fertilizer";
}

@Test
public void testWithIteration() {
String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer";
String actual = testProb.withIteration(inputString, testMap);
Assert.assertEquals(expected, actual);
}

@Test
public void testWithRecursion() {
String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer";
String actual = testProb.withRecursion(inputString, testMap);
Assert.assertEquals(expected, actual);
}

}