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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
</dependency>

</dependencies>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
</project>
30 changes: 30 additions & 0 deletions src/main/java/io/zipcoder/Problem1.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
package io.zipcoder;

import java.util.HashMap;
import java.util.Set;

public class Problem1 {

String encodeIterate (String raw, HashMap<Character, Character> cypher){
Set<Character> keyChars = cypher.keySet();
for (Character keyChar :
keyChars) {
raw = raw.replace(keyChar, cypher.get(keyChar));
}
return raw;
}

public String encodeRecursive (String raw, HashMap<Character, Character> cypher){
return encodeRecursive(raw, cypher, 0);

}

private String encodeRecursive (String raw, HashMap<Character, Character> 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);
}
}
}
34 changes: 34 additions & 0 deletions src/test/java/io/zipcoder/Problem1Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
package io.zipcoder;

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

import java.util.HashMap;

public class Problem1Test {

private HashMap<Character, Character> cypher = new HashMap<Character, Character>(){{
put('f', '7');
put('s', '$');
put('1', '!');
put('a', '@');
}};

@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 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 encodeRecursiveTest(){
Problem1 problem1 = new Problem1();
String raw = "The Farmer went to the store to get 1 dollar’s worth of fertilizer";

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);
}
}