Skip to content

Commit 6cae548

Browse files
authored
Merge pull request eugenp#2 from eugenp/master
Getting latest changes from main repository
2 parents b11033b + 66ede7f commit 6cae548

521 files changed

Lines changed: 13263 additions & 1686 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
4242
*.springBeans
4343

4444
20171220-JMeter.csv
45+
46+
.factorypath
47+
dependency-reduced-pom.xml
48+
*.so
49+
*.dylib
50+
*.dll

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ This tutorials project is being built **[>> HERE](https://rest-security.ci.cloud
3939

4040
- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
4141
- [Apache Maven Tutorial](http://www.baeldung.com/maven)
42+
- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)

algorithms/src/main/java/com/baeldung/algorithms/mcts/montecarlo/MonteCarloTreeSearch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class MonteCarloTreeSearch {
1010

1111
private static final int WIN_SCORE = 10;
1212
private int level;
13-
private int oponent;
13+
private int opponent;
1414

1515
public MonteCarloTreeSearch() {
1616
this.level = 3;
@@ -32,11 +32,11 @@ public Board findNextMove(Board board, int playerNo) {
3232
long start = System.currentTimeMillis();
3333
long end = start + 60 * getMillisForCurrentLevel();
3434

35-
oponent = 3 - playerNo;
35+
opponent = 3 - playerNo;
3636
Tree tree = new Tree();
3737
Node rootNode = tree.getRoot();
3838
rootNode.getState().setBoard(board);
39-
rootNode.getState().setPlayerNo(oponent);
39+
rootNode.getState().setPlayerNo(opponent);
4040

4141
while (System.currentTimeMillis() < end) {
4242
// Phase 1 - Selection
@@ -93,7 +93,7 @@ private int simulateRandomPlayout(Node node) {
9393
State tempState = tempNode.getState();
9494
int boardStatus = tempState.getBoard().checkStatus();
9595

96-
if (boardStatus == oponent) {
96+
if (boardStatus == opponent) {
9797
tempNode.getParent().getState().setWinScore(Integer.MIN_VALUE);
9898
return boardStatus;
9999
}

core-java/src/main/java/com/baeldung/linkedlist/MiddleElementLookup.java renamed to algorithms/src/main/java/com/baeldung/algorithms/middleelementlookup/MiddleElementLookup.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
package com.baeldung.linkedlist;
1+
package com.baeldung.algorithms.middleelementlookup;
22

33
import java.util.LinkedList;
4-
5-
import com.baeldung.linkedlist.Node;
4+
import java.util.Optional;
65

76
public class MiddleElementLookup {
87

9-
public static String findMiddleElementLinkedList(LinkedList<String> linkedList) {
8+
public static Optional<String> findMiddleElementLinkedList(LinkedList<String> linkedList) {
109
if (linkedList == null || linkedList.isEmpty()) {
11-
return null;
10+
return Optional.empty();
1211
}
1312

14-
return linkedList.get((linkedList.size() - 1) / 2);
13+
return Optional.ofNullable(linkedList.get((linkedList.size() - 1) / 2));
1514
}
1615

17-
public static String findMiddleElementFromHead(Node head) {
16+
public static Optional<String> findMiddleElementFromHead(Node head) {
1817
if (head == null) {
19-
return null;
18+
return Optional.empty();
2019
}
2120

2221
// calculate the size of the list
@@ -33,17 +32,17 @@ public static String findMiddleElementFromHead(Node head) {
3332
current = current.next();
3433
}
3534

36-
return current.data();
35+
return Optional.ofNullable(current.data());
3736
}
3837

39-
public static String findMiddleElementFromHead1PassRecursively(Node head) {
38+
public static Optional<String> findMiddleElementFromHead1PassRecursively(Node head) {
4039
if (head == null) {
41-
return null;
40+
return Optional.empty();
4241
}
4342

4443
MiddleAuxRecursion middleAux = new MiddleAuxRecursion();
4544
findMiddleRecursively(head, middleAux);
46-
return middleAux.middle.data();
45+
return Optional.ofNullable(middleAux.middle.data());
4746
}
4847

4948
private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAux) {
@@ -63,9 +62,9 @@ private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAu
6362
middleAux.length--;
6463
}
6564

66-
public static String findMiddleElementFromHead1PassIteratively(Node head) {
65+
public static Optional<String> findMiddleElementFromHead1PassIteratively(Node head) {
6766
if (head == null) {
68-
return null;
67+
return Optional.empty();
6968
}
7069

7170
Node slowPointer = head;
@@ -78,7 +77,7 @@ public static String findMiddleElementFromHead1PassIteratively(Node head) {
7877
slowPointer = slowPointer.next();
7978
}
8079

81-
return slowPointer.data();
80+
return Optional.ofNullable(slowPointer.data());
8281
}
8382

8483
private static class MiddleAuxRecursion {

core-java/src/main/java/com/baeldung/linkedlist/Node.java renamed to algorithms/src/main/java/com/baeldung/algorithms/middleelementlookup/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.linkedlist;
1+
package com.baeldung.algorithms.middleelementlookup;
22

33
public class Node {
44
private Node next;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.algorithms.romannumerals;
2+
3+
import java.util.List;
4+
5+
class RomanArabicConverter {
6+
7+
public static int romanToArabic(String input) {
8+
String romanNumeral = input.toUpperCase();
9+
int result = 0;
10+
11+
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
12+
13+
int i = 0;
14+
15+
while ((romanNumeral.length() > 0) && (i < romanNumerals.size())) {
16+
RomanNumeral symbol = romanNumerals.get(i);
17+
if (romanNumeral.startsWith(symbol.name())) {
18+
result += symbol.getValue();
19+
romanNumeral = romanNumeral.substring(symbol.name().length());
20+
} else {
21+
i++;
22+
}
23+
}
24+
if (romanNumeral.length() > 0) {
25+
throw new IllegalArgumentException(input + " cannot be converted to a Roman Numeral");
26+
}
27+
28+
return result;
29+
}
30+
31+
public static String arabicToRoman(int number) {
32+
if ((number <= 0) || (number > 4000)) {
33+
throw new IllegalArgumentException(number + " is not in range (0,4000]");
34+
}
35+
36+
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
37+
38+
int i = 0;
39+
StringBuilder sb = new StringBuilder();
40+
41+
while (number > 0 && i < romanNumerals.size()) {
42+
RomanNumeral currentSymbol = romanNumerals.get(i);
43+
if (currentSymbol.getValue() <= number) {
44+
sb.append(currentSymbol.name());
45+
number -= currentSymbol.getValue();
46+
} else {
47+
i++;
48+
}
49+
}
50+
return sb.toString();
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.algorithms.romannumerals;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
enum RomanNumeral {
9+
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
10+
11+
private int value;
12+
13+
RomanNumeral(int value) {
14+
this.value = value;
15+
}
16+
17+
public int getValue() {
18+
return value;
19+
}
20+
21+
public static List<RomanNumeral> getReverseSortedValues() {
22+
return Arrays.stream(values())
23+
.sorted(Comparator.comparing((RomanNumeral e) -> e.value).reversed())
24+
.collect(Collectors.toList());
25+
}
26+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package algorithms;
2+
3+
import com.baeldung.algorithms.middleelementlookup.MiddleElementLookup;
4+
import com.baeldung.algorithms.middleelementlookup.Node;
5+
import org.junit.Test;
6+
7+
import java.util.LinkedList;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertFalse;
11+
12+
public class MiddleElementLookupUnitTest {
13+
14+
@Test
15+
public void whenFindingMiddleLinkedList_thenMiddleFound() {
16+
assertEquals("3", MiddleElementLookup
17+
.findMiddleElementLinkedList(createLinkedList(5))
18+
.get());
19+
assertEquals("2", MiddleElementLookup
20+
.findMiddleElementLinkedList(createLinkedList(4))
21+
.get());
22+
}
23+
24+
@Test
25+
public void whenFindingMiddleFromHead_thenMiddleFound() {
26+
assertEquals("3", MiddleElementLookup
27+
.findMiddleElementFromHead(createNodesList(5))
28+
.get());
29+
assertEquals("2", MiddleElementLookup
30+
.findMiddleElementFromHead(createNodesList(4))
31+
.get());
32+
}
33+
34+
@Test
35+
public void whenFindingMiddleFromHead1PassRecursively_thenMiddleFound() {
36+
assertEquals("3", MiddleElementLookup
37+
.findMiddleElementFromHead1PassRecursively(createNodesList(5))
38+
.get());
39+
assertEquals("2", MiddleElementLookup
40+
.findMiddleElementFromHead1PassRecursively(createNodesList(4))
41+
.get());
42+
}
43+
44+
@Test
45+
public void whenFindingMiddleFromHead1PassIteratively_thenMiddleFound() {
46+
assertEquals("3", MiddleElementLookup
47+
.findMiddleElementFromHead1PassIteratively(createNodesList(5))
48+
.get());
49+
assertEquals("2", MiddleElementLookup
50+
.findMiddleElementFromHead1PassIteratively(createNodesList(4))
51+
.get());
52+
}
53+
54+
@Test
55+
public void whenListEmptyOrNull_thenMiddleNotFound() {
56+
// null list
57+
assertFalse(MiddleElementLookup
58+
.findMiddleElementLinkedList(null)
59+
.isPresent());
60+
assertFalse(MiddleElementLookup
61+
.findMiddleElementFromHead(null)
62+
.isPresent());
63+
assertFalse(MiddleElementLookup
64+
.findMiddleElementFromHead1PassIteratively(null)
65+
.isPresent());
66+
assertFalse(MiddleElementLookup
67+
.findMiddleElementFromHead1PassRecursively(null)
68+
.isPresent());
69+
70+
// empty LinkedList
71+
assertFalse(MiddleElementLookup
72+
.findMiddleElementLinkedList(new LinkedList<>())
73+
.isPresent());
74+
75+
// LinkedList with nulls
76+
LinkedList<String> nullsList = new LinkedList<>();
77+
nullsList.add(null);
78+
nullsList.add(null);
79+
assertFalse(MiddleElementLookup
80+
.findMiddleElementLinkedList(nullsList)
81+
.isPresent());
82+
83+
// nodes with null values
84+
assertFalse(MiddleElementLookup
85+
.findMiddleElementFromHead(new Node(null))
86+
.isPresent());
87+
assertFalse(MiddleElementLookup
88+
.findMiddleElementFromHead1PassIteratively(new Node(null))
89+
.isPresent());
90+
assertFalse(MiddleElementLookup
91+
.findMiddleElementFromHead1PassRecursively(new Node(null))
92+
.isPresent());
93+
}
94+
95+
private static LinkedList<String> createLinkedList(int n) {
96+
LinkedList<String> list = new LinkedList<>();
97+
98+
for (int i = 1; i <= n; i++) {
99+
list.add(String.valueOf(i));
100+
}
101+
102+
return list;
103+
}
104+
105+
private static Node createNodesList(int n) {
106+
Node head = new Node("1");
107+
Node current = head;
108+
109+
for (int i = 2; i <= n; i++) {
110+
Node newNode = new Node(String.valueOf(i));
111+
current.setNext(newNode);
112+
current = newNode;
113+
}
114+
115+
return head;
116+
}
117+
118+
}

0 commit comments

Comments
 (0)