Skip to content
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

General performance improvement: #6078

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main/java/com/thealgorithms/ciphers/AES.java
Original file line number Diff line number Diff line change
Expand Up @@ -2418,8 +2418,6 @@ public static BigInteger scheduleCore(BigInteger t, int rconCounter) {
rBytes = new StringBuilder(rBytes.substring(0, i * 2) + currentByteBits + rBytes.substring((i + 1) * 2));
}

// t = new BigInteger(rBytes, 16);
// return t;
return new BigInteger(rBytes.toString(), 16);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/thealgorithms/ciphers/AffineCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ private AffineCipher() {
*/
static String encryptMessage(char[] msg) {
// Cipher Text initially empty
String cipher = "";
StringBuilder cipher = new StringBuilder();
for (int i = 0; i < msg.length; i++) {
// Avoid space to be encrypted
/* applying encryption formula ( a * x + b ) mod m
{here x is msg[i] and m is 26} and added 'A' to
bring it in the range of ASCII alphabet [65-90 | A-Z] */
if (msg[i] != ' ') {
cipher += (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
cipher.append((char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A'));
} else { // else simply append space character
cipher += msg[i];
cipher.append(msg[i]);
}
}
return cipher;
return cipher.toString();
}

/**
Expand All @@ -56,7 +56,7 @@ static String encryptMessage(char[] msg) {
* @return the decrypted plaintext message
*/
static String decryptCipher(String cipher) {
String msg = "";
StringBuilder msg = new StringBuilder();
int aInv = 0;
int flag;

Expand All @@ -75,13 +75,13 @@ static String decryptCipher(String cipher) {
{here x is cipher[i] and m is 26} and added 'A'
to bring it in the range of ASCII alphabet [65-90 | A-Z] */
if (cipher.charAt(i) != ' ') {
msg += (char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A');
msg.append((char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A'));
} else { // else simply append space character
msg += cipher.charAt(i);
msg.append(cipher.charAt(i));
}
}

return msg;
return msg.toString();
}

// Driver code
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/thealgorithms/ciphers/Blowfish.java
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ public class Blowfish {
* @return String object which is a binary representation of the hex number passed as parameter
*/
private String hexToBin(String hex) {
String binary = "";
StringBuilder binary = new StringBuilder();
long num;
String binary4B;
int n = hex.length();
Expand All @@ -1089,9 +1089,9 @@ private String hexToBin(String hex) {
binary4B = "0000" + binary4B;

binary4B = binary4B.substring(binary4B.length() - 4);
binary += binary4B;
binary.append(binary4B);
}
return binary;
return binary.toString();
}

/**
Expand All @@ -1103,12 +1103,12 @@ private String hexToBin(String hex) {
*/
private String binToHex(String binary) {
long num = Long.parseUnsignedLong(binary, 2);
String hex = Long.toHexString(num);
StringBuilder hex = new StringBuilder(Long.toHexString(num));
while (hex.length() < (binary.length() / 4)) {
hex = "0" + hex;
hex.insert(0, "0");
}

return hex;
return hex.toString();
}

/**
Expand All @@ -1121,12 +1121,12 @@ private String binToHex(String binary) {
private String xor(String a, String b) {
a = hexToBin(a);
b = hexToBin(b);
String ans = "";
StringBuilder ans = new StringBuilder();
for (int i = 0; i < a.length(); i++) {
ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
ans.append((char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0'));
}
ans = binToHex(ans);
return ans;
ans = new StringBuilder(binToHex(ans.toString()));
return ans.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static String base2base(String n, int b1, int b2) {
int decimalValue = 0;
int charB2;
char charB1;
String output = "";
StringBuilder output = new StringBuilder();
// Go through every character of n
for (int i = 0; i < n.length(); i++) {
// store the character in charB1
Expand Down Expand Up @@ -167,15 +167,15 @@ public static String base2base(String n, int b1, int b2) {
// If the remainder is a digit < 10, simply add it to
// the left side of the new number.
if (decimalValue % b2 < 10) {
output = decimalValue % b2 + output;
output.insert(0, decimalValue % b2);
} // If the remainder is >= 10, add a character with the
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
else {
output = (char) ((decimalValue % b2) + 55) + output;
output.insert(0, (char) ((decimalValue % b2) + 55));
}
// Divide by the new base again
decimalValue /= b2;
}
return output;
return output.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public String convert(String numHex) {
*/
public String completeDigits(String binNum) {
final int byteSize = 8;
while (binNum.length() < byteSize) {
binNum = "0" + binNum;
StringBuilder binNumBuilder = new StringBuilder(binNum);
while (binNumBuilder.length() < byteSize) {
binNumBuilder.insert(0, "0");
}
binNum = binNumBuilder.toString();
return binNum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public FloydWarshall(int numberofvertices) {
public void floydwarshall(int[][] adjacencyMatrix) {
// Initialize the distance matrix with the adjacency matrix.
for (int source = 1; source <= numberofvertices; source++) {
for (int destination = 1; destination <= numberofvertices; destination++) {
distanceMatrix[source][destination] = adjacencyMatrix[source][destination];
}
System.arraycopy(adjacencyMatrix[source], 1, distanceMatrix[source], 1, numberofvertices);
}
for (int intermediate = 1; intermediate <= numberofvertices; intermediate++) {
for (int source = 1; source <= numberofvertices; source++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public static void main(String[] arg) {
SinglyLinkedList list = new SinglyLinkedList();
assert list.isEmpty();
assert list.size() == 0 && list.count() == 0;
assert list.toString().equals("");
assert list.toString().isEmpty();

/* Test insert function */
list.insertHead(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private static final class Node {
}

private final Node root;

public GenericTree() { // Constructor
Scanner scn = new Scanner(System.in);
root = createTreeG(null, 0, scn);
Expand Down Expand Up @@ -225,8 +226,6 @@ private void removeleaves(Node node) {
for (int i = 0; i < node.child.size(); i++) {
if (node.child.get(i).child.size() == 0) {
arr.add(i);
// node.child.remove(i);
// i--;
} else {
removeleaves(node.child.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.thealgorithms.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/thealgorithms/maths/NthUglyNumber.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.thealgorithms.maths;

import static java.util.Collections.singletonList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.lang3.tuple.MutablePair;

Expand All @@ -16,7 +17,7 @@
* - the base [2, 3, 5] ugly numbers are the same as base [5, 6, 2, 3, 5] ugly numbers
*/
public class NthUglyNumber {
private ArrayList<Long> uglyNumbers = new ArrayList<>(Arrays.asList(1L));
private ArrayList<Long> uglyNumbers = new ArrayList<>(singletonList(1L));
private ArrayList<MutablePair<Integer, Integer>> positions = new ArrayList<>();

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/thealgorithms/maths/VampireNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ static void test(int startValue, int stopValue) {
// System.out.println(i+ " "+ j);
if (isVampireNumber(i, j, true)) {
countofRes++;
res.append("" + countofRes + ": = ( " + i + "," + j + " = " + i * j + ")"
+ "\n");
res.append("").append(countofRes).append(": = ( ").append(i).append(",").append(j).append(" = ").append(i * j).append(")").append("\n");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.scheduling;

import java.util.Collections;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
Expand Down Expand Up @@ -72,9 +73,7 @@ public static Process[] scheduleProcesses(Process[] processes) {
int index = 0;
Process[] executionOrder = new Process[processes.length];

for (Process process : processes) {
waitingQueue.add(process);
}
Collections.addAll(waitingQueue, processes);

while (!waitingQueue.isEmpty() || !pq.isEmpty()) {
// Add processes that have arrived to the priority queue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.backtracking;

import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
Expand All @@ -11,7 +12,7 @@ public class NQueensTest {

@Test
public void testNQueens1() {
List<List<String>> expected = Arrays.asList(Arrays.asList("Q"));
List<List<String>> expected = singletonList(singletonList("Q"));
assertEquals(expected, NQueens.getNQueensArrangements(1));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.bitmanipulation;

import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
Expand All @@ -14,8 +15,8 @@ void testGenerateSubsetsWithTwoElements() {
int[] set = {1, 2};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(1));
expected.add(Arrays.asList(2));
expected.add(singletonList(1));
expected.add(singletonList(2));
expected.add(Arrays.asList(1, 2));

List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
Expand All @@ -27,7 +28,7 @@ void testGenerateSubsetsWithOneElement() {
int[] set = {3};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(3));
expected.add(singletonList(3));

List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
assertEquals(expected, result);
Expand All @@ -38,10 +39,10 @@ void testGenerateSubsetsWithThreeElements() {
int[] set = {4, 5, 6};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(4));
expected.add(Arrays.asList(5));
expected.add(singletonList(4));
expected.add(singletonList(5));
expected.add(Arrays.asList(4, 5));
expected.add(Arrays.asList(6));
expected.add(singletonList(6));
expected.add(Arrays.asList(4, 6));
expected.add(Arrays.asList(5, 6));
expected.add(Arrays.asList(4, 5, 6));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.datastructures.graphs;

import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

Expand Down Expand Up @@ -41,6 +42,6 @@ public void testAStarPathNotFound() {
public void testAStarSameNode() {
AStar.PathAndDistance result = AStar.aStar(0, 0, graph, heuristic);
assertEquals(0, result.getDistance(), "Expected distance from 0 to 0 is 0");
assertEquals(Arrays.asList(0), result.getPath(), "Expected path should only contain the start node");
assertEquals(singletonList(0), result.getPath(), "Expected path should only contain the start node");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.dynamicprogramming;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
Expand All @@ -10,7 +12,7 @@ public class AllConstructTest {

@Test
public void testAllConstructBasic() {
List<List<String>> expected = Arrays.asList(Arrays.asList("he", "l", "l", "o"));
List<List<String>> expected = singletonList(Arrays.asList("he", "l", "l", "o"));
List<List<String>> result = AllConstruct.allConstruct("hello", Arrays.asList("he", "l", "o"));
assertEquals(expected, result);
}
Expand All @@ -24,14 +26,14 @@ public void testAllConstructMultipleWays() {

@Test
public void testAllConstructNoWays() {
List<List<String>> expected = Arrays.asList();
List<List<String>> expected = emptyList();
List<List<String>> result = AllConstruct.allConstruct("abcdef", Arrays.asList("gh", "ijk"));
assertEquals(expected, result);
}

@Test
public void testAllConstructEmptyTarget() {
List<List<String>> expected = Arrays.asList(Arrays.asList());
List<List<String>> expected = singletonList(emptyList());
List<List<String>> result = AllConstruct.allConstruct("", Arrays.asList("a", "b", "c"));
assertEquals(expected, result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.dynamicprogramming;

import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
Expand All @@ -23,7 +24,7 @@ public void testCountNoOfWays() {
public void testNoPossibleAssignments() {
int totalTasks = 3;

List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(2), Arrays.asList(3));
List<List<Integer>> taskPerformed = Arrays.asList(singletonList(2), singletonList(3));

AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
int ways = assignment.countNoOfWays();
Expand All @@ -34,7 +35,7 @@ public void testNoPossibleAssignments() {
public void testSinglePersonMultipleTasks() {
int totalTasks = 3;

List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(1, 2, 3));
List<List<Integer>> taskPerformed = singletonList(Arrays.asList(1, 2, 3));

AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
int ways = assignment.countNoOfWays();
Expand All @@ -45,7 +46,7 @@ public void testSinglePersonMultipleTasks() {
public void testMultiplePeopleSingleTask() {
int totalTasks = 1;

List<List<Integer>> taskPerformed = Arrays.asList(Arrays.asList(1), Arrays.asList(1));
List<List<Integer>> taskPerformed = Arrays.asList(singletonList(1), singletonList(1));

AssignmentUsingBitmask assignment = new AssignmentUsingBitmask(taskPerformed, totalTasks);
int ways = assignment.countNoOfWays();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.thealgorithms.graph;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
Expand Down
Loading