Skip to content

Commit b7cbac7

Browse files
authored
Merge branch 'master' into BAEL-2775
2 parents f1ef8d9 + 3ca1646 commit b7cbac7

File tree

1,724 files changed

+81216
-2349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,724 files changed

+81216
-2349
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
.idea/
2020
*.iml
2121
*.iws
22+
out/
2223

2324
# Mac
2425
.DS_Store
@@ -27,6 +28,9 @@
2728
log/
2829
target/
2930

31+
# Gradle
32+
.gradle/
33+
3034
spring-openid/src/main/resources/application.properties
3135
.recommenders/
3236
/spring-hibernate4/nbproject/

akka-streams/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
<dependencies>
1515
<dependency>
1616
<groupId>com.typesafe.akka</groupId>
17-
<artifactId>akka-stream_2.11</artifactId>
17+
<artifactId>akka-stream_${scala.version}</artifactId>
1818
<version>${akkastreams.version}</version>
1919
</dependency>
2020
<dependency>
2121
<groupId>com.typesafe.akka</groupId>
22-
<artifactId>akka-stream-testkit_2.11</artifactId>
22+
<artifactId>akka-stream-testkit_${scala.version}</artifactId>
2323
<version>${akkastreams.version}</version>
2424
</dependency>
2525
</dependencies>
2626

2727
<properties>
2828
<akkastreams.version>2.5.2</akkastreams.version>
29+
<scala.version>2.11</scala.version>
2930
</properties>
3031

3132
</project>

