Merge pull request #1145 from shellhub/dev

update AnyBaseToDecimal and optimization
This commit is contained in:
Yang Libin 2019-10-28 14:08:08 +08:00 committed by GitHub
commit cbc1899b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 38 deletions

View File

@ -1,61 +1,53 @@
package Conversions; package Conversions;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/** /**
*
* @author Varun Upadhyay (https://github.com/varunu28) * @author Varun Upadhyay (https://github.com/varunu28)
*
*/ */
// Driver program // Driver program
public class AnyBaseToDecimal { public class AnyBaseToDecimal {
public static void main (String[] args) throws Exception{ public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2);
assert convertToDecimal("777", 8) == Integer.valueOf("777", 8);
String inp = br.readLine(); assert convertToDecimal("999", 10) == Integer.valueOf("999", 10);
int base = Integer.parseInt(br.readLine()); assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16);
assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36);
System.out.println("Input in base " + base + " is: " + inp);
System.out.println("Decimal value of " + inp + " is: " + convertToDecimal(inp, base));
br.close();
} }
/** /**
* This method produces a decimal value of any given input number of any base * Convert any radix to decimal number
* @param inp_num String of which we need the decimal value and base in integer format *
* @return string format of the decimal value * @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 int convertToDecimal(String s, int radix) {
public static String convertToDecimal(String inp_num, int base) {
int len = inp_num.length();
int num = 0; int num = 0;
int pow = 1; int pow = 1;
for (int i=len-1; i>=0; i--) { for (int i = s.length() - 1; i >= 0; i--) {
if (valOfChar(inp_num.charAt(i)) >= base) { int digit = valOfChar(s.charAt(i));
return "Invalid Number"; if (digit >= radix) {
throw new NumberFormatException("For input string " + s);
} }
num += valOfChar(inp_num.charAt(i))*pow; num += valOfChar(s.charAt(i)) * pow;
pow *= base; pow *= radix;
} }
return String.valueOf(num); return num;
} }
/** /**
* This method produces integer value of the input character and returns it * Convert character to integer
* @param c Char of which we need the integer value of *
* @return integer value of input char * @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) { public static int valOfChar(char c) {
if (c >= '0' && c <= '9') { if (!(Character.isUpperCase(c) || Character.isDigit(c))) {
return (int)c - '0'; throw new NumberFormatException("invalid character :" + c);
}
else {
return (int)c - 'A' + 10;
} }
return Character.isDigit(c) ? c - '0' : c - 'A' + 10;
} }
} }

View File

@ -134,7 +134,7 @@ public class SinglyLinkedList {
* @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high} * @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high}
*/ */
public void checkBounds(int position, int low, int high) { public void checkBounds(int position, int low, int high) {
if (position < low || position > high) { if (position > high || position < low) {
throw new IndexOutOfBoundsException(position + ""); throw new IndexOutOfBoundsException(position + "");
} }
} }

View File

@ -62,7 +62,7 @@ class BalancedBrackets {
case ')': case ')':
case ']': case ']':
case '}': case '}':
if (!(!bracketsStack.isEmpty() && isPaired(bracketsStack.pop(), bracket))) { if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
return false; return false;
} }
break; break;