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


import java.util.Map;

// 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”
// Output = “The 7@rmer went to the $tore to get ! doll@r’$ worth of 7ertilizer”
public class Problem1 {



public static String replaceCharsInString(String input,Map<Character,Character> map) {
StringBuilder builder = new StringBuilder();

for(int i = 0; i < input.length(); i++ ) {
Character currentChar = input.charAt(i);

if (map.containsKey(currentChar)) {
currentChar = map.get(currentChar);
//get returns the value of the key
}
builder.append(currentChar);
}
return builder.toString();
}


}
32 changes: 32 additions & 0 deletions src/test/java/io/zipcoder/Problem1Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
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> map;
String input;

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

}

@Test
public void Test(){


String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer";
String actual = Problem1.replaceCharsInString(input,map);

Assert.assertEquals(expected,actual);
}
}