Skip to content

Commit 0c90a96

Browse files
authored
Merge branch 'master' into avl_improve
2 parents ce975a4 + e94be71 commit 0c90a96

File tree

6 files changed

+250
-2
lines changed

6 files changed

+250
-2
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@
466466
* [SumWithoutArithmeticOperators](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SumWithoutArithmeticOperators.java)
467467
* [TrinomialTriangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java)
468468
* [TwinPrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/TwinPrime.java)
469+
* [UniformNumbers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/UniformNumbers.java)
469470
* [VampireNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/VampireNumber.java)
470471
* [VectorCrossProduct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java)
471472
* [Volume](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Volume.java)
@@ -559,6 +560,7 @@
559560
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
560561
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
561562
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
563+
* [RandomScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RandomScheduling.java)
562564
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
563565
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
564566
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -1121,6 +1123,7 @@
11211123
* [SumWithoutArithmeticOperatorsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java)
11221124
* [TestArmstrong](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TestArmstrong.java)
11231125
* [TwinPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java)
1126+
* [UniformNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/UniformNumbersTest.java)
11241127
* [VolumeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/VolumeTest.java)
11251128
* misc
11261129
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
@@ -1191,6 +1194,7 @@
11911194
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
11921195
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
11931196
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
1197+
* [RandomSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RandomSchedulingTest.java)
11941198
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
11951199
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
11961200
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<dependency>
126126
<groupId>com.puppycrawl.tools</groupId>
127127
<artifactId>checkstyle</artifactId>
128-
<version>10.18.2</version>
128+
<version>10.19.0</version>
129129
</dependency>
130130
</dependencies>
131131
</plugin>
@@ -153,7 +153,7 @@
153153
<plugin>
154154
<groupId>org.apache.maven.plugins</groupId>
155155
<artifactId>maven-pmd-plugin</artifactId>
156-
<version>3.25.0</version>
156+
<version>3.26.0</version>
157157
<configuration>
158158
<printFailingErrors>true</printFailingErrors>
159159
<includeTests>true</includeTests>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* A positive integer is considered uniform if all
5+
* of its digits are equal. For example, 222 is uniform,
6+
* while 223 is not.
7+
* Given two positive integers a and b, determine the
8+
* number of uniform integers between a and b.
9+
*/
10+
public final class UniformNumbers {
11+
// Private constructor to prevent instantiation of the utility class
12+
private UniformNumbers() {
13+
// Prevent instantiation
14+
}
15+
/**
16+
* This function will find the number of uniform numbers
17+
* from 1 to num
18+
* @param num upper limit to find the uniform numbers
19+
* @return the count of uniform numbers between 1 and num
20+
*/
21+
public static int uniformNumbers(int num) {
22+
String numStr = Integer.toString(num);
23+
int uniformCount = (numStr.length() - 1) * 9;
24+
int finalUniform = Integer.parseInt(String.valueOf(numStr.charAt(0)).repeat(numStr.length()));
25+
26+
if (finalUniform <= num) {
27+
uniformCount += Integer.parseInt(String.valueOf(numStr.charAt(0)));
28+
} else {
29+
uniformCount += Integer.parseInt(String.valueOf(numStr.charAt(0))) - 1;
30+
}
31+
32+
return uniformCount;
33+
}
34+
/**
35+
* This function will calculate the number of uniform numbers
36+
* between a and b
37+
* @param a lower bound of range
38+
* @param b upper bound of range
39+
* @return the count of uniform numbers between a and b
40+
*/
41+
public static int countUniformIntegers(int a, int b) {
42+
if (b > a && b > 0 && a > 0) {
43+
return uniformNumbers(b) - uniformNumbers(a - 1);
44+
} else if (b == a) {
45+
return 1;
46+
} else {
47+
return 0;
48+
}
49+
}
50+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
/**
10+
* RandomScheduling is an algorithm that assigns tasks in a random order.
11+
* It doesn't consider priority, deadlines, or burst times, making it
12+
* inefficient but useful in scenarios where fairness or unpredictability
13+
* is required (e.g., load balancing in distributed systems).
14+
*
15+
* Use Case: Distributed systems where randomness helps avoid task starvation.
16+
*
17+
* @author Hardvan
18+
*/
19+
public final class RandomScheduling {
20+
21+
private final List<String> tasks;
22+
private final Random random;
23+
24+
/**
25+
* Constructs a new RandomScheduling instance.
26+
*
27+
* @param tasks A collection of task names to be scheduled.
28+
* @param random A Random instance for generating random numbers.
29+
*/
30+
public RandomScheduling(Collection<String> tasks, Random random) {
31+
this.tasks = new ArrayList<>(tasks);
32+
this.random = random;
33+
}
34+
35+
/**
36+
* Schedules the tasks randomly and returns the randomized order.
37+
*
38+
* @return A list representing the tasks in their randomized execution order.
39+
*/
40+
public List<String> schedule() {
41+
List<String> shuffledTasks = new ArrayList<>(tasks);
42+
Collections.shuffle(shuffledTasks, random);
43+
return shuffledTasks;
44+
}
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class UniformNumbersTest {
8+
9+
@Test
10+
void testSingleUniformDigitRange() {
11+
assertEquals(1, UniformNumbers.countUniformIntegers(1, 1));
12+
assertEquals(9, UniformNumbers.countUniformIntegers(1, 9));
13+
}
14+
15+
@Test
16+
void testSmallRange() {
17+
assertEquals(1, UniformNumbers.countUniformIntegers(10, 11));
18+
assertEquals(2, UniformNumbers.countUniformIntegers(22, 33));
19+
}
20+
21+
@Test
22+
void testRangeWithNoUniformNumbers() {
23+
assertEquals(0, UniformNumbers.countUniformIntegers(12, 21));
24+
assertEquals(0, UniformNumbers.countUniformIntegers(123, 128));
25+
}
26+
27+
@Test
28+
void testRangeWithAllUniformNumbers() {
29+
assertEquals(9, UniformNumbers.countUniformIntegers(1, 9));
30+
assertEquals(18, UniformNumbers.countUniformIntegers(1, 99));
31+
}
32+
33+
@Test
34+
void testMultiDigitRangeWithUniformNumbers() {
35+
assertEquals(1, UniformNumbers.countUniformIntegers(100, 111));
36+
assertEquals(2, UniformNumbers.countUniformIntegers(111, 222));
37+
}
38+
39+
@Test
40+
void testExactUniformBoundary() {
41+
assertEquals(1, UniformNumbers.countUniformIntegers(111, 111));
42+
assertEquals(2, UniformNumbers.countUniformIntegers(111, 222));
43+
}
44+
45+
@Test
46+
void testLargeRange() {
47+
assertEquals(27, UniformNumbers.countUniformIntegers(1, 999));
48+
assertEquals(36, UniformNumbers.countUniformIntegers(1, 9999));
49+
}
50+
51+
@Test
52+
void testInvalidRange() {
53+
assertEquals(0, UniformNumbers.countUniformIntegers(500, 100));
54+
assertEquals(0, UniformNumbers.countUniformIntegers(-100, -1));
55+
}
56+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.Mockito.anyInt;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import java.util.List;
9+
import java.util.Random;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class RandomSchedulingTest {
14+
15+
private RandomScheduling randomScheduling;
16+
private Random mockRandom;
17+
18+
@BeforeEach
19+
public void setup() {
20+
mockRandom = mock(Random.class); // Mocking Random for predictable behavior
21+
}
22+
23+
@Test
24+
public void testRandomOrder1() {
25+
// Arrange
26+
List<String> tasks = List.of("Task1", "Task2", "Task3");
27+
// Mock the random sequence to control shuffling: swap 0 <-> 1, and 1 <-> 2.
28+
when(mockRandom.nextInt(anyInt())).thenReturn(1, 2, 0);
29+
randomScheduling = new RandomScheduling(tasks, mockRandom);
30+
31+
// Act
32+
List<String> result = randomScheduling.schedule();
33+
34+
// Assert
35+
assertEquals(List.of("Task1", "Task2", "Task3"), result);
36+
}
37+
38+
@Test
39+
public void testRandomOrder2() {
40+
// Arrange
41+
List<String> tasks = List.of("A", "B", "C", "D");
42+
// Mocking predictable swaps for the sequence: [C, B, D, A]
43+
when(mockRandom.nextInt(anyInt())).thenReturn(2, 1, 3, 0);
44+
randomScheduling = new RandomScheduling(tasks, mockRandom);
45+
46+
// Act
47+
List<String> result = randomScheduling.schedule();
48+
49+
// Assert
50+
assertEquals(List.of("A", "C", "B", "D"), result);
51+
}
52+
53+
@Test
54+
public void testSingleTask() {
55+
// Arrange
56+
List<String> tasks = List.of("SingleTask");
57+
when(mockRandom.nextInt(anyInt())).thenReturn(0); // No real shuffle
58+
randomScheduling = new RandomScheduling(tasks, mockRandom);
59+
60+
// Act
61+
List<String> result = randomScheduling.schedule();
62+
63+
// Assert
64+
assertEquals(List.of("SingleTask"), result);
65+
}
66+
67+
@Test
68+
public void testEmptyTaskList() {
69+
// Arrange
70+
List<String> tasks = List.of();
71+
randomScheduling = new RandomScheduling(tasks, mockRandom);
72+
73+
// Act
74+
List<String> result = randomScheduling.schedule();
75+
76+
// Assert
77+
assertEquals(List.of(), result); // Should return an empty list
78+
}
79+
80+
@Test
81+
public void testSameTasksMultipleTimes() {
82+
// Arrange
83+
List<String> tasks = List.of("X", "X", "Y", "Z");
84+
when(mockRandom.nextInt(anyInt())).thenReturn(3, 0, 1, 2);
85+
randomScheduling = new RandomScheduling(tasks, mockRandom);
86+
87+
// Act
88+
List<String> result = randomScheduling.schedule();
89+
90+
// Assert
91+
assertEquals(List.of("Y", "X", "X", "Z"), result);
92+
}
93+
}

0 commit comments

Comments
 (0)