-
Notifications
You must be signed in to change notification settings - Fork 20.6k
Fixed Type conversion error in BloomFilter.java Added RemoveStars and ComplexNumberMultiplication #6678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SwarritSrivastava
wants to merge
11
commits into
TheAlgorithms:master
Choose a base branch
from
SwarritSrivastava:remove-stars-complex-mult
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−1
Open
Fixed Type conversion error in BloomFilter.java Added RemoveStars and ComplexNumberMultiplication #6678
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1be2986
Added RemoveStars.java and ComplexNumberMultiplication.java
SwarritSrivastava 5a0c1cf
Added RemoveStarsTest.java and ComplexNumberMultiplicationTest.java
SwarritSrivastava 9a6a84c
Refined Variable Names for RemoveStars.java and ComplexNumberMultipli…
SwarritSrivastava 3d7a6ea
Reformatted code using clang formatter
SwarritSrivastava 5f22138
Reformatted code using clang formatter
SwarritSrivastava 5345a68
Merge branch 'master' into remove-stars-complex-mult
SwarritSrivastava 55f3be8
Merge branch 'master' into remove-stars-complex-mult
SwarritSrivastava 8420d3b
fix: fixed type conversion error in datastructures/bloomfilter/BloomF…
SwarritSrivastava c37b8e6
refactor: Reformatted code usaing clang
SwarritSrivastava c495f3d
refactor: Reformatted code usaing clang
SwarritSrivastava 2f43688
Merge branch 'master' into remove-stars-complex-mult
SwarritSrivastava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn’t this class be a better fit for the math package? |
This file contains hidden or 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,45 @@ | ||
package com.thealgorithms.strings; | ||
/** | ||
* @author Swarit Srivastava (https://github.com/SwarritSrivastava) | ||
*/ | ||
public final class ComplexNumberMultiplication { | ||
private ComplexNumberMultiplication() { | ||
} | ||
/** | ||
* Multiplies two complex numbers given as strings. | ||
* <p> | ||
* Each complex number is represented in the form "real+imaginaryi" where: | ||
* <ul> | ||
* <li>real is the real part and is an integer in the range [-100, 100]</li> | ||
* <li>imaginary is the imaginary part and is an integer in the range [-100, 100]</li> | ||
* <li>i * i = -1</li> | ||
* </ul> | ||
* | ||
* Example: {@code multiplyComplexNumbers("1+1i", "1+1i") -> "0+2i"} | ||
* | ||
* @param num1 the first complex number | ||
* @param num2 the second complex number | ||
* @return the resulting complex number after multiplication | ||
*/ | ||
public static String multiplyComplexNumbers(String num1, String num2) { | ||
int plusIndex1 = num1.indexOf('+'); | ||
int plusIndex2 = num2.indexOf('+'); | ||
|
||
String realPart1 = num1.substring(0, plusIndex1); | ||
String imagPart1 = num1.substring(plusIndex1 + 1, num1.length() - 1); | ||
|
||
int re1 = Integer.parseInt(realPart1); | ||
int im1 = Integer.parseInt(imagPart1); | ||
|
||
String realPart2 = num2.substring(0, plusIndex2); | ||
String imagPart2 = num2.substring(plusIndex2 + 1, num2.length() - 1); | ||
|
||
int re2 = Integer.parseInt(realPart2); | ||
int im2 = Integer.parseInt(imagPart2); | ||
|
||
int reResult = re1 * re2 - im1 * im2; | ||
int imResult = re1 * im2 + im1 * re2; | ||
|
||
return reResult + "+" + imResult + "i"; | ||
} | ||
} |
This file contains hidden or 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,38 @@ | ||
package com.thealgorithms.strings; | ||
/** | ||
* @author Swarit Srivastava (https://github.com/SwarritSrivastava) | ||
*/ | ||
public final class RemoveStars { | ||
private RemoveStars() { | ||
} | ||
/** | ||
* Removes stars ('*') from the given string according to the following rules: | ||
* <ul> | ||
* <li>For each star in the string, remove the closest non-star character to its left | ||
* along with the star itself.</li> | ||
* <li>Return the final string after performing all removals.</li> | ||
* <li>Given that such operation is always possible for the input</li> | ||
* </ul> | ||
* | ||
* Example: {@code "leet**cod*e" -> "lecoe"} | ||
* | ||
* @param input The input string possibly containing '*' characters. | ||
* @return The resulting string after removing stars as per the rules. | ||
*/ | ||
public static String removeStars(String input) { | ||
if (input == null || input.isEmpty()) { | ||
return input; | ||
} | ||
int n = input.length(); | ||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0; i < n; i++) { | ||
char currentChar = input.charAt(i); | ||
if (currentChar != '*') { | ||
result.append(currentChar); | ||
} else { | ||
result.deleteCharAt(result.length() - 1); | ||
} | ||
} | ||
return result.toString(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java
This file contains hidden or 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,28 @@ | ||
package com.thealgorithms.strings; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ComplexNumberMultiplicationTest { | ||
|
||
@Test | ||
void testExample() { | ||
assertEquals("0+2i", ComplexNumberMultiplication.multiplyComplexNumbers("1+1i", "1+1i")); | ||
} | ||
|
||
@Test | ||
void testNegative() { | ||
assertEquals("2+0i", ComplexNumberMultiplication.multiplyComplexNumbers("1+1i", "1+-1i")); | ||
} | ||
|
||
@Test | ||
void testZero() { | ||
assertEquals("0+0i", ComplexNumberMultiplication.multiplyComplexNumbers("0+0i", "5+5i")); | ||
} | ||
|
||
@Test | ||
void testDifferentValues() { | ||
assertEquals("5+5i", ComplexNumberMultiplication.multiplyComplexNumbers("1+2i", "3+-1i")); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/com/thealgorithms/strings/RemoveStarsTest.java
This file contains hidden or 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,34 @@ | ||
package com.thealgorithms.strings; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class RemoveStarsTest { | ||
|
||
@Test | ||
void testExample() { | ||
assertEquals("lecoe", RemoveStars.removeStars("leet**cod*e")); | ||
} | ||
|
||
@Test | ||
void testMultipleStars() { | ||
assertEquals("c", RemoveStars.removeStars("ab*c*d**c")); | ||
} | ||
|
||
@Test | ||
void testEmptyInput() { | ||
assertEquals("", RemoveStars.removeStars("")); | ||
} | ||
|
||
@Test | ||
void testNullInput() { | ||
assertNull(RemoveStars.removeStars(null)); | ||
} | ||
|
||
@Test | ||
void testNoStars() { | ||
assertEquals("hello", RemoveStars.removeStars("hello")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are all these if-else branches really necessary?
You’re manually handling every primitive array type here (byte[], int[], double[], etc.), but this can be simplified. Java’s reflection API allows a more concise and general approach — you can check key.getClass().isArray() and then use either Arrays.toString((Object) key) for primitive arrays or Arrays.deepToString((Object[]) key) for object arrays.