Skip to content

Commit 7256070

Browse files
authored
Merge branch 'master' into refactor/Anagrams
2 parents dd5d7ef + 7e9cdad commit 7256070

File tree

8 files changed

+150
-77
lines changed

8 files changed

+150
-77
lines changed
Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
package com.thealgorithms.others;
22

3-
/*
4-
* A left rotation operation on an array
5-
* shifts each of the array's elements
6-
* given integer n unit to the left.
3+
/**
4+
* Provides a method to perform a left rotation on an array.
5+
* A left rotation operation shifts each element of the array
6+
* by a specified number of positions to the left.
77
*
88
* @author sangin-lee
99
*/
10-
1110
public final class ArrayLeftRotation {
1211
private ArrayLeftRotation() {
1312
}
1413

15-
/*
16-
* Returns the result of left rotation of given array arr and integer n
17-
*
18-
* @param arr : int[] given array
19-
*
20-
* @param n : int given integer
14+
/**
15+
* Performs a left rotation on the given array by the specified number of positions.
2116
*
22-
* @return : int[] result of left rotation
17+
* @param arr the array to be rotated
18+
* @param n the number of positions to rotate the array to the left
19+
* @return a new array containing the elements of the input array rotated to the left
2320
*/
2421
public static int[] rotateLeft(int[] arr, int n) {
2522
int size = arr.length;
26-
int[] dst = new int[size];
23+
24+
// Handle cases where array is empty or rotation count is zero
25+
if (size == 0 || n <= 0) {
26+
return arr.clone();
27+
}
28+
29+
// Normalize the number of rotations
2730
n = n % size;
31+
if (n == 0) {
32+
return arr.clone();
33+
}
34+
35+
int[] rotated = new int[size];
36+
37+
// Perform rotation
2838
for (int i = 0; i < size; i++) {
29-
dst[i] = arr[n];
30-
n = (n + 1) % size;
39+
rotated[i] = arr[(i + n) % size];
3140
}
32-
return dst;
41+
42+
return rotated;
3343
}
3444
}

src/main/java/com/thealgorithms/stacks/BalancedBrackets.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,22 @@ public static boolean isBalanced(String brackets) {
5959
switch (bracket) {
6060
case '(':
6161
case '[':
62+
case '<':
6263
case '{':
6364
bracketsStack.push(bracket);
6465
break;
6566
case ')':
6667
case ']':
68+
case '>':
6769
case '}':
6870
if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
6971
return false;
7072
}
7173
break;
7274
default:
73-
/* other character is invalid */
7475
return false;
7576
}
7677
}
7778
return bracketsStack.isEmpty();
7879
}
79-
80-
public static void main(String[] args) {
81-
assert isBalanced("[()]{}{[()()]()}");
82-
assert !isBalanced("[(])");
83-
}
8480
}

