Skip to content

Commit 8acb4dd

Browse files
authored
Merge branch 'master' into refactor/BalancedBrackets
2 parents ac75d1e + a5f57fb commit 8acb4dd

File tree

6 files changed

+130
-83
lines changed

6 files changed

+130
-83
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/others/EulersFunction.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package com.thealgorithms.others;
22

33
/**
4-
* @brief utility class for <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>
4+
* Utility class for computing
5+
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
56
*/
67
public final class EulersFunction {
78
private EulersFunction() {
89
}
910

11+
/**
12+
* Validates that the input is a positive integer.
13+
*
14+
* @param n the input number to validate
15+
* @throws IllegalArgumentException if {@code n} is non-positive
16+
*/
1017
private static void checkInput(int n) {
1118
if (n <= 0) {
1219
throw new IllegalArgumentException("n must be positive.");
1320
}
1421
}
1522

1623
/**
17-
* @brief computes the value of Euler's totient function for given input
18-
* @details has time complexity of O(sqrt(n))
19-
* @param n the input
20-
* @exception IllegalArgumentException n is non-positive
21-
* @return the value of Euler's totient function for the input
24+
* Computes the value of Euler's totient function for a given input.
25+
* This function has a time complexity of O(sqrt(n)).
26+
*
27+
* @param n the input number
28+
* @return the value of Euler's totient function for the given input
29+
* @throws IllegalArgumentException if {@code n} is non-positive
2230
*/
2331
public static int getEuler(int n) {
2432
checkInput(n);

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/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
}

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,31 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.util.HashMap;
7-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
810

911
class EulersFunctionTest {
10-
@Test
11-
public void testGetEuler() {
12-
HashMap<Integer, Integer> testCases = new HashMap<>();
13-
testCases.put(1, 1);
14-
testCases.put(2, 1);
15-
testCases.put(3, 2);
16-
testCases.put(4, 2);
17-
testCases.put(5, 4);
18-
testCases.put(6, 2);
19-
testCases.put(10, 4);
20-
testCases.put(21, 12);
21-
testCases.put(69, 44);
22-
testCases.put(47, 46);
23-
testCases.put(46, 22);
24-
testCases.put(55, 40);
25-
testCases.put(34, 16);
26-
testCases.put(20, 8);
27-
testCases.put(20, 8);
28-
testCases.put(1024, 512);
2912

30-
for (final var tc : testCases.entrySet()) {
31-
assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey()));
32-
}
13+
@ParameterizedTest
14+
@MethodSource("provideNumbersForGetEuler")
15+
void testGetEuler(int input, int expected) {
16+
assertEquals(expected, EulersFunction.getEuler(input));
3317
}
3418

35-
@Test
36-
public void testGetEulerThrowsExceptionForNonPositiveInput() {
37-
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0));
19+
@ParameterizedTest
20+
@MethodSource("provideInvalidNumbersForGetEuler")
21+
void testGetEulerThrowsExceptionForNonPositiveInput(int input) {
22+
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(input));
23+
}
24+
25+
private static Stream<Arguments> provideNumbersForGetEuler() {
26+
return Stream.of(Arguments.of(1, 1), Arguments.of(2, 1), Arguments.of(3, 2), Arguments.of(4, 2), Arguments.of(5, 4), Arguments.of(6, 2), Arguments.of(10, 4), Arguments.of(21, 12), Arguments.of(69, 44), Arguments.of(47, 46), Arguments.of(46, 22), Arguments.of(55, 40), Arguments.of(34, 16),
27+
Arguments.of(20, 8), Arguments.of(1024, 512));
28+
}
29+
30+
private static Stream<Arguments> provideInvalidNumbersForGetEuler() {
31+
return Stream.of(Arguments.of(0), Arguments.of(-1), Arguments.of(-10));
3832
}
3933
}
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+
}

0 commit comments

Comments
 (0)