Skip to content

Commit 46c8f48

Browse files
authored
Merge branch 'master' into BAEL-2313-String-to-byte-array
2 parents 6b7dfe7 + 0f443d3 commit 46c8f48

1,489 files changed

Lines changed: 22540 additions & 4337 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.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ before_install:
44
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
55

66
install: skip
7-
script: travis_wait 60 mvn -q install -Pdefault-first,default-second
7+
script: travis_wait 60 mvn -q install -Pdefault-first,default-second -Dgib.enabled=true
88

99
sudo: required
1010

JGit/src/main/java/com/baeldung/jgit/porcelain/Log.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public static void main(String[] args) throws IOException, GitAPIException {
2828
System.out.println("Had " + count + " commits overall on current branch");
2929

3030
logs = git.log()
31-
.add(repository.resolve("remotes/origin/testbranch"))
31+
.add(repository.resolve(git.getRepository().getFullBranch()))
3232
.call();
3333
count = 0;
3434
for (RevCommit rev : logs) {
3535
System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
3636
count++;
3737
}
38-
System.out.println("Had " + count + " commits overall on test-branch");
38+
System.out.println("Had " + count + " commits overall on "+git.getRepository().getFullBranch());
3939

4040
logs = git.log()
4141
.all()

JGit/src/test/java/com/baeldung/jgit/JGitBugIntegrationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.baeldung.jgit;
2+
13
import com.baeldung.jgit.helper.Helper;
24
import org.eclipse.jgit.lib.ObjectLoader;
35
import org.eclipse.jgit.lib.ObjectReader;

algorithms-genetic/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
54
<artifactId>algorithms-genetic</artifactId>
65
<version>0.0.1-SNAPSHOT</version>
7-
6+
<name>algorithms-genetic</name>
7+
88
<parent>
99
<groupId>com.baeldung</groupId>
1010
<artifactId>parent-modules</artifactId>
@@ -61,4 +61,5 @@
6161
<commons-codec.version>1.11</commons-codec.version>
6262
</properties>
6363

64-
</project>
64+
65+
</project>

algorithms-miscellaneous-1/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
54
<artifactId>algorithms-miscellaneous-1</artifactId>
65
<version>0.0.1-SNAPSHOT</version>
7-
6+
<name>algorithms-miscellaneous-1</name>
7+
88
<parent>
99
<groupId>com.baeldung</groupId>
1010
<artifactId>parent-modules</artifactId>
@@ -17,6 +17,11 @@
1717
<artifactId>commons-math3</artifactId>
1818
<version>${commons-math3.version}</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>com.google.guava</groupId>
22+
<artifactId>guava</artifactId>
23+
<version>${guava.version}</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>commons-codec</groupId>
2227
<artifactId>commons-codec</artifactId>
@@ -73,6 +78,7 @@
7378
<commons-math3.version>3.6.1</commons-math3.version>
7479
<org.assertj.core.version>3.9.0</org.assertj.core.version>
7580
<commons-codec.version>1.11</commons-codec.version>
81+
<guava.version>25.1-jre</guava.version>
7682
</properties>
7783

7884
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.algorithms.factorial;
2+
3+
import java.math.BigInteger;
4+
import java.util.stream.LongStream;
5+
6+
import org.apache.commons.math3.util.CombinatoricsUtils;
7+
8+
import com.google.common.math.BigIntegerMath;
9+
10+
public class Factorial {
11+
12+
public long factorialUsingForLoop(int n) {
13+
long fact = 1;
14+
for (int i = 2; i <= n; i++) {
15+
fact = fact * i;
16+
}
17+
return fact;
18+
}
19+
20+
public long factorialUsingStreams(int n) {
21+
return LongStream.rangeClosed(1, n)
22+
.reduce(1, (long x, long y) -> x * y);
23+
}
24+
25+
public long factorialUsingRecursion(int n) {
26+
if (n <= 2) {
27+
return n;
28+
}
29+
return n * factorialUsingRecursion(n - 1);
30+
}
31+
32+
private Long[] factorials = new Long[20];
33+
34+
public long factorialUsingMemoize(int n) {
35+
36+
if (factorials[n] != null) {
37+
return factorials[n];
38+
}
39+
40+
if (n <= 2) {
41+
return n;
42+
}
43+
long nthValue = n * factorialUsingMemoize(n - 1);
44+
factorials[n] = nthValue;
45+
return nthValue;
46+
}
47+
48+
public BigInteger factorialHavingLargeResult(int n) {
49+
BigInteger result = BigInteger.ONE;
50+
for (int i = 2; i <= n; i++)
51+
result = result.multiply(BigInteger.valueOf(i));
52+
return result;
53+
}
54+
55+
public long factorialUsingApacheCommons(int n) {
56+
return CombinatoricsUtils.factorial(n);
57+
}
58+
59+
public BigInteger factorialUsingGuava(int n) {
60+
return BigIntegerMath.factorial(n);
61+
}
62+
63+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.algorithms.string;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class LongestSubstringNonRepeatingCharacters {
9+
10+
public static String getUniqueCharacterSubstringBruteForce(String input) {
11+
String output = "";
12+
for (int start = 0; start < input.length(); start++) {
13+
Set<Character> visited = new HashSet<>();
14+
int end = start;
15+
for (; end < input.length(); end++) {
16+
char currChar = input.charAt(end);
17+
if (visited.contains(currChar)) {
18+
break;
19+
} else {
20+
visited.add(currChar);
21+
}
22+
}
23+
if (output.length() < end - start + 1) {
24+
output = input.substring(start, end);
25+
}
26+
}
27+
return output;
28+
}
29+
30+
public static String getUniqueCharacterSubstring(String input) {
31+
Map<Character, Integer> visited = new HashMap<>();
32+
String output = "";
33+
for (int start = 0, end = 0; end < input.length(); end++) {
34+
char currChar = input.charAt(end);
35+
if (visited.containsKey(currChar)) {
36+
start = Math.max(visited.get(currChar) + 1, start);
37+
}
38+
if (output.length() < end - start + 1) {
39+
output = input.substring(start, end + 1);
40+
}
41+
visited.put(currChar, end);
42+
}
43+
return output;
44+
}
45+
46+
public static void main(String[] args) {
47+
if(args.length > 0) {
48+
System.out.println(getUniqueCharacterSubstring(args[0]));
49+
} else {
50+
System.err.println("This program expects command-line input. Please try again!");
51+
}
52+
}
53+
54+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.algorithms.string;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class SubstringPalindrome {
7+
8+
public Set<String> findAllPalindromesUsingCenter(String input) {
9+
final Set<String> palindromes = new HashSet<>();
10+
if (input == null || input.isEmpty()) {
11+
return palindromes;
12+
}
13+
if (input.length() == 1) {
14+
palindromes.add(input);
15+
return palindromes;
16+
}
17+
for (int i = 0; i < input.length(); i++) {
18+
palindromes.addAll(findPalindromes(input, i, i + 1));
19+
palindromes.addAll(findPalindromes(input, i, i));
20+
}
21+
return palindromes;
22+
}
23+
24+
private Set<String> findPalindromes(String input, int low, int high) {
25+
Set<String> result = new HashSet<>();
26+
while (low >= 0 && high < input.length() && input.charAt(low) == input.charAt(high)) {
27+
result.add(input.substring(low, high + 1));
28+
low--;
29+
high++;
30+
}
31+
return result;
32+
}
33+
34+
public Set<String> findAllPalindromesUsingBruteForceApproach(String input) {
35+
Set<String> palindromes = new HashSet<>();
36+
if (input == null || input.isEmpty()) {
37+
return palindromes;
38+
}
39+
if (input.length() == 1) {
40+
palindromes.add(input);
41+
return palindromes;
42+
}
43+
for (int i = 0; i < input.length(); i++) {
44+
for (int j = i + 1; j <= input.length(); j++)
45+
if (isPalindrome(input.substring(i, j))) {
46+
palindromes.add(input.substring(i, j));
47+
}
48+
}
49+
return palindromes;
50+
}
51+
52+
private boolean isPalindrome(String input) {
53+
StringBuilder plain = new StringBuilder(input);
54+
StringBuilder reverse = plain.reverse();
55+
return (reverse.toString()).equals(input);
56+
}
57+
58+
public Set<String> findAllPalindromesUsingManachersAlgorithm(String input) {
59+
Set<String> palindromes = new HashSet<>();
60+
String formattedInput = "@" + input + "#";
61+
char inputCharArr[] = formattedInput.toCharArray();
62+
int max;
63+
int radius[][] = new int[2][input.length() + 1];
64+
for (int j = 0; j <= 1; j++) {
65+
radius[j][0] = max = 0;
66+
int i = 1;
67+
while (i <= input.length()) {
68+
palindromes.add(Character.toString(inputCharArr[i]));
69+
while (inputCharArr[i - max - 1] == inputCharArr[i + j + max])
70+
max++;
71+
radius[j][i] = max;
72+
int k = 1;
73+
while ((radius[j][i - k] != max - k) && (k < max)) {
74+
radius[j][i + k] = Math.min(radius[j][i - k], max - k);
75+
k++;
76+
}
77+
max = Math.max(max - k, 0);
78+
i += k;
79+
}
80+
}
81+
for (int i = 1; i <= input.length(); i++) {
82+
for (int j = 0; j <= 1; j++) {
83+
for (max = radius[j][i]; max > 0; max--) {
84+
palindromes.add(input.substring(i - max - 1, max + j + i - 1));
85+
}
86+
}
87+
}
88+
return palindromes;
89+
}
90+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.algorithms.factorial;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.math.BigInteger;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class FactorialUnitTest {
11+
12+
Factorial factorial;
13+
14+
@Before
15+
public void setup() {
16+
factorial = new Factorial();
17+
}
18+
19+
@Test
20+
public void whenCalculatingFactorialUsingForLoop_thenCorrect() {
21+
int n = 5;
22+
23+
assertThat(factorial.factorialUsingForLoop(n)).isEqualTo(120);
24+
}
25+
26+
@Test
27+
public void whenCalculatingFactorialUsingStreams_thenCorrect() {
28+
int n = 5;
29+
30+
assertThat(factorial.factorialUsingStreams(n)).isEqualTo(120);
31+
}
32+
33+
@Test
34+
public void whenCalculatingFactorialUsingRecursion_thenCorrect() {
35+
int n = 5;
36+
37+
assertThat(factorial.factorialUsingRecursion(n)).isEqualTo(120);
38+
}
39+
40+
@Test
41+
public void whenCalculatingFactorialUsingMemoize_thenCorrect() {
42+
int n = 5;
43+
44+
assertThat(factorial.factorialUsingMemoize(n)).isEqualTo(120);
45+
46+
n = 6;
47+
48+
assertThat(factorial.factorialUsingMemoize(n)).isEqualTo(720);
49+
}
50+
51+
@Test
52+
public void whenCalculatingFactorialHavingLargeResult_thenCorrect() {
53+
int n = 22;
54+
55+
assertThat(factorial.factorialHavingLargeResult(n)).isEqualTo(new BigInteger("1124000727777607680000"));
56+
}
57+
58+
@Test
59+
public void whenCalculatingFactorialUsingApacheCommons_thenCorrect() {
60+
int n = 5;
61+
62+
assertThat(factorial.factorialUsingApacheCommons(n)).isEqualTo(120);
63+
}
64+
65+
@Test
66+
public void whenCalculatingFactorialUsingGuava_thenCorrect() {
67+
int n = 22;
68+
69+
assertThat(factorial.factorialUsingGuava(n)).isEqualTo(new BigInteger("1124000727777607680000"));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)