Skip to content

Commit cbc1899

Browse files
authored
Merge pull request TheAlgorithms#1145 from shellhub/dev
update AnyBaseToDecimal and optimization
2 parents 32e4987 + 064a84f commit cbc1899

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

Conversions/AnyBaseToDecimal.java

+28-36
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,53 @@
11
package Conversions;
22

3-
import java.io.BufferedReader;
4-
import java.io.InputStreamReader;
5-
63
/**
7-
*
84
* @author Varun Upadhyay (https://github.com/varunu28)
9-
*
105
*/
116

127
// Driver program
138
public class AnyBaseToDecimal {
14-
public static void main (String[] args) throws Exception{
15-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16-
17-
String inp = br.readLine();
18-
int base = Integer.parseInt(br.readLine());
19-
20-
System.out.println("Input in base " + base + " is: " + inp);
21-
System.out.println("Decimal value of " + inp + " is: " + convertToDecimal(inp, base));
22-
23-
br.close();
9+
public static void main(String[] args) {
10+
assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2);
11+
assert convertToDecimal("777", 8) == Integer.valueOf("777", 8);
12+
assert convertToDecimal("999", 10) == Integer.valueOf("999", 10);
13+
assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16);
14+
assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36);
2415
}
2516

2617
/**
27-
* This method produces a decimal value of any given input number of any base
28-
* @param inp_num String of which we need the decimal value and base in integer format
29-
* @return string format of the decimal value
18+
* Convert any radix to decimal number
19+
*
20+
* @param s the string to be convert
21+
* @param radix the radix
22+
* @return decimal of bits
23+
* @throws NumberFormatException if {@code bits} or {@code radix} is invalid
3024
*/
31-
32-
public static String convertToDecimal(String inp_num, int base) {
33-
int len = inp_num.length();
25+
public static int convertToDecimal(String s, int radix) {
3426
int num = 0;
3527
int pow = 1;
3628

37-
for (int i=len-1; i>=0; i--) {
38-
if (valOfChar(inp_num.charAt(i)) >= base) {
39-
return "Invalid Number";
29+
for (int i = s.length() - 1; i >= 0; i--) {
30+
int digit = valOfChar(s.charAt(i));
31+
if (digit >= radix) {
32+
throw new NumberFormatException("For input string " + s);
4033
}
41-
num += valOfChar(inp_num.charAt(i))*pow;
42-
pow *= base;
34+
num += valOfChar(s.charAt(i)) * pow;
35+
pow *= radix;
4336
}
44-
return String.valueOf(num);
37+
return num;
4538
}
4639

4740
/**
48-
* This method produces integer value of the input character and returns it
49-
* @param c Char of which we need the integer value of
50-
* @return integer value of input char
41+
* Convert character to integer
42+
*
43+
* @param c the character
44+
* @return represented digit of given character
45+
* @throws NumberFormatException if {@code ch} is not UpperCase or Digit character.
5146
*/
52-
5347
public static int valOfChar(char c) {
54-
if (c >= '0' && c <= '9') {
55-
return (int)c - '0';
56-
}
57-
else {
58-
return (int)c - 'A' + 10;
48+
if (!(Character.isUpperCase(c) || Character.isDigit(c))) {
49+
throw new NumberFormatException("invalid character :" + c);
5950
}
51+
return Character.isDigit(c) ? c - '0' : c - 'A' + 10;
6052
}
6153
}

DataStructures/Lists/SinglyLinkedList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void deleteNth(int position) {
134134
* @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high}
135135
*/
136136
public void checkBounds(int position, int low, int high) {
137-
if (position < low || position > high) {
137+
if (position > high || position < low) {
138138
throw new IndexOutOfBoundsException(position + "");
139139
}
140140
}

DataStructures/Stacks/BalancedBrackets.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static boolean isBalanced(String brackets) {
6262
case ')':
6363
case ']':
6464
case '}':
65-
if (!(!bracketsStack.isEmpty() && isPaired(bracketsStack.pop(), bracket))) {
65+
if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
6666
return false;
6767
}
6868
break;

0 commit comments

Comments
 (0)