src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,33 @@ public final class DecimalToAnyUsingStack {
66
private DecimalToAnyUsingStack() {
77
}
88

9-
public static void main(String[] args) {
10-
assert convert(0, 2).equals("0");
11-
assert convert(30, 2).equals("11110");
12-
assert convert(30, 8).equals("36");
13-
assert convert(30, 10).equals("30");
14-
assert convert(30, 16).equals("1E");
15-
}
16-
179
/**
18-
* Convert decimal number to another radix
10+
* Convert a decimal number to another radix.
1911
*
2012
* @param number the number to be converted
2113
* @param radix the radix
22-
* @return another radix
23-
* @throws ArithmeticException if <tt>number</tt> or <tt>radius</tt> is
24-
* invalid
14+
* @return the number represented in the new radix as a String
15+
* @throws IllegalArgumentException if <tt>number</tt> is negative or <tt>radix</tt> is not between 2 and 16 inclusive
2516
*/
26-
private static String convert(int number, int radix) {
17+
public static String convert(int number, int radix) {
18+
if (number < 0) {
19+
throw new IllegalArgumentException("Number must be non-negative.");
20+
}
2721
if (radix < 2 || radix > 16) {
28-
throw new ArithmeticException(String.format("Invalid input -> number:%d,radius:%d", number, radix));
22+
throw new IllegalArgumentException(String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix));
23+
}
24+
25+
if (number == 0) {
26+
return "0";
2927
}
30-
char[] tables = {
31-
'0',
32-
'1',
33-
'2',
34-
'3',
35-
'4',
36-
'5',
37-
'6',
38-
'7',
39-
'8',
40-
'9',
41-
'A',
42-
'B',
43-
'C',
44-
'D',
45-
'E',
46-
'F',
47-
};
28+
29+
char[] tables = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
30+
4831
Stack<Character> bits = new Stack<>();
49-
do {
32+
while (number > 0) {
5033
bits.push(tables[number % radix]);
5134
number = number / radix;
52-
} while (number != 0);
35+
}
5336

5437
StringBuilder result = new StringBuilder();
5538
while (!bits.isEmpty()) {

src/main/java/com/thealgorithms/strings/CheckVowels.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.strings;
22

3-
import java.util.Arrays;
4-
import java.util.HashSet;
53
import java.util.Set;
64

75
/**
@@ -10,24 +8,24 @@
108
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
119
*/
1210
public final class CheckVowels {
11+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u');
12+
1313
private CheckVowels() {
1414
}
1515

16-
private static final Set<Character> VOWELS = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
17-
1816
/**
19-
* Check if a string is has vowels or not
17+
* Checks if a string contains any vowels.
2018
*
21-
* @param input a string
22-
* @return {@code true} if given string has vowels, otherwise {@code false}
19+
* @param input a string to check
20+
* @return {@code true} if the given string contains at least one vowel, otherwise {@code false}
2321
*/
2422
public static boolean hasVowels(String input) {
25-
if (input == null) {
23+
if (input == null || input.isEmpty()) {
2624
return false;
2725
}
28-
input = input.toLowerCase();
29-
for (int i = 0; i < input.length(); i++) {
30-
if (VOWELS.contains(input.charAt(i))) {
26+
27+
for (char c : input.toLowerCase().toCharArray()) {
28+
if (VOWELS.contains(c)) {
3129
return true;
3230
}
3331
}

src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ void testForHigherSizeStep() {
4444
int[] result = ArrayLeftRotation.rotateLeft(arr, n);
4545
assertArrayEquals(expected, result);
4646
}
47+
48+
@Test
49+
void testForEmptyArray() {
50+
int[] arr = {};
51+
int[] result = ArrayLeftRotation.rotateLeft(arr, 3);
52+
assertArrayEquals(arr, result);
53+
}
4754
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.CsvSource;
11+
12+
class BalancedBracketsTest {
13+
14+
@ParameterizedTest
15+
@CsvSource({"(, )", "[, ]", "{, }", "<, >"})
16+
void testIsPairedTrue(char opening, char closing) {
17+
assertTrue(BalancedBrackets.isPaired(opening, closing));
18+
}
19+
20+
@ParameterizedTest
21+
@CsvSource({"(, ]", "[, )", "{, >", "<, )", "a, b", "!, @"})
22+
void testIsPairedFalse(char opening, char closing) {
23+
assertFalse(BalancedBrackets.isPaired(opening, closing));
24+
}
25+
26+
@ParameterizedTest
27+
@CsvSource({"'[()]{}{[()()]()}', true", "'()', true", "'[]', true", "'{}', true", "'<>', true", "'[{<>}]', true", "'', true", "'[(])', false", "'([)]', false", "'{[<]>}', false", "'[', false", "')', false", "'[{', false", "']', false", "'[a+b]', false", "'a+b', false"})
28+
void testIsBalanced(String input, boolean expected) {
29+
assertEquals(expected, BalancedBrackets.isBalanced(input));
30+
}
31+
32+
@Test
33+
void testIsBalancedNull() {
34+
assertThrows(IllegalArgumentException.class, () -> BalancedBrackets.isBalanced(null));
35+
}
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class DecimalToAnyUsingStackTest {
9+
10+
@Test
11+
void testConvertToBinary() {
12+
assertEquals("0", DecimalToAnyUsingStack.convert(0, 2));
13+
assertEquals("11110", DecimalToAnyUsingStack.convert(30, 2));
14+
}
15+
16+
@Test
17+
void testConvertToOctal() {
18+
assertEquals("36", DecimalToAnyUsingStack.convert(30, 8));
19+
}
20+
21+
@Test
22+
void testConvertToDecimal() {
23+
assertEquals("30", DecimalToAnyUsingStack.convert(30, 10));
24+
}
25+
26+
@Test
27+
void testConvertToHexadecimal() {
28+
assertEquals("1E", DecimalToAnyUsingStack.convert(30, 16));
29+
}
30+
31+
@Test
32+
void testInvalidRadix() {
33+
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 1));
34+
assertEquals("Invalid radix: 1. Radix must be between 2 and 16.", thrown.getMessage());
35+
36+
thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(30, 17));
37+
assertEquals("Invalid radix: 17. Radix must be between 2 and 16.", thrown.getMessage());
38+
}
39+
40+
@Test
41+
void testNegativeNumber() {
42+
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> DecimalToAnyUsingStack.convert(-30, 2));
43+
assertEquals("Number must be non-negative.", thrown.getMessage());
44+
}
45+
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
54

6-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
77

8-
public class CheckVowelsTest {
8+
class CheckVowelsTest {
99

10-
@Test
11-
public void isVowel() {
12-
assertTrue(CheckVowels.hasVowels("foo"));
13-
assertTrue(CheckVowels.hasVowels("bar"));
14-
assertFalse(CheckVowels.hasVowels("why"));
15-
assertFalse(CheckVowels.hasVowels("myths"));
10+
@ParameterizedTest
11+
@CsvSource({"'foo', true", "'bar', true", "'why', false", "'myths', false", "'', false", "'AEIOU', true", "'bcdfghjklmnpqrstvwxyz', false", "'AeIoU', true"})
12+
void testHasVowels(String input, boolean expected) {
13+
assertEquals(CheckVowels.hasVowels(input), expected);
1614
}
1715
}

0 commit comments

Comments
 (0)