From 8a2492aad0b7815c59698d106ab0754390395528 Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Fri, 15 Sep 2017 14:31:57 +0300 Subject: [PATCH 1/7] Implementation plan --- roman-numerals-solution/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 roman-numerals-solution/README.md diff --git a/roman-numerals-solution/README.md b/roman-numerals-solution/README.md new file mode 100644 index 0000000..5e28285 --- /dev/null +++ b/roman-numerals-solution/README.md @@ -0,0 +1,11 @@ +Hi, + + +## Implementation plan + +* Create java main class with ability to read input from cmd pipe +* Implement convert algorithm +* Catch all possible exceptions +* Make some refac/add test/finally review + +## User guide \ No newline at end of file From 535e18241f3197d29278c0625fbed2898e2ecc02 Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Fri, 15 Sep 2017 14:32:51 +0300 Subject: [PATCH 2/7] Create java main class with ability to read input from cmd pipe --- .../RomanNumeralsConvert.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 roman-numerals-solution/RomanNumeralsConvert.java diff --git a/roman-numerals-solution/RomanNumeralsConvert.java b/roman-numerals-solution/RomanNumeralsConvert.java new file mode 100644 index 0000000..e18d286 --- /dev/null +++ b/roman-numerals-solution/RomanNumeralsConvert.java @@ -0,0 +1,22 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class RomanNumeralsConvert { + + public static void main(String[] args) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in));) { + String line; + while ((line = in.readLine()) != null) { + line = line.replace("\"", "").trim(); + final String[] numArray = line.split("\\s+"); + + for (int i = 0; i < numArray.length; i++) { + System.out.println(numArray[i]); + } + } + } catch (final IOException e) { + System.err.println("IOException reading System.in" + e); + } + } +} \ No newline at end of file From d33a641d18635e5501f899ef970b502e35ed24d9 Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Fri, 15 Sep 2017 14:39:58 +0300 Subject: [PATCH 3/7] Implement convert algorithm --- .../RomanNumeralsConvert.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/roman-numerals-solution/RomanNumeralsConvert.java b/roman-numerals-solution/RomanNumeralsConvert.java index e18d286..1a3259e 100644 --- a/roman-numerals-solution/RomanNumeralsConvert.java +++ b/roman-numerals-solution/RomanNumeralsConvert.java @@ -12,11 +12,47 @@ public static void main(String[] args) { final String[] numArray = line.split("\\s+"); for (int i = 0; i < numArray.length; i++) { - System.out.println(numArray[i]); + System.out.println(convert(numArray[i])); } } } catch (final IOException e) { System.err.println("IOException reading System.in" + e); } } + + public static String convert(String numStr) { + String Roman = ""; + + final String onesArray[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; + final String tensArray[] = { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }; + final String hundredsArray[] = { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }; + int num = Integer.parseInt(numStr); + + final int ones = num % 10; + + num = (num - ones) / 10; + final int tens = num % 10; + + num = (num - tens) / 10; + final int hundreds = num % 10; + + num = (num - hundreds) / 10; + for (int i = 0; i < num; i++) { + Roman += "M"; + } + + if (hundreds >= 1) { + Roman += hundredsArray[hundreds - 1]; + } + + if (tens >= 1) { + Roman += tensArray[tens - 1]; + } + + if (ones >= 1) { + Roman += onesArray[ones - 1]; + } + + return String.valueOf(Roman); + } } \ No newline at end of file From 1103ee24b87a4381865faa3c773bec1d2b63dd92 Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Fri, 15 Sep 2017 15:11:07 +0300 Subject: [PATCH 4/7] Catch all possible exceptions --- .../RomanNumeralsConvert.java | 102 +++++++++++++----- 1 file changed, 74 insertions(+), 28 deletions(-) diff --git a/roman-numerals-solution/RomanNumeralsConvert.java b/roman-numerals-solution/RomanNumeralsConvert.java index 1a3259e..24a0a47 100644 --- a/roman-numerals-solution/RomanNumeralsConvert.java +++ b/roman-numerals-solution/RomanNumeralsConvert.java @@ -3,56 +3,102 @@ import java.io.InputStreamReader; public class RomanNumeralsConvert { + + public final static String onesArray[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; + public final static String tensArray[] = { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }; + public final static String hundredsArray[] = { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }; + + private static String[] numArray; public static void main(String[] args) { + + readInput(); + + validate(); + + convert(); + + printResult(); + + } + + public static void readInput() { try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in));) { String line; while ((line = in.readLine()) != null) { line = line.replace("\"", "").trim(); - final String[] numArray = line.split("\\s+"); - - for (int i = 0; i < numArray.length; i++) { - System.out.println(convert(numArray[i])); - } + numArray = line.split("\\s+"); } } catch (final IOException e) { System.err.println("IOException reading System.in" + e); } } - public static String convert(String numStr) { - String Roman = ""; + public static void validate() { + for (int i = 0; i < numArray.length; i++) { + final String numStr = numArray[i]; + boolean e = true; + for (int k = 0; k < numStr.length(); k++) { + if (Character.isDigit(numStr.charAt(k)) == false) { + numArray[i] = numArray[i] + ", illegal argument error - is not valid digital number."; + e = false; + break; + } + } + + if (e) { + final int num = Integer.parseInt(numStr); + if (num > 3999 || num < 1) { + numArray[i] = numArray[i] + ", range bound error - number should be in the range [1 - 3999]."; + } + } + } + } + + public static void convert() { + for (int i = 0; i < numArray.length; i++) { + final String numStr = numArray[i]; - final String onesArray[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; - final String tensArray[] = { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }; - final String hundredsArray[] = { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }; - int num = Integer.parseInt(numStr); + if (numStr.contains("error")) { + continue; + } + + String Roman = ""; + int num = Integer.parseInt(numStr); - final int ones = num % 10; + final int ones = num % 10; - num = (num - ones) / 10; - final int tens = num % 10; + num = (num - ones) / 10; + final int tens = num % 10; - num = (num - tens) / 10; - final int hundreds = num % 10; + num = (num - tens) / 10; + final int hundreds = num % 10; - num = (num - hundreds) / 10; - for (int i = 0; i < num; i++) { - Roman += "M"; - } + num = (num - hundreds) / 10; + for (int n = 0; n < num; n++) { + Roman += "M"; + } - if (hundreds >= 1) { - Roman += hundredsArray[hundreds - 1]; - } + if (hundreds >= 1) { + Roman += hundredsArray[hundreds - 1]; + } + + if (tens >= 1) { + Roman += tensArray[tens - 1]; + } - if (tens >= 1) { - Roman += tensArray[tens - 1]; + if (ones >= 1) { + Roman += onesArray[ones - 1]; + } + + numArray[i] = String.valueOf(Roman); } + } - if (ones >= 1) { - Roman += onesArray[ones - 1]; + public static void printResult() { + for (int i = 0; i < numArray.length; i++) { + System.out.println(numArray[i]); } - return String.valueOf(Roman); } } \ No newline at end of file From 384c346716ea711a0a24f28cf3d13a41e9e42476 Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Fri, 15 Sep 2017 15:33:27 +0300 Subject: [PATCH 5/7] Make some refac/add test/finally review --- .../RomanNumeralsConvert.java | 73 +++++++++++++++++-- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/roman-numerals-solution/RomanNumeralsConvert.java b/roman-numerals-solution/RomanNumeralsConvert.java index 24a0a47..acf11b8 100644 --- a/roman-numerals-solution/RomanNumeralsConvert.java +++ b/roman-numerals-solution/RomanNumeralsConvert.java @@ -8,17 +8,30 @@ public class RomanNumeralsConvert { public final static String tensArray[] = { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }; public final static String hundredsArray[] = { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }; + public final static String TEST_MOD = "-test"; + public final static String ONE = "I"; + public final static String TWELVE = "XII"; + public final static String ONE_HUNDRED_AND_THREE = "CIII"; + private static String[] numArray; public static void main(String[] args) { - readInput(); + if (args.length > 0) { + if (args.length > 1 || !TEST_MOD.equals(args[0])) { + System.out.println("Only one flag supports: - test"); + } else { + runTest(); + } + } else { + readInput(); - validate(); + validate(); - convert(); + convert(); - printResult(); + printResult(); + } } @@ -62,7 +75,7 @@ public static void convert() { if (numStr.contains("error")) { continue; } - + String Roman = ""; int num = Integer.parseInt(numStr); @@ -101,4 +114,54 @@ public static void printResult() { } } + + public static void runTest() { + numArray = new String[5]; + + numArray[0] = "1"; + numArray[1] = "12"; + numArray[2] = "103"; + + numArray[3] = "10char3"; + numArray[4] = "10399911"; + + validate(); + + convert(); + + System.out.print("1 => " + numArray[0]); + if (ONE.equals(numArray[0])) { + System.out.println(" passed."); + } else { + System.out.println(" failed."); + } + + System.out.print("12 => " + numArray[1]); + if (TWELVE.equals(numArray[1])) { + System.out.println(" passed."); + } else { + System.out.println(" failed."); + } + + System.out.print("103 => " + numArray[2]); + if (ONE_HUNDRED_AND_THREE.equals(numArray[2])) { + System.out.println(" passed."); + } else { + System.out.println(" failed."); + } + + System.out.print("10char3 => " + numArray[3]); + if ("10char3, illegal argument error - is not valid digital number.".equals(numArray[3])) { + System.out.println(" passed."); + } else { + System.out.println(" failed."); + } + + System.out.print("10399911 => " + numArray[4]); + if ("10399911, range bound error - number should be in the range [1 - 3999].".equals(numArray[4])) { + System.out.println(" passed."); + } else { + System.out.println(" failed."); + } + } } \ No newline at end of file From 84df0ee118e4c587d32eb54f3a85a09bfa4e3bfa Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Fri, 15 Sep 2017 15:38:49 +0300 Subject: [PATCH 6/7] Add user guide steps --- roman-numerals-solution/README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/roman-numerals-solution/README.md b/roman-numerals-solution/README.md index 5e28285..e663a11 100644 --- a/roman-numerals-solution/README.md +++ b/roman-numerals-solution/README.md @@ -8,4 +8,24 @@ Hi, * Catch all possible exceptions * Make some refac/add test/finally review -## User guide \ No newline at end of file +## User guide + +1. Compile java source file: + + G:>javac RomanNumeralsConvert.java + +2. Run java application with cmd pipe: + + G:>echo "1 12 103" | java RomanNumeralsConvert + I + XII + CIII + +3. Add "-test" flag to run application in test mode: + + G:>echo "1 12 103" | java RomanNumeralsConvert -test + 1 => I passed. + 12 => XII passed. + 103 => CIII passed. + 10char3 => 10char3, illegal argument error - is not valid digital number. passed. + 10399911 => 10399911, range bound error - number should be in the range [1 - 3999]. passed. \ No newline at end of file From 0a0a499203fe857e008305012641df9c8eb6c62f Mon Sep 17 00:00:00 2001 From: EnjoyLife Date: Mon, 18 Sep 2017 13:38:34 +0300 Subject: [PATCH 7/7] Rewrite convert solution using collection change convert algorithm exit if catch exception changed methods access modifiers fix condition in if statement --- .../RomanNumeralsConvert.java | 156 +++++++++--------- 1 file changed, 82 insertions(+), 74 deletions(-) diff --git a/roman-numerals-solution/RomanNumeralsConvert.java b/roman-numerals-solution/RomanNumeralsConvert.java index acf11b8..a519d92 100644 --- a/roman-numerals-solution/RomanNumeralsConvert.java +++ b/roman-numerals-solution/RomanNumeralsConvert.java @@ -1,59 +1,83 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.ListIterator; +import java.util.TreeMap; public class RomanNumeralsConvert { - - public final static String onesArray[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; - public final static String tensArray[] = { "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }; - public final static String hundredsArray[] = { "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }; public final static String TEST_MOD = "-test"; public final static String ONE = "I"; public final static String TWELVE = "XII"; public final static String ONE_HUNDRED_AND_THREE = "CIII"; - private static String[] numArray; + private final static TreeMap map = new TreeMap<>(); + static { + map.put(1000, "M"); + map.put(900, "CM"); + map.put(500, "D"); + map.put(400, "CD"); + map.put(100, "C"); + map.put(90, "XC"); + map.put(50, "L"); + map.put(40, "XL"); + map.put(10, "X"); + map.put(9, "IX"); + map.put(5, "V"); + map.put(4, "IV"); + map.put(1, "I"); + } public static void main(String[] args) { if (args.length > 0) { if (args.length > 1 || !TEST_MOD.equals(args[0])) { System.out.println("Only one flag supports: - test"); - } else { - runTest(); } + + runTest(); + } else { - readInput(); + final List numbersList = readInput(); - validate(); + validate(numbersList); - convert(); + convert(numbersList); - printResult(); + printResult(numbersList); } } - public static void readInput() { - try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in));) { + private static List readInput() { + final List numbersList = new ArrayList<>(); + try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) { String line; while ((line = in.readLine()) != null) { line = line.replace("\"", "").trim(); - numArray = line.split("\\s+"); + numbersList.addAll(Arrays.asList(line.split("\\s+"))); } + } catch (final IOException e) { System.err.println("IOException reading System.in" + e); + System.exit(1); } + + return numbersList; } - public static void validate() { - for (int i = 0; i < numArray.length; i++) { - final String numStr = numArray[i]; + private static void validate(List numbersList) { + final ListIterator listIterator = numbersList.listIterator(); + while (listIterator.hasNext()) { + final String numStr = listIterator.next(); + boolean e = true; for (int k = 0; k < numStr.length(); k++) { if (Character.isDigit(numStr.charAt(k)) == false) { - numArray[i] = numArray[i] + ", illegal argument error - is not valid digital number."; + listIterator.set(numStr + ", illegal argument error - is not valid digital number."); e = false; break; } @@ -62,103 +86,87 @@ public static void validate() { if (e) { final int num = Integer.parseInt(numStr); if (num > 3999 || num < 1) { - numArray[i] = numArray[i] + ", range bound error - number should be in the range [1 - 3999]."; + listIterator.set(numStr + ", range bound error - number should be in the range [1 - 3999]."); } } } } - public static void convert() { - for (int i = 0; i < numArray.length; i++) { - final String numStr = numArray[i]; + private final static String toRoman(Integer number) { + final int l = map.floorKey(number); + if (number == l) { + return map.get(number); + } + return map.get(l) + toRoman(number - l); + } + + private static void convert(List numbersList) { + final ListIterator listIterator = numbersList.listIterator(); + while (listIterator.hasNext()) { + final String numStr = listIterator.next(); if (numStr.contains("error")) { continue; } + + final Integer num = Integer.valueOf(numStr); - String Roman = ""; - int num = Integer.parseInt(numStr); - - final int ones = num % 10; - - num = (num - ones) / 10; - final int tens = num % 10; - - num = (num - tens) / 10; - final int hundreds = num % 10; - - num = (num - hundreds) / 10; - for (int n = 0; n < num; n++) { - Roman += "M"; - } - - if (hundreds >= 1) { - Roman += hundredsArray[hundreds - 1]; - } - - if (tens >= 1) { - Roman += tensArray[tens - 1]; - } - - if (ones >= 1) { - Roman += onesArray[ones - 1]; + final int l = map.floorKey(num); + if (num == l) { + listIterator.set(map.get(num)); + } else { + listIterator.set(map.get(l) + toRoman(num - l)); } - - numArray[i] = String.valueOf(Roman); } } - public static void printResult() { - for (int i = 0; i < numArray.length; i++) { - System.out.println(numArray[i]); - } - + public static void printResult(List numbersList) { + numbersList.forEach(System.out::println); } public static void runTest() { - numArray = new String[5]; - - numArray[0] = "1"; - numArray[1] = "12"; - numArray[2] = "103"; + final List numbersList = new ArrayList<>(); - numArray[3] = "10char3"; - numArray[4] = "10399911"; + numbersList.add("1"); + numbersList.add("12"); + numbersList.add("103"); + numbersList.add("10char3"); + numbersList.add("10399911"); - validate(); + validate(numbersList); - convert(); + convert(numbersList); - System.out.print("1 => " + numArray[0]); - if (ONE.equals(numArray[0])) { + System.out.print("1 => " + numbersList.get(0)); + if (ONE.equals(numbersList.get(0))) { System.out.println(" passed."); } else { System.out.println(" failed."); } - System.out.print("12 => " + numArray[1]); - if (TWELVE.equals(numArray[1])) { + System.out.print("12 => " + numbersList.get(1)); + if (TWELVE.equals(numbersList.get(1))) { System.out.println(" passed."); } else { System.out.println(" failed."); } - System.out.print("103 => " + numArray[2]); - if (ONE_HUNDRED_AND_THREE.equals(numArray[2])) { + System.out.print("103 => " + numbersList.get(2)); + if (ONE_HUNDRED_AND_THREE.equals(numbersList.get(2))) { System.out.println(" passed."); } else { System.out.println(" failed."); } - System.out.print("10char3 => " + numArray[3]); - if ("10char3, illegal argument error - is not valid digital number.".equals(numArray[3])) { + System.out.print("10char3 => " + numbersList.get(3)); + if ("10char3, illegal argument error - is not valid digital number.".equals(numbersList.get(3))) { System.out.println(" passed."); } else { System.out.println(" failed."); } - System.out.print("10399911 => " + numArray[4]); - if ("10399911, range bound error - number should be in the range [1 - 3999].".equals(numArray[4])) { + System.out.print("10399911 => " + numbersList.get(4)); + if ("10399911, range bound error - number should be in the range [1 - 3999].".equals(numbersList.get(4))) { System.out.println(" passed."); } else { System.out.println(" failed.");