Skip to content

Commit

Permalink
Merge branch 'master' into bandwidth_new_algo
Browse files Browse the repository at this point in the history
  • Loading branch information
siriak authored Oct 25, 2024
2 parents ceaa91e + 0f1dcbe commit 4e2e3cf
Show file tree
Hide file tree
Showing 165 changed files with 6,531 additions and 1,247 deletions.
69 changes: 69 additions & 0 deletions DIRECTORY.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.2</version>
<version>5.11.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -31,7 +31,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.2</version>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -43,15 +43,15 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.14.1</version>
<version>5.14.2</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.2</version>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -132,7 +132,7 @@
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.8.6.4</version>
<version>4.8.6.5</version>
<configuration>
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
<includeTests>true</includeTests>
Expand Down
3 changes: 0 additions & 3 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
</Match>
<!-- fb-contrib -->
<Match>
<Bug pattern="OCP_OVERLY_CONCRETE_PARAMETER" />
</Match>
<Match>
<Bug pattern="LSC_LITERAL_STRING_COMPARISON" />
</Match>
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/com/thealgorithms/backtracking/ArrayCombination.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,44 @@
import java.util.List;

/**
* Finds all combinations of 0...n-1 of length k
* This class provides methods to find all combinations of integers from 0 to n-1
* of a specified length k using backtracking.
*/
public final class ArrayCombination {
private ArrayCombination() {
}

/**
* Finds all combinations of length k of 0..n-1 using backtracking.
* Generates all possible combinations of length k from the integers 0 to n-1.
*
* @param n Number of the elements.
* @param k Length of the combination.
* @return A list of all combinations of length k.
* @param n The total number of elements (0 to n-1).
* @param k The desired length of each combination.
* @return A list containing all combinations of length k.
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
*/
public static List<List<Integer>> combination(int n, int k) {
if (n < 0 || k < 0 || k > n) {
throw new IllegalArgumentException("Wrong input.");
throw new IllegalArgumentException("Invalid input: n must be non-negative, k must be non-negative and less than or equal to n.");
}

List<List<Integer>> combinations = new ArrayList<>();
combine(combinations, new ArrayList<>(), 0, n, k);
return combinations;
}

/**
* A helper method that uses backtracking to find combinations.
*
* @param combinations The list to store all valid combinations found.
* @param current The current combination being built.
* @param start The starting index for the current recursion.
* @param n The total number of elements (0 to n-1).
* @param k The desired length of each combination.
*/
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
if (current.size() == k) { // Base case: combination found
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
// Base case: combination found
if (current.size() == k) {
combinations.add(new ArrayList<>(current));
return;
}

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -95,7 +96,7 @@ public static void removeWord(char[][] puzzle, String word, int row, int col, bo
* @param words The list of words to be placed.
* @return true if the crossword is solved, false otherwise.
*/
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
public static boolean solveCrossword(char[][] puzzle, Collection<String> words) {
// Create a mutable copy of the words list
List<String> remainingWords = new ArrayList<>(words);

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/BaconianCipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.thealgorithms.ciphers;

import java.util.HashMap;
import java.util.Map;

/**
* The Baconian Cipher is a substitution cipher where each letter is represented
* by a group of five binary digits (A's and B's). It can also be used to hide
* messages within other texts, making it a simple form of steganography.
* https://en.wikipedia.org/wiki/Bacon%27s_cipher
*
* @author Bennybebo
*/
public class BaconianCipher {

private static final Map<Character, String> BACONIAN_MAP = new HashMap<>();
private static final Map<String, Character> REVERSE_BACONIAN_MAP = new HashMap<>();

static {
// Initialize the Baconian cipher mappings
String[] baconianAlphabet = {"AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA", "AABAB", "AABBA", "AABBB", "ABAAA", "ABAAB", "ABABA", "ABABB", "ABBAA", "ABBAB", "ABBBA", "ABBBB", "BAAAA", "BAAAB", "BAABA", "BAABB", "BABAA", "BABAB", "BABBA", "BABBB", "BBAAA", "BBAAB"};
char letter = 'A';
for (String code : baconianAlphabet) {
BACONIAN_MAP.put(letter, code);
REVERSE_BACONIAN_MAP.put(code, letter);
letter++;
}

// Handle I/J as the same letter
BACONIAN_MAP.put('I', BACONIAN_MAP.get('J'));
REVERSE_BACONIAN_MAP.put(BACONIAN_MAP.get('I'), 'I');
}

/**
* Encrypts the given plaintext using the Baconian cipher.
*
* @param plaintext The plaintext message to encrypt.
* @return The ciphertext as a binary (A/B) sequence.
*/
public String encrypt(String plaintext) {
StringBuilder ciphertext = new StringBuilder();
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Remove non-letter characters

for (char letter : plaintext.toCharArray()) {
ciphertext.append(BACONIAN_MAP.get(letter));
}

return ciphertext.toString();
}

/**
* Decrypts the given ciphertext encoded in binary (A/B) format using the Baconian cipher.
*
* @param ciphertext The ciphertext to decrypt.
* @return The decrypted plaintext message.
*/
public String decrypt(String ciphertext) {
StringBuilder plaintext = new StringBuilder();

for (int i = 0; i < ciphertext.length(); i += 5) {
String code = ciphertext.substring(i, i + 5);
if (REVERSE_BACONIAN_MAP.containsKey(code)) {
plaintext.append(REVERSE_BACONIAN_MAP.get(code));
} else {
throw new IllegalArgumentException("Invalid Baconian code: " + code);
}
}

return plaintext.toString();
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/DiffieHellman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.thealgorithms.ciphers;

import java.math.BigInteger;

public final class DiffieHellman {

private final BigInteger base;
private final BigInteger secret;
private final BigInteger prime;

// Constructor to initialize base, secret, and prime
public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) {
// Check for non-null and positive values
if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {
throw new IllegalArgumentException("Base, secret, and prime must be non-null and positive values.");
}
this.base = base;
this.secret = secret;
this.prime = prime;
}

// Method to calculate public value (g^x mod p)
public BigInteger calculatePublicValue() {
// Returns g^x mod p
return base.modPow(secret, prime);
}

// Method to calculate the shared secret key (otherPublic^secret mod p)
public BigInteger calculateSharedSecret(BigInteger otherPublicValue) {
if (otherPublicValue == null || otherPublicValue.signum() <= 0) {
throw new IllegalArgumentException("Other public value must be non-null and positive.");
}
// Returns b^x mod p or a^y mod p
return otherPublicValue.modPow(secret, prime);
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.thealgorithms.ciphers;

public final class MonoAlphabetic {

private MonoAlphabetic() {
throw new UnsupportedOperationException("Utility class");
}

// Encryption method
public static String encrypt(String data, String key) {
if (!data.matches("[A-Z]+")) {
throw new IllegalArgumentException("Input data contains invalid characters. Only uppercase A-Z are allowed.");
}
StringBuilder sb = new StringBuilder();

// Encrypt each character
for (char c : data.toCharArray()) {
int idx = charToPos(c); // Get the index of the character
sb.append(key.charAt(idx)); // Map to the corresponding character in the key
}
return sb.toString();
}

// Decryption method
public static String decrypt(String data, String key) {
StringBuilder sb = new StringBuilder();

// Decrypt each character
for (char c : data.toCharArray()) {
int idx = key.indexOf(c); // Find the index of the character in the key
if (idx == -1) {
throw new IllegalArgumentException("Input data contains invalid characters.");
}
sb.append(posToChar(idx)); // Convert the index back to the original character
}
return sb.toString();
}

// Helper method: Convert a character to its position in the alphabet
private static int charToPos(char c) {
return c - 'A'; // Subtract 'A' to get position (0 for A, 1 for B, etc.)
}

// Helper method: Convert a position in the alphabet to a character
private static char posToChar(int pos) {
return (char) (pos + 'A'); // Add 'A' to convert position back to character
}
}
Loading

0 comments on commit 4e2e3cf

Please sign in to comment.