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

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

public class Problem1 {

private Map<Character, Character> map;

public Problem1() {
map = new HashMap<Character, Character>();
initializeMap();
}

public Map<Character, Character> getMap() {
return map;
}

public void initializeMap() {
map.put('f', '7');
map.put('s', '$');
map.put('1', '!');
map.put('a', '@');
}

public String replaceCharsWithIteration(String originalString) {
char[] charArray = originalString.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char c = Character.toLowerCase(charArray[i]);
if (map.containsKey(c)) {
charArray[i] = map.get(c);
}
}
return new String(charArray);
}

public String replaceCharsWithRecursion(String originalString, int index) {
char[] charArray = originalString.toCharArray();
char c = Character.toLowerCase(charArray[index]);
if (map.containsKey(c)) {
charArray[index] = map.get(c);
}
if (index < originalString.length() - 1) {
index++;
return replaceCharsWithRecursion(new String(charArray), index);
}
return new String(charArray);
}

public static void main(String[] args) {
Problem1 problem1 = new Problem1();

String input = "The Farmer went to the store to get 1 dollar\'s worth of fertilizer";
String output = "The 7@rmer went to the $tore to get ! doll@r\'$ worth o7 7ertilizer";

String test = problem1.replaceCharsWithRecursion(input, 0);

System.out.println(input);
System.out.println(output);
System.out.println(test);
}


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

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

public class Problem1Test {

private Problem1 problem1;
private String input = "The Farmer went to the store to get 1 dollar\'s worth of fertilizer";
private String output = "The 7@rmer went to the $tore to get ! doll@r\'$ worth o7 7ertilizer";

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

@Test
public void initializeMapTest() {
// Given
char key = 'f';
char expectedValue = '7';
// When
char actualValue = problem1.getMap().get('f');
// Then
Assert.assertEquals(expectedValue, actualValue);
}

@Test
public void replaceCharsWithIterationTest() {
// Given
String expectedOutput = output;
// When
String actualOutput = problem1.replaceCharsWithIteration(input);
// Then
Assert.assertEquals(expectedOutput, actualOutput);
}

@Test
public void replaceCharsWithRecursionTest() {
// Given
int startingIndex = 0;
String expectedOutput = output;
// When
String actualOutput = problem1.replaceCharsWithRecursion(input, startingIndex);
// Then
Assert.assertEquals(expectedOutput, actualOutput);
}


}