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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ UNIT TESTING IS MANDATORY !!!

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”
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”

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 replaceByIteration(Map aMap, String input) {
StringBuilder output = new StringBuilder();
for (String aChar : input.split("")){
if (aMap.containsKey(aChar)){
output.append(aMap.get(aChar));
}
else {
output.append(aChar);
}
}
return output.toString();
}

public String replaceByRecursion(Map aMap, String input, String output, Integer index) {
String[] inputSplit = input.split("");

String currentChar = inputSplit[index];
if (aMap.containsKey(currentChar)){
output += aMap.get(currentChar);
}
else {
output += currentChar;
}

if (index<inputSplit.length-1) {
index++;
output = replaceByRecursion(aMap, input, output, index);
}

return output;
}

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

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

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

public class Problem1Test {

@Test
public void replaceByIterationTest() {
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";
Map<String,String> mapKey = new HashMap<String, String>();

mapKey.put("f","7");
mapKey.put("s","$");
mapKey.put("1","!");
mapKey.put("a","@");

Problem1 problem1 = new Problem1();

String actual = problem1.replaceByIteration(mapKey, input);

Assert.assertEquals(expected,actual);
}

@Test
public void replaceByRecursionTest() {
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";
Map<String,String> mapKey = new HashMap<String, String>();

mapKey.put("f","7");
mapKey.put("s","$");
mapKey.put("1","!");
mapKey.put("a","@");

Problem1 problem1 = new Problem1();
String output = "";

String actual = problem1.replaceByRecursion(mapKey, input, output,0);

Assert.assertEquals(expected,actual);
}
}