fix conflicts

This commit is contained in:
Dragomir Alin
2024-04-16 18:45:19 +03:00
712 changed files with 13187 additions and 2400 deletions
@@ -5,6 +5,18 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-miscellaneous-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<name>algorithms-miscellaneous-5</name>
<parent>
@@ -1,15 +1,12 @@
package com.baeldung.algorithms.conversion;
import java.math.BigInteger;
import com.google.common.io.BaseEncoding;
import jakarta.xml.bind.DatatypeConverter;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import com.google.common.io.BaseEncoding;
import jakarta.xml.bind.DatatypeConverter;
import java.math.BigInteger;
import java.util.HexFormat;
public class HexStringConverter {
@@ -109,4 +106,14 @@ public class HexStringConverter {
return BaseEncoding.base16()
.decode(hexString.toUpperCase());
}
public String encodeUsingHexFormat(byte[] bytes) {
HexFormat hexFormat = HexFormat.of();
return hexFormat.formatHex(bytes);
}
public byte[] decodeUsingHexFormat(String hexString) {
HexFormat hexFormat = HexFormat.of();
return hexFormat.parseHex(hexString);
}
}
@@ -1,15 +1,13 @@
package com.baeldung.algorithms.conversion;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.commons.codec.DecoderException;
import org.hamcrest.text.IsEqualIgnoringCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.conversion.HexStringConverter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ByteArrayConverterUnitTest {
@@ -24,7 +22,7 @@ class ByteArrayConverterUnitTest {
void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
if(hexString.charAt(0) == '0') {
if (hexString.charAt(0) == '0') {
hexString = hexString.substring(1);
}
String output = hexStringConverter.encodeUsingBigIntegerToString(bytes);
@@ -46,7 +44,7 @@ class ByteArrayConverterUnitTest {
byte[] output = hexStringConverter.decodeUsingBigInteger(hexString);
assertArrayEquals(bytes, output);
}
@Test
void shouldEncodeByteArrayToHexStringUsingCharacterConversion() {
byte[] bytes = getSampleBytes();
@@ -62,7 +60,7 @@ class ByteArrayConverterUnitTest {
byte[] output = hexStringConverter.decodeHexString(hexString);
assertArrayEquals(bytes, output);
}
@Test
void shouldDecodeHexToByteWithInvalidHexCharacter() {
assertThrows(IllegalArgumentException.class, () -> {
@@ -118,12 +116,28 @@ class ByteArrayConverterUnitTest {
assertArrayEquals(bytes, output);
}
@Test
void shouldEncodeByteArrayToHexStringUsingHexFormat() throws DecoderException {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
String output = hexStringConverter.encodeUsingHexFormat(bytes);
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
}
@Test
void shouldDecodeHexStringToByteArrayUsingHexFormat() throws DecoderException {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
byte[] output = hexStringConverter.decodeUsingHexFormat(hexString);
assertArrayEquals(bytes, output);
}
private String getSampleHexString() {
return "0af50c0e2d10";
}
private byte[] getSampleBytes() {
return new byte[] { 10, -11, 12, 14, 45, 16 };
return new byte[]{10, -11, 12, 14, 45, 16};
}
}
@@ -10,8 +10,4 @@
- [Check if Two Strings Are Rotations of Each Other](https://www.baeldung.com/java-string-check-strings-rotations)
- [Find the Largest Prime Under the Given Number in Java](https://www.baeldung.com/java-largest-prime-lower-threshold)
- [Count the Number of Unique Digits in an Integer using Java](https://www.baeldung.com/java-int-count-unique-digits)
- [Generate Juggler Sequence in Java](https://www.baeldung.com/java-generate-juggler-sequence)
- [Finding the Parent of a Node in a Binary Search Tree with Java](https://www.baeldung.com/java-find-parent-node-binary-search-tree)
- [Check if a Number Is a Happy Number in Java](https://www.baeldung.com/java-happy-sad-number-test)
- [Find the Largest Number Possible After Removing k Digits of a Number](https://www.baeldung.com/java-find-largest-number-remove-k-digits)
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)
@@ -13,4 +13,22 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<jmh.version>1.35</jmh.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,50 @@
package com.baeldung.algorithms.perfectnumber;
import java.util.stream.IntStream;
class PerfectNumber {
public static boolean isPerfectBruteForce(int number) {
int sum = 0;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
sum += i;
}
}
return sum == number;
}
public static boolean isPerfectStream(int number) {
int sum = IntStream.rangeClosed(2, (int) Math.sqrt(number))
.filter(test -> number % test == 0)
.reduce(1, (s, test) -> s + test + (number / test));
return sum == number;
}
public static boolean isPerfectEuclidEuler(int number) {
int p = 2;
int perfectNumber = (int) (Math.pow(2, p - 1) * (Math.pow(2, p) - 1));
while (perfectNumber <= number) {
if (perfectNumber == number) {
return true;
}
p++;
perfectNumber = (int) (Math.pow(2, p - 1) * (Math.pow(2, p) - 1));
}
return false;
}
public static boolean isPerfectEuclidEulerUsingShift(int number) {
int p = 2;
int perfectNumber = (2 << (p - 1)) * ((2 << p) - 1);
while (perfectNumber <= number) {
if (perfectNumber == number) {
return true;
}
p++;
perfectNumber = (2 << (p - 1)) * ((2 << p) - 1);
}
return false;
}
}
@@ -0,0 +1,41 @@
package com.baeldung.algorithms.perfectnumber;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@State(Scope.Benchmark)
public class PerfectNumberBenchmark {
@Benchmark
public boolean bruteForceBenchmark() {
return PerfectNumber.isPerfectBruteForce(33550336);
}
@Benchmark
public boolean streamBenchmark() {
return PerfectNumber.isPerfectStream(33550336);
}
@Benchmark
public boolean euclidEulerBenchmark() {
return PerfectNumber.isPerfectEuclidEuler(33550336);
}
@Benchmark
public boolean euclidEulerUsingShiftBenchmark() {
return PerfectNumber.isPerfectEuclidEulerUsingShift(33550336);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(PerfectNumberBenchmark.class.getSimpleName())
.forks(1)
.build();
new Runner(options).run();
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 160 KiB

@@ -1,2 +0,0 @@
## Relevant Articles
- [Implement Connect 4 Game with Java](https://www.baeldung.com/java-connect-4-game)
@@ -0,0 +1,68 @@
package com.baeldung.algorithms.perfectnumber;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PerfectNumberUnitTest {
@Test
void givenPerfectNumber_whenCheckingIsPerfectBruteForce_thenReturnTrue() {
assertTrue(PerfectNumber.isPerfectBruteForce(6));
}
@Test
void givenNonPerfectNumber_whenCheckingIsPerfectBruteForce_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectBruteForce(10));
}
@Test
void givenNegativeNumber_whenCheckingIsPerfectBruteForce_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectBruteForce(-28));
}
@Test
void givenPerfectNumber_whenCheckingIsPerfectStream_thenReturnTrue() {
assertTrue(PerfectNumber.isPerfectStream(28));
}
@Test
void givenNonPerfectNumber_whenCheckingIsPerfectStream_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectStream(10));
}
@Test
void givenNegativeNumber_whenCheckingIsPerfectStream_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectStream(-6));
}
@Test
void givenPerfectNumber_whenCheckingIsPerfectEuclidEuler_thenReturnTrue() {
assertTrue(PerfectNumber.isPerfectEuclidEuler(28));
}
@Test
void givenNonPerfectNumber_whenCheckingIsPerfectEuclidEuler_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectEuclidEuler(10));
}
@Test
void givenNegativeNumber_whenCheckingIsPerfectEuclidEuler_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectEuclidEuler(-6));
}
@Test
void givenPerfectNumber_whenCheckingIsPerfectEuclidEulerUsingShift_thenReturnTrue() {
assertTrue(PerfectNumber.isPerfectEuclidEulerUsingShift(28));
}
@Test
void givenNonPerfectNumber_whenCheckingIsPerfectEuclidEulerUsingShift_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectEuclidEulerUsingShift(10));
}
@Test
void givenNegativeNumber_whenCheckingIsPerfectEuclidEulerUsingShift_thenReturnFalse() {
assertFalse(PerfectNumber.isPerfectEuclidEulerUsingShift(-6));
}
}
@@ -1,2 +1,9 @@
### Relevant Articles:
- [Vigenère Cipher in Java](https://www.baeldung.com/java-vigenere-cipher)
- [Merge Overlapping Intervals in a Java Collection](https://www.baeldung.com/java-collection-merge-overlapping-intervals)
- [Generate Juggler Sequence in Java](https://www.baeldung.com/java-generate-juggler-sequence)
- [Finding the Parent of a Node in a Binary Search Tree with Java](https://www.baeldung.com/java-find-parent-node-binary-search-tree)
- [Check if a Number Is a Happy Number in Java](https://www.baeldung.com/java-happy-sad-number-test)
- [Find the Largest Number Possible After Removing k Digits of a Number](https://www.baeldung.com/java-find-largest-number-remove-k-digits)
- [Implement Connect 4 Game with Java](https://www.baeldung.com/java-connect-4-game)
- More articles: [[<-- prev]](/algorithms-miscellaneous-7)
@@ -0,0 +1,76 @@
package com.baeldung.algorithms.eastersunday;
import java.time.LocalDate;
public class EasterDateCalculator {
LocalDate computeEasterDateWithGaussAlgorithm(int year) {
int a = year % 19;
int b = year % 4;
int c = year % 7;
int k = year / 100;
int p = (13 + 8*k) / 25;
int q = k / 4;
int M = (15 - p + k - q) % 30;
int N = (4 + k - q) % 7;
int d = (19*a + M) % 30;
int e = (2*b + 4*c + 6*d + N) % 7;
if (d==29 && e == 6) {
return LocalDate.of(year, 4, 19);
} else if (d==28 && e==6 && ((11*M + 11)%30 < 10)) {
return LocalDate.of(year, 4, 18);
}
int H = 22 + d + e;
if (H <= 31) {
return LocalDate.of(year, 3, H);
}
return LocalDate.of(year, 4, H-31);
}
LocalDate computeEasterDateWithButcherMeeusAlgorithm(int year) {
int a = year % 19;
int b = year / 100;
int c = year % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19*a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int l = (2*e + 2*i - h - k + 32) % 7;
int m = (a + 11*h + 22*l) / 451;
int t = h + l - 7*m + 114;
int n = t / 31;
int o = t % 31;
return LocalDate.of(year, n, o+1);
}
LocalDate computeEasterDateWithConwayAlgorithm(int year) {
int s = year / 100;
int t = year % 100;
int a = t / 4;
int p = s % 4;
int x = (9 - 2*p) % 7;
int y = (x + t + a) % 7;
int g = year % 19;
int G = g + 1;
int b = s / 4;
int r = 8 * (s + 11) / 25;
int C = -s + b + r;
int d = (11*G + C) % 30;
d = (d + 30) % 30;
int h = (551 - 19*d + G) / 544;
int e = (50 - d - h) % 7;
int f = (e + y) % 7;
int R = 57 - d - f - h;
if (R <= 31) {
return LocalDate.of(year, 3, R);
}
return LocalDate.of(year, 4, R-31);
}
}
@@ -1,6 +1,6 @@
package com.baeldung.algorithms.largestNumberRemovingK;
import java.util.*;
import java.util.Stack;
public class LargestNumberRemoveKDigits {
public static int findLargestNumberUsingArithmetic(int num, int k) {
@@ -1,23 +1,22 @@
package com.baeldung.algorithms.mergeintervals;
import static java.lang.Integer.max;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class MergeOverlappingIntervals {
public List<Interval> doMerge(List<Interval> intervals) {
// Sort the intervals based on start time
intervals.sort((one, two) -> one.start - two.start);
// Create somewhere to put the merged list, start it off with the earliest starting interval
intervals.sort(Comparator.comparingInt(interval -> interval.start));
ArrayList<Interval> merged = new ArrayList<>();
merged.add(intervals.get(0));
// Loop over each interval and merge if start time is before the end time of the
// previous interval
intervals.forEach(interval -> {
if (merged.get(merged.size() - 1).end > interval.start) {
merged.get(merged.size() - 1).setEnd(interval.end);
Interval lastMerged = merged.get(merged.size() - 1);
if (interval.start <= lastMerged.end){
lastMerged.setEnd(max(interval.end, lastMerged.end));
} else {
merged.add(interval);
}
@@ -25,5 +24,4 @@ public class MergeOverlappingIntervals {
return merged;
}
}
}
@@ -0,0 +1,53 @@
package com.baeldung.algorithms.eastersunday;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalDate;
import org.junit.jupiter.api.Test;
public class EasterDateCalculatorUnitTest {
@Test
void givenEasterInMarch_whenComputeEasterDateWithGaussAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(2024, 3, 31), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(2024));
}
@Test
void givenEasterInApril_whenComputeEasterDateWithGaussAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(2004, 4, 11), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(2004));
}
@Test
void givenEdgeCases_whenComputeEasterDateWithGaussAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(1981, 4, 19), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(1981));
assertEquals(LocalDate.of(1954, 4, 18), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(1954));
}
@Test
void givenEasterInMarch_whenComputeEasterDateWithButcherMeeusAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(2024, 3, 31), new EasterDateCalculator().computeEasterDateWithButcherMeeusAlgorithm(2024));
}
@Test
void givenEasterInApril_whenComputeEasterDateWithButcherMeeusAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(2004, 4, 11), new EasterDateCalculator().computeEasterDateWithButcherMeeusAlgorithm(2004));
// The following are added just to notice that there is no need for a special case with this algorithm
assertEquals(LocalDate.of(1981, 4, 19), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(1981));
assertEquals(LocalDate.of(1954, 4, 18), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(1954));
}
@Test
void givenEasterInMarch_whenComputeEasterDateWithConwayAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(2024, 3, 31), new EasterDateCalculator().computeEasterDateWithConwayAlgorithm(2024));
}
@Test
void givenEasterInApril_whenComputeEasterDateWithConwayAlgorithm_thenCorrectResult() {
assertEquals(LocalDate.of(2004, 4, 11), new EasterDateCalculator().computeEasterDateWithConwayAlgorithm(2004));
// The following are added just to notice that there is no need for a special case with this algorithm
assertEquals(LocalDate.of(1981, 4, 19), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(1981));
assertEquals(LocalDate.of(1954, 4, 18), new EasterDateCalculator().computeEasterDateWithGaussAlgorithm(1954));
}
}
@@ -1,30 +1,38 @@
package com.baeldung.algorithms.mergeintervals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
class MergeIntervalsUnitTest {
private ArrayList<Interval> intervals = new ArrayList<>(Arrays.asList(
new Interval(2, 5),
new Interval(13, 20),
new Interval(11, 15),
new Interval(1, 3)
private List<Interval> intervals = new ArrayList<>(Arrays.asList(
// @formatter:off
new Interval(3, 5),
new Interval(13, 20),
new Interval(11, 15),
new Interval(15, 16),
new Interval(1, 3),
new Interval(4, 5),
new Interval(16, 17)
// @formatter:on
));
private ArrayList<Interval> intervalsMerged = new ArrayList<>(Arrays.asList(
new Interval(1, 5),
new Interval(11, 20)
private List<Interval> intervalsMerged = new ArrayList<>(Arrays.asList(
// @formatter:off
new Interval(1, 5),
new Interval(11, 20)
// @formatter:on
));
@Test
void givenIntervals_whenMerging_thenReturnMergedIntervals() {
MergeOverlappingIntervals merger = new MergeOverlappingIntervals();
ArrayList<Interval> result = (ArrayList<Interval>) merger.doMerge(intervals);
assertArrayEquals(intervalsMerged.toArray(), result.toArray());
List<Interval> result = merger.doMerge(intervals);
assertEquals(intervalsMerged, result);
}
}
}
@@ -5,7 +5,9 @@ import org.junit.jupiter.api.Test;
import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNull;
class BinaryTreeParentNodeFinderUnitTest {
+2 -1
View File
@@ -1,5 +1,6 @@
## Relevant Articles
- [Understanding XSLT Processing in Java](https://www.baeldung.com/java-extensible-stylesheet-language-transformations)
- [Add Camel Route at Runtime in Java](https://www.baeldung.com/java-camel-dynamic-route)
- [Logging in Apache Camel](https://www.baeldung.com/java-apache-camel-logging)
- More articles: [[<-- prev]](../apache-libraries)
@@ -0,0 +1 @@
Welcome to Baeldung
+4
View File
@@ -0,0 +1,4 @@
{
"name" : "phillip",
"age" : 5
}
@@ -0,0 +1 @@
WELCOME TO BAELDUNG
+1
View File
@@ -0,0 +1 @@
{"name":"phillip","age":5,"transformedName":"PHILLIP","transformedAge":15}

Some files were not shown because too many files have changed in this diff Show More