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
35 changes: 35 additions & 0 deletions src/main/java/io/zipcoder/Problem1.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
package io.zipcoder;

import java.util.Map;

public class Problem1 {


public String replaceIteration(String expected, Map<String, String> aMap) {

String[] stringArray = expected.split("");
String toReturn = "";

for (int i = 0; i < stringArray.length; i++) {
if (aMap.keySet().contains(stringArray[i].toLowerCase())) {
stringArray[i] = aMap.get(stringArray[i].toLowerCase());
}
toReturn += stringArray[i];
}

return toReturn;
}


public String replaceRecursion(String input, Map<String, String> testMap) {
String lower = input.toLowerCase();

if (testMap.keySet().size() == 0) {
return input.substring(0,1).toUpperCase() + input.substring(1);
} else {
String toReturn = lower.replaceAll(testMap.keySet().iterator().next(), "\\" + testMap.get(testMap.keySet().iterator().next()));
testMap.remove(testMap.keySet().iterator().next());
return replaceRecursion(toReturn, testMap);
}


}


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

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

import java.util.HashMap;
import java.util.Map;

public class Problem1Test {

private Problem1 tester;

@Before
public void setup() {
tester = new Problem1();
}

@Test
public void replaceIterationTest1() {
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("f", "7");
testMap.put("s", "$");
testMap.put("1", "!");
testMap.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 = tester.replaceIteration(input, testMap);
Assert.assertEquals(expected, actual);
}

@Test
public void replaceRecursionTest1() {
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("f", "7");
testMap.put("s", "$");
testMap.put("1", "!");
testMap.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 = tester.replaceRecursion(input, testMap);
Assert.assertEquals(expected, actual);
}

}