Last active
March 21, 2020 15:28
-
-
Save rij12/58f5c0d46504e6614f36814dcbfdbe71 to your computer and use it in GitHub Desktop.
HackAJob Interview Royal Problem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* @author Richard Price-Jones | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
/* | |
* Some work here; return type and arguments should be according to the problem's requirements | |
*/ | |
Main main = new Main(); | |
String[] inputs = {"George VI", "William II", "Elizabeth I", "William I"}; | |
main.processInput(inputs); | |
} | |
/** | |
* Process Royal Names and return as ordered String | |
* | |
* @param royalNames String[] RoyalNames | |
* @return String RoyalNamesOrder | |
*/ | |
public String processInput(String... royalNames) { | |
List<Royal> royals = new ArrayList<>(); | |
for (String royalName : royalNames) { | |
String[] names = royalName.split(" "); | |
royals.add(new Royal(names[0], names[1])); | |
} | |
StringBuilder stringBuilder = new StringBuilder(); | |
// Sort Values and appended into a string. | |
royals.stream().sorted(Comparator.comparing(Royal::getName) | |
.thenComparing(r -> convertRomanToInt(r.getOrdinalNumber()))) | |
.forEach(r -> stringBuilder.append(r.toString()).append(" ")); | |
return stringBuilder.toString(); | |
} | |
/** | |
* Takes a Roman number and Converts into a Integer. | |
* | |
* @param roman String Roman Number | |
* @return Integer Converted Value | |
*/ | |
public Integer convertRomanToInt(String roman) { | |
HashMap<String, Integer> roman_numerals = new HashMap<>(); | |
roman_numerals.put("I", 1); | |
roman_numerals.put("V", 5); | |
roman_numerals.put("X", 10); | |
roman_numerals.put("L", 50); | |
String currentValue; | |
// Roman Logic | |
// if Number is greater or equal than next one add to result otherwise subtract it from result. | |
// 47 -> XLVII -> -10 + 50 + 5 + 1 + 1 = 47 | |
char[] romanArr = roman.toCharArray(); | |
Integer result = 0; | |
for (int i = 0; i <= romanArr.length - 1; i++) { | |
currentValue = String.valueOf(roman.charAt(i)); | |
if ((i + 1) == roman.length() || roman_numerals.get(currentValue) >= roman_numerals.get(String.valueOf(roman.charAt(i + 1)))) { | |
result += roman_numerals.get(currentValue); | |
} else { | |
result -= roman_numerals.get(currentValue); | |
} | |
} | |
return result; | |
} | |
} | |
class Royal { | |
private final String name; | |
private final String ordinalNumber; | |
public Royal(String name, String ordinalNumber) { | |
this.name = name; | |
this.ordinalNumber = ordinalNumber; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getOrdinalNumber() { | |
return ordinalNumber; | |
} | |
@Override | |
public String toString() { | |
return this.name + " " + this.ordinalNumber; | |
} | |
} | |
class TestRoyal { | |
@Test | |
public void testProcessInput() { | |
String[] inputs = {"George VI", "William II", "Elizabeth I", "William I"}; | |
String expectOutput = "Elizabeth I George VI William I William II"; | |
Main main = new Main(); | |
String result = main.processInput(inputs); | |
Assert.assertEquals(expectOutput, result); | |
} | |
@Test | |
public void testConvertRomanToIntForFortySeven() { | |
final String romanValueForFortySeven = "XLVII"; | |
final Integer expectOutput = 47; | |
Main main = new Main(); | |
Integer result = main.convertRomanToInt(romanValueForFortySeven); | |
Assert.assertEquals(expectOutput, result); | |
} | |
@Test | |
public void testConvertRomanToIntForOne() { | |
final String romanValueForOne = "I"; | |
final Integer expectOutput = 1; | |
Main main = new Main(); | |
Integer result = main.convertRomanToInt(romanValueForOne); | |
Assert.assertEquals(expectOutput, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment