forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
47 changed files
with
863 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
.../main/java/com/baeldung/algorithms/largestNumberRemovingK/LargestNumberRemoveKDigits.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.baeldung.algorithms.largestNumberRemovingK; | ||
|
||
import java.util.*; | ||
|
||
public class LargestNumberRemoveKDigits { | ||
public static int findLargestNumberUsingArithmetic(int num, int k) { | ||
for (int j = 0; j < k; j++) { | ||
|
||
int result = 0; | ||
int i = 1; | ||
|
||
while (num / i > 0) { | ||
int temp = (num / (i * 10)) | ||
* i | ||
+ (num % i); | ||
i *= 10; | ||
|
||
result = Math.max(result, temp); | ||
} | ||
num = result; | ||
} | ||
|
||
return num; | ||
} | ||
|
||
public static int findLargestNumberUsingStack(int num, int k) { | ||
String numStr = Integer.toString(num); | ||
int length = numStr.length(); | ||
|
||
if (k == length) return 0; | ||
|
||
Stack<Character> stack = new Stack<>(); | ||
|
||
for (int i = 0; i < length; i++) { | ||
char digit = numStr.charAt(i); | ||
|
||
while (k > 0 && !stack.isEmpty() && stack.peek() < digit) { | ||
stack.pop(); | ||
k--; | ||
} | ||
|
||
stack.push(digit); | ||
} | ||
|
||
while (k > 0) { | ||
stack.pop(); | ||
k--; | ||
} | ||
|
||
StringBuilder result = new StringBuilder(); | ||
while (!stack.isEmpty()) { | ||
result.insert(0, stack.pop()); | ||
} | ||
|
||
return Integer.parseInt(result.toString()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...va/com/baeldung/algorithms/largestNumberRemovingK/LargestNumberRemoveKDigitsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.baeldung.algorithms.largestNumberRemovingK; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LargestNumberRemoveKDigitsUnitTest { | ||
|
||
@Test | ||
public void givenNumber_UsingArithmeticRemoveKDigits_thenReturnLargestNumber(){ | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingArithmetic(9461, 1), 961); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingArithmetic(463, 2), 6); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingArithmetic(98625410, 6), 98); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingArithmetic(20, 2), 0); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingArithmetic(98989, 4), 9); | ||
} | ||
|
||
@Test | ||
public void givenNumber_UsingStackRemoveKDigits_thenReturnLargestNumber(){ | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingStack(9461, 1), 961); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingStack(463, 2), 6); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingStack(98625410, 6), 98); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingStack(20, 2), 0); | ||
Assertions.assertEquals(LargestNumberRemoveKDigits.findLargestNumberUsingStack(98989, 4), 9); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...rithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.baeldung.algorithms.vigenere; | ||
|
||
public class VigenereCipher { | ||
private final String characters; | ||
|
||
public VigenereCipher() { | ||
this("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | ||
} | ||
|
||
public VigenereCipher(String characters) { | ||
this.characters = characters; | ||
} | ||
|
||
public String encode(String input, String key) { | ||
String result = ""; | ||
|
||
int keyPosition = 0; | ||
for (char c : input.toCharArray()) { | ||
char k = key.charAt(keyPosition % key.length()); | ||
|
||
int charIndex = characters.indexOf(c); | ||
int keyIndex = characters.indexOf(k); | ||
|
||
if (charIndex >= 0) { | ||
if (keyIndex >= 0) { | ||
int newCharIndex = (charIndex + keyIndex + 1) % characters.length(); | ||
c = characters.charAt(newCharIndex); | ||
|
||
} | ||
|
||
keyPosition++; | ||
} | ||
|
||
result += c; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public String decode(String input, String key) { | ||
String result = ""; | ||
|
||
int keyPosition = 0; | ||
for (char c : input.toCharArray()) { | ||
char k = key.charAt(keyPosition % key.length()); | ||
|
||
int charIndex = characters.indexOf(c); | ||
int keyIndex = characters.indexOf(k); | ||
|
||
if (charIndex >= 0) { | ||
if (keyIndex >= 0) { | ||
int newCharIndex = charIndex - keyIndex - 1; | ||
if (newCharIndex < 0) { | ||
newCharIndex = characters.length() + newCharIndex; | ||
} | ||
c = characters.charAt(newCharIndex); | ||
|
||
} | ||
|
||
keyPosition++; | ||
} | ||
|
||
result += c; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...iscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.baeldung.algorithms.vigenere; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class VigenereCipherUnitTest { | ||
|
||
@Test | ||
void encodeBaeldung() { | ||
VigenereCipher cipher = new VigenereCipher(); | ||
String output = cipher.encode("BAELDUNG", "HELLO"); | ||
|
||
Assertions.assertEquals("JFQXSCSS", output); | ||
} | ||
|
||
@Test | ||
void encodeBaeldungMixedCharacters() { | ||
VigenereCipher cipher = new VigenereCipher("JQFVHPWORZSLNMKYCGBUXIEDTA"); | ||
String output = cipher.encode("BAELDUNG", "HELLO"); | ||
|
||
Assertions.assertEquals("DERDPTZV", output); | ||
} | ||
|
||
@Test | ||
void encodeArticleTitle() { | ||
VigenereCipher cipher = new VigenereCipher(); | ||
String output = cipher.encode("VEGENERE CIPHER IN JAVA", "BAELDUNG"); | ||
|
||
Assertions.assertEquals("XFLQRZFL EJUTIM WU LBAM", output); | ||
} | ||
|
||
@Test | ||
void encodeArticleTitleMoreCharacters() { | ||
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ "); | ||
String output = cipher.encode("VEGENERE CIPHER IN JAVA", "BAELDUNG"); | ||
|
||
Assertions.assertEquals("XFLQRZELBDNALZEGKOEVEPO", output); | ||
} | ||
|
||
@Test | ||
void decodeBaeldung() { | ||
VigenereCipher cipher = new VigenereCipher(); | ||
String output = cipher.decode("JFQXSCSS", "HELLO"); | ||
|
||
Assertions.assertEquals("BAELDUNG", output); | ||
} | ||
|
||
@Test | ||
void decodeBaeldungMixedCharacters() { | ||
VigenereCipher cipher = new VigenereCipher("JQFVHPWORZSLNMKYCGBUXIEDTA"); | ||
String output = cipher.decode("DERDPTZV", "HELLO"); | ||
|
||
Assertions.assertEquals("BAELDUNG", output); | ||
} | ||
|
||
@Test | ||
void decodeArticleTitleMoreCharacters() { | ||
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ "); | ||
String output = cipher.decode("XFLQRZELBDNALZEGKOEVEPO", "BAELDUNG"); | ||
|
||
Assertions.assertEquals("VEGENERE CIPHER IN JAVA", output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
persistence-modules/spring-data-cassandra-2/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
spring.data.cassandra.keyspace-name=${CASSANDRA_KEYSPACE_NAME} | ||
spring.data.cassandra.contact-points=${CASSANDRA_CONTACT_POINTS} | ||
spring.data.cassandra.port=${CASSANDRA_PORT} | ||
spring.data.cassandra.local-datacenter=datacenter1 | ||
spring.cassandra.keyspace-name=${CASSANDRA_KEYSPACE_NAME} | ||
spring.cassandra.contact-points=${CASSANDRA_CONTACT_POINTS} | ||
spring.cassandra.port=${CASSANDRA_PORT} | ||
spring.cassandra.local-datacenter=datacenter1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.