From 28cb69ed68b52fd11e768ab3d8d12cc14e62f65a Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 27 Oct 2019 17:48:30 +0800 Subject: [PATCH 1/3] update AnyBaseToDecimal --- Conversions/AnyBaseToDecimal.java | 64 ++++++++++++++----------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/Conversions/AnyBaseToDecimal.java b/Conversions/AnyBaseToDecimal.java index 46451bb8..6b4a680a 100644 --- a/Conversions/AnyBaseToDecimal.java +++ b/Conversions/AnyBaseToDecimal.java @@ -1,61 +1,53 @@ package Conversions; -import java.io.BufferedReader; -import java.io.InputStreamReader; - /** - * * @author Varun Upadhyay (https://github.com/varunu28) - * */ // Driver program public class AnyBaseToDecimal { - public static void main (String[] args) throws Exception{ - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - - String inp = br.readLine(); - int base = Integer.parseInt(br.readLine()); - - System.out.println("Input in base " + base + " is: " + inp); - System.out.println("Decimal value of " + inp + " is: " + convertToDecimal(inp, base)); - - br.close(); + public static void main(String[] args) { + assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2); + assert convertToDecimal("777", 8) == Integer.valueOf("777", 8); + assert convertToDecimal("999", 10) == Integer.valueOf("999", 10); + assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16); + assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36); } /** - * This method produces a decimal value of any given input number of any base - * @param inp_num String of which we need the decimal value and base in integer format - * @return string format of the decimal value + * Convert any radix to decimal number + * + * @param s the string to be convert + * @param radix the radix + * @return decimal of bits + * @throws NumberFormatException if {@code bits} or {@code radix} is invalid */ - - public static String convertToDecimal(String inp_num, int base) { - int len = inp_num.length(); + public static int convertToDecimal(String s, int radix) { int num = 0; int pow = 1; - for (int i=len-1; i>=0; i--) { - if (valOfChar(inp_num.charAt(i)) >= base) { - return "Invalid Number"; + for (int i = s.length() - 1; i >= 0; i--) { + int digit = valOfChar(s.charAt(i)); + if (digit >= radix) { + throw new NumberFormatException("For input string " + s); } - num += valOfChar(inp_num.charAt(i))*pow; - pow *= base; + num += valOfChar(s.charAt(i)) * pow; + pow *= radix; } - return String.valueOf(num); + return num; } /** - * This method produces integer value of the input character and returns it - * @param c Char of which we need the integer value of - * @return integer value of input char + * Convert character to integer + * + * @param c the character + * @return represented digit of given character + * @throws NumberFormatException if {@code ch} is not UpperCase or Digit character. */ - public static int valOfChar(char c) { - if (c >= '0' && c <= '9') { - return (int)c - '0'; - } - else { - return (int)c - 'A' + 10; + if (!(Character.isUpperCase(c) || Character.isDigit(c))) { + throw new NumberFormatException("invalid character :" + c); } + return Character.isDigit(c) ? c - '0' : c - 'A' + 10; } } From 87fcd2c8a425af324137a0d1308c843b8c1bfba4 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 27 Oct 2019 17:56:39 +0800 Subject: [PATCH 2/3] make code readable --- DataStructures/Stacks/BalancedBrackets.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStructures/Stacks/BalancedBrackets.java b/DataStructures/Stacks/BalancedBrackets.java index ea342219..d9f6aa0a 100644 --- a/DataStructures/Stacks/BalancedBrackets.java +++ b/DataStructures/Stacks/BalancedBrackets.java @@ -62,7 +62,7 @@ class BalancedBrackets { case ')': case ']': case '}': - if (!(!bracketsStack.isEmpty() && isPaired(bracketsStack.pop(), bracket))) { + if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) { return false; } break; From 064a84f971ffc34dd88752c04f2dcfe0f1afcdb9 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 27 Oct 2019 18:04:12 +0800 Subject: [PATCH 3/3] statistics optimization --- DataStructures/Lists/SinglyLinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStructures/Lists/SinglyLinkedList.java b/DataStructures/Lists/SinglyLinkedList.java index a478d7a5..04f904f9 100644 --- a/DataStructures/Lists/SinglyLinkedList.java +++ b/DataStructures/Lists/SinglyLinkedList.java @@ -106,7 +106,7 @@ public class SinglyLinkedList { * @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high} */ public void checkBounds(int position, int low, int high) { - if (position < low || position > high) { + if (position > high || position < low) { throw new IndexOutOfBoundsException(position + ""); } }