Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvariyath committed Mar 9, 2024
2 parents ae7d298 + eb3460d commit 7f0bd99
Show file tree
Hide file tree
Showing 47 changed files with 863 additions and 51 deletions.
3 changes: 3 additions & 0 deletions algorithms-modules/algorithms-miscellaneous-7/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
- [Find Missing Number From a Given Array in Java](https://www.baeldung.com/java-array-find-missing-number)
- [Calculate Weighted Mean in Java](https://www.baeldung.com/java-compute-weighted-average)
- [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)
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)
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());
}
}
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);
}
}
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;
}

}
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);
}
}
3 changes: 2 additions & 1 deletion apache-httpclient4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
<version>${spring-oxm.version}</version>
</dependency>
<!-- marshalling -->
<dependency>
Expand Down Expand Up @@ -233,6 +233,7 @@
</profiles>

<properties>
<spring-oxm.version>6.1.4</spring-oxm.version>
<!-- util -->
<commons-codec.version>1.16.0</commons-codec.version>
<httpasyncclient.version>4.1.5</httpasyncclient.version>
Expand Down
1 change: 1 addition & 0 deletions core-java-modules/core-java-collections-list-6/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- [Call a Method on Each Element of a List in Java](https://www.baeldung.com/java-call-method-each-list-item)
- [Sorting One List Based on Another List in Java](https://www.baeldung.com/java-sorting-one-list-using-another)
- [Reset ListIterator to First Element of the List in Java](https://www.baeldung.com/java-reset-listiterator)
- [Modify and Print List Items With Java Streams](https://www.baeldung.com/java-stream-list-update-print-elements)
1 change: 1 addition & 0 deletions core-java-modules/core-java-string-operations-8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- [Check if String is Base64 Encoded](https://www.baeldung.com/java-check-string-base64-encoding)
- [Find an Unique Email Address in a List](https://www.baeldung.com/java-find-unique-email-address)
- [Get First n Characters in a String in Java](https://www.baeldung.com/get-first-n-characters-in-a-string-in-java)
- [Remove Only Trailing Spaces or Whitespace From a String in Java](https://www.baeldung.com/java-string-remove-only-trailing-whitespace)
1 change: 1 addition & 0 deletions json-modules/gson-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ This module contains articles about Gson
- [Difference between Gson @Expose and @SerializedName](https://www.baeldung.com/gson-expose-vs-serializedname)
- [Resolving Gson’s “Multiple JSON Fields” Exception](https://www.baeldung.com/java-gson-multiple-json-fields-exception)
- [Using Static Methods Instead of Deprecated JsonParser](https://www.baeldung.com/java-static-methods-jsonparser-replacement)
- [Gson TypeToken With Dynamic List Item Type](https://www.baeldung.com/gson-typetoken-dynamic-list-item-type)
1 change: 1 addition & 0 deletions libraries-server-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ This module contains articles about server libraries.
### Relevant Articles:

- [HTTP/2 in Jetty](https://www.baeldung.com/jetty-http-2)
- [Custom Event Handlers and Listeners in Netty](https://www.baeldung.com/netty-chat-room-customize-event-handlers-listeners)
- More articles: [[<-- prev]](../libraries-server)
1 change: 1 addition & 0 deletions persistence-modules/spring-boot-persistence-4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- [N+1 Problem in Hibernate and Spring Data JPA](https://www.baeldung.com/spring-hibernate-n1-problem)
- [Get All Results at Once in a Spring Boot Paged Query Method](https://www.baeldung.com/spring-boot-paged-query-all-results)
- [Calling Custom Database Functions With JPA and Spring Boot](https://www.baeldung.com/spring-data-jpa-custom-database-functions)
- [Spring Data JPA Repository for Database View](https://www.baeldung.com/spring-data-jpa-repository-view)
13 changes: 7 additions & 6 deletions persistence-modules/spring-data-cassandra-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../../parent-boot-3</relativePath>
</parent>

<dependencies>
Expand Down Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-mapper-runtime</artifactId>
<version>4.15.0</version>
<version>4.17.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -99,10 +99,11 @@
</build>

<properties>
<org.springframework.data.version>3.4.15</org.springframework.data.version>
<testcontainers.version>1.19.0</testcontainers.version>
<system.stubs.version>1.1.0</system.stubs.version>
<org.springframework.data.version>4.1.9</org.springframework.data.version>
<testcontainers.version>1.19.5</testcontainers.version>
<system.stubs.version>2.1.5</system.stubs.version>
<junit.jupiter.version>5.9.3</junit.jupiter.version>
<start-class>org.baeldung.springcassandra.SpringCassandraApplication</start-class>
</properties>

</project>
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand All @@ -30,14 +32,14 @@ class ProductRepositoryNestedLiveTest {
private static final String KEYSPACE_NAME = "mynamespace";

@Container
private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2")
private static final CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2")
.withExposedPorts(9042);

@BeforeAll
static void setupCassandraConnectionProperties() {
System.setProperty("spring.data.cassandra.keyspace-name", KEYSPACE_NAME);
System.setProperty("spring.data.cassandra.contact-points", cassandra.getHost());
System.setProperty("spring.data.cassandra.port", String.valueOf(cassandra.getMappedPort(9042)));
System.setProperty("spring.cassandra.keyspace-name", KEYSPACE_NAME);
System.setProperty("spring.cassandra.contact-points", cassandra.getHost());
System.setProperty("spring.cassandra.port", String.valueOf(cassandra.getMappedPort(9042)));
createKeyspace(cassandra.getCluster());
}

Expand Down Expand Up @@ -72,9 +74,9 @@ void givenValidProductsIsFetched_whenFindByProductIdsIsCalled_thenProductIsRetur
Product product3 = new Product(productId3, "Banana", "Banana v1", 5.5);
Product product4 = new Product(productId3, "Banana v2", "Banana v2", 15.5);

productRepository.saveAll(List.of(product1, product2, product3, product4));
productRepository.saveAll(Arrays.asList(product1, product2, product3, product4));

List<Product> existingProducts = productRepository.findByProductIds(List.of(productId1, productId2));
List<Product> existingProducts = productRepository.findByProductIds(Arrays.asList(productId1, productId2));
assertEquals(2, existingProducts.size());
assertTrue(existingProducts.contains(product1));
assertTrue(existingProducts.contains(product2));
Expand All @@ -89,10 +91,10 @@ void givenExistingProducts_whenFindByIdAndNamesIsCalled_thenProductIsReturned()
Product product3 = new Product(productId2, "Banana", "Banana v1", 5.5);
Product product4 = new Product(productId2, "Banana v2", "Banana v2", 15.5);

productRepository.saveAll(List.of(product1, product2, product3, product4));
productRepository.saveAll(Arrays.asList(product1, product2, product3, product4));

List<Product> existingProducts = productRepository.findByProductIdAndNames(productId1,
List.of(product1.getProductName(), product2.getProductName()));
Arrays.asList(product1.getProductName(), product2.getProductName()));
assertEquals(2, existingProducts.size());
assertTrue(existingProducts.contains(product1));
assertTrue(existingProducts.contains(product2));
Expand All @@ -107,10 +109,11 @@ void givenNonExistingProductName_whenFindByIdAndNamesIsCalled_thenProductIsRetur
Product product3 = new Product(productId2, "Banana", "Banana v1", 5.5);
Product product4 = new Product(productId2, "Banana v2", "Banana v2", 15.5);

productRepository.saveAll(List.of(product1, product2, product4));
productRepository.saveAll(Arrays.asList(product1, product2, product4));

List<Product> existingProducts = productRepository.findByProductIdAndNames(productId1,
List.of(product3.getProductName()));
Collections.singletonList(product3.getProductName())
);
assertEquals(0, existingProducts.size());
}
}
Expand Down
Loading

0 comments on commit 7f0bd99

Please sign in to comment.