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

import java.util.HashMap;

public class Problem1 {

private HashMap<Character, Character> replaceChars = new HashMap<Character, Character>();

public Problem1() {
replaceChars.put('f', '7');
replaceChars.put('s', '$');
replaceChars.put('1', '!');
replaceChars.put('a', '@');
}

public String replaceCharsByIteration(String input) {
char[] allChars = input.toCharArray();
for(int i = 0; i < allChars.length; i++) {
char c = allChars[i];
if(replaceChars.containsKey(Character.toLowerCase(c))) {
allChars[i] = replaceChars.get(Character.toLowerCase(c));
}
}

return new String(allChars);
}

public String replaceCharsByRecursion(String input) {
char[] allChars = input.toCharArray();
for(char c : replaceChars.keySet()) {
int index = input.indexOf(Character.toLowerCase(c));
if(index > -1) {
allChars[index] = replaceChars.get(c);
input = new String(allChars);
replaceCharsByRecursion(input);
}
else {
index = input.indexOf(Character.toUpperCase(c));
if(index > -1) {
allChars[index] = replaceChars.get(c);
input = new String(allChars);
replaceCharsByRecursion(input);
}
}
}
return input;
}

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

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

public class Problem1Test {

@Test
public void replaceCharsByIterationTest() {
Problem1 problem1 = new Problem1();
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 = problem1.replaceCharsByIteration(input);
Assert.assertEquals(expected, actual);
}

@Test
public void replaceCharsByRecursionTest() {
Problem1 problem1 = new Problem1();
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 = problem1.replaceCharsByRecursion(input);
Assert.assertEquals(expected, actual);
}
}