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

public class Problem1 {

public String changeLetters(String str,HashMap<Character,Character> changeMap){
StringBuilder builder = new StringBuilder();
for(int i = 0;i<str.length();i++){
Character currentChar = str.charAt(i);
if(changeMap.containsKey(Character.toLowerCase(currentChar))){
currentChar = changeMap.get(Character.toLowerCase(currentChar));
}
builder.append(currentChar);
}
return builder.toString();
}

public String recursion(String str,HashMap<Character,Character> changeMap){
if(str.length() == 0){
return "";
}
Character currentChar = str.charAt(0);
if(changeMap.containsKey(Character.toLowerCase(currentChar))){
currentChar = changeMap.get(Character.toLowerCase(currentChar));
}
return currentChar + recursion(str.substring(1),changeMap);

}



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

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

import java.util.HashMap;

public class Problem1Test {
Problem1 problem1;
HashMap<Character,Character> changeMap;
String input;

@Before
public void setUp() {
changeMap = new HashMap<Character, Character>();
changeMap.put('f', '7');
changeMap.put('s', '$');
changeMap.put('1', '!');
changeMap.put('a', '@');
problem1 = new Problem1();
input = "The Farmer went to the store to get 1 dollar’s worth of fertilizer";

}

@Test
public void withLoop(){
String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer";
String actual = problem1.changeLetters(input,changeMap);
Assert.assertEquals(expected,actual);
}

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