algorithms-miscellaneous-1/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@
1414
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
1515
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
1616
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
17-
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
1817
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
19-
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
2018
- [Generate Combinations in Java](https://www.baeldung.com/java-combinations-algorithm)

algorithms-miscellaneous-2/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
99
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
1010
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
11-
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
12-
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
13-
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
1411
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
1512
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
1613
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.algorithms.relativelyprime;
2+
3+
import java.math.BigInteger;
4+
5+
class RelativelyPrime {
6+
7+
static boolean iterativeRelativelyPrime(int a, int b) {
8+
return iterativeGCD(a, b) == 1;
9+
}
10+
11+
static boolean recursiveRelativelyPrime(int a, int b) {
12+
return recursiveGCD(a, b) == 1;
13+
}
14+
15+
static boolean bigIntegerRelativelyPrime(int a, int b) {
16+
return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).equals(BigInteger.ONE);
17+
}
18+
19+
private static int iterativeGCD(int a, int b) {
20+
int tmp;
21+
while (b != 0) {
22+
if (a < b) {
23+
tmp = a;
24+
a = b;
25+
b = tmp;
26+
}
27+
tmp = b;
28+
b = a % b;
29+
a = tmp;
30+
}
31+
return a;
32+
}
33+
34+
private static int recursiveGCD(int a, int b) {
35+
if (b == 0) {
36+
return a;
37+
}
38+
if (a < b) {
39+
return recursiveGCD(b, a);
40+
}
41+
return recursiveGCD(b, a % b);
42+
}
43+
44+
45+
}
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
package com.baeldung.algorithms.reversingtree;
2-
3-
public class TreeNode {
4-
5-
private int value;
6-
private TreeNode rightChild;
7-
private TreeNode leftChild;
8-
9-
public int getValue() {
10-
return value;
11-
}
12-
13-
public void setValue(int value) {
14-
this.value = value;
15-
}
16-
17-
public TreeNode getRightChild() {
18-
return rightChild;
19-
}
20-
21-
public void setRightChild(TreeNode rightChild) {
22-
this.rightChild = rightChild;
23-
}
24-
25-
public TreeNode getLeftChild() {
26-
return leftChild;
27-
}
28-
29-
public void setLeftChild(TreeNode leftChild) {
30-
this.leftChild = leftChild;
31-
}
32-
33-
public TreeNode(int value, TreeNode rightChild, TreeNode leftChild) {
34-
this.value = value;
35-
this.rightChild = rightChild;
36-
this.leftChild = leftChild;
37-
}
38-
39-
public TreeNode(int value) {
40-
this.value = value;
41-
}
42-
}
1+
package com.baeldung.algorithms.reversingtree;
2+
3+
public class TreeNode {
4+
5+
private int value;
6+
private TreeNode rightChild;
7+
private TreeNode leftChild;
8+
9+
public int getValue() {
10+
return value;
11+
}
12+
13+
public void setValue(int value) {
14+
this.value = value;
15+
}
16+
17+
public TreeNode getRightChild() {
18+
return rightChild;
19+
}
20+
21+
public void setRightChild(TreeNode rightChild) {
22+
this.rightChild = rightChild;
23+
}
24+
25+
public TreeNode getLeftChild() {
26+
return leftChild;
27+
}
28+
29+
public void setLeftChild(TreeNode leftChild) {
30+
this.leftChild = leftChild;
31+
}
32+
33+
public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
34+
this.value = value;
35+
this.rightChild = rightChild;
36+
this.leftChild = leftChild;
37+
}
38+
39+
public TreeNode(int value) {
40+
this.value = value;
41+
}
42+
}
Lines changed: 53 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,53 @@
1-
package com.baeldung.algorithms.reversingtree;
2-
3-
import java.util.LinkedList;
4-
5-
public class TreeReverser {
6-
7-
public TreeNode createBinaryTree() {
8-
9-
TreeNode leaf1 = new TreeNode(3);
10-
TreeNode leaf2 = new TreeNode(1);
11-
TreeNode leaf3 = new TreeNode(9);
12-
TreeNode leaf4 = new TreeNode(6);
13-
14-
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
15-
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
16-
17-
TreeNode root = new TreeNode(4, nodeRight, nodeLeft);
18-
19-
return root;
20-
}
21-
22-
public void reverseRecursive(TreeNode treeNode) {
23-
if (treeNode == null) {
24-
return;
25-
}
26-
27-
TreeNode temp = treeNode.getLeftChild();
28-
treeNode.setLeftChild(treeNode.getRightChild());
29-
treeNode.setRightChild(temp);
30-
31-
reverseRecursive(treeNode.getLeftChild());
32-
reverseRecursive(treeNode.getRightChild());
33-
}
34-
35-
public void reverseIterative(TreeNode treeNode) {
36-
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
37-
38-
if (treeNode != null) {
39-
queue.add(treeNode);
40-
}
41-
42-
while (!queue.isEmpty()) {
43-
44-
TreeNode node = queue.poll();
45-
if (node.getLeftChild() != null)
46-
queue.add(node.getLeftChild());
47-
if (node.getRightChild() != null)
48-
queue.add(node.getRightChild());
49-
50-
TreeNode temp = node.getLeftChild();
51-
node.setLeftChild(node.getRightChild());
52-
node.setRightChild(temp);
53-
}
54-
}
55-
56-
public String toString(TreeNode root) {
57-
if (root == null) {
58-
return "";
59-
}
60-
61-
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
62-
63-
buffer.append(toString(root.getLeftChild()));
64-
buffer.append(toString(root.getRightChild()));
65-
66-
return buffer.toString();
67-
}
68-
}
1+
package com.baeldung.algorithms.reversingtree;
2+
3+
import java.util.LinkedList;
4+
5+
public class TreeReverser {
6+
7+
public void reverseRecursive(TreeNode treeNode) {
8+
if (treeNode == null) {
9+
return;
10+
}
11+
12+
TreeNode temp = treeNode.getLeftChild();
13+
treeNode.setLeftChild(treeNode.getRightChild());
14+
treeNode.setRightChild(temp);
15+
16+
reverseRecursive(treeNode.getLeftChild());
17+
reverseRecursive(treeNode.getRightChild());
18+
}
19+
20+
public void reverseIterative(TreeNode treeNode) {
21+
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
22+
23+
if (treeNode != null) {
24+
queue.add(treeNode);
25+
}
26+
27+
while (!queue.isEmpty()) {
28+
29+
TreeNode node = queue.poll();
30+
if (node.getLeftChild() != null)
31+
queue.add(node.getLeftChild());
32+
if (node.getRightChild() != null)
33+
queue.add(node.getRightChild());
34+
35+
TreeNode temp = node.getLeftChild();
36+
node.setLeftChild(node.getRightChild());
37+
node.setRightChild(temp);
38+
}
39+
}
40+
41+
public String toString(TreeNode root) {
42+
if (root == null) {
43+
return "";
44+
}
45+
46+
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
47+
48+
buffer.append(toString(root.getLeftChild()));
49+
buffer.append(toString(root.getRightChild()));
50+
51+
return buffer.toString();
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.algorithms.relativelyprime;
2+
3+
import org.junit.Test;
4+
5+
import static com.baeldung.algorithms.relativelyprime.RelativelyPrime.*;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
public class RelativelyPrimeUnitTest {
9+
10+
@Test
11+
public void givenNonRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnFalse() {
12+
13+
boolean result = iterativeRelativelyPrime(45, 35);
14+
assertThat(result).isFalse();
15+
}
16+
17+
@Test
18+
public void givenRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnTrue() {
19+
20+
boolean result = iterativeRelativelyPrime(500, 501);
21+
assertThat(result).isTrue();
22+
}
23+
24+
@Test
25+
public void givenNonRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnFalse() {
26+
27+
boolean result = recursiveRelativelyPrime(45, 35);
28+
assertThat(result).isFalse();
29+
}
30+
31+
@Test
32+
public void givenRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnTrue() {
33+
34+
boolean result = recursiveRelativelyPrime(500, 501);
35+
assertThat(result).isTrue();
36+
}
37+
38+
@Test
39+
public void givenNonRelativelyPrimeNumbers_whenCheckingUsingBigIntegers_shouldReturnFalse() {
40+
41+
boolean result = bigIntegerRelativelyPrime(45, 35);
42+
assertThat(result).isFalse();
43+
}
44+
45+
@Test
46+
public void givenRelativelyPrimeNumbers_whenCheckingBigIntegers_shouldReturnTrue() {
47+
48+
boolean result = bigIntegerRelativelyPrime(500, 501);
49+
assertThat(result).isTrue();
50+
}
51+
}

0 commit comments

Comments
 (0)