Skip to content

Commit

Permalink
fix: Changes Requested Done
Browse files Browse the repository at this point in the history
* Removed the extra empty lines

* Respected the alphabetical order of the README.md file and insert Luhn Mod N to the correct position

* Checked the code duplication reported by Sonar
  • Loading branch information
Karim-Ashraf1 committed Dec 8, 2024
1 parent 2900bfc commit 3169576
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 213 deletions.
244 changes: 116 additions & 128 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,122 @@ public class LinearSearchSnippet {
}
}
```
### Luhn Mod N


```java
public class LuhnModnSnippet {

private static final String CODE_POINTS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/**
* Throws an exception based on the specified mode, character, or code point.
*
* @param mode the mode of operation; determines which exception to throw
* @param character the character to include in the exception message (used if mode is 1)
* @param codePoint the code point to include in the exception message (used if mode is 2)
* @throws IllegalArgumentException if mode is 1, message including the invalid character,
* or if mode is 2, message including the invalid code point
*/
public static void throwException(int mode, char character, int codePoint) {
switch (mode) {
case 1:
throw new IllegalArgumentException("Invalid character: " + character);
case 2:
throw new IllegalArgumentException("Invalid code point: " + codePoint);
default:
return;
}
}

/**
* Converts a character to its corresponding code point index.
*
* @param character the input character
* @return the code point index
* @throws IllegalArgumentException if the character is invalid
*/
public static int codePointFromCharacter(char character) {
if (CODE_POINTS.indexOf(character) == -1) {
throwException(1, character, 0);
}
return CODE_POINTS.indexOf(character);
}

/**
* Converts a code point index to its corresponding character.
*
* @param codePoint the code point index
* @return the corresponding character
* @throws IllegalArgumentException if the code point is out of range
*/
public static char characterFromCodePoint(int codePoint) {
if (codePoint < 0 || codePoint >= CODE_POINTS.length()) {
throwException(2, '\0', codePoint);
}
return CODE_POINTS.charAt(codePoint);
}

/**
* Returns the number of valid input characters.
*
* @return the total count of valid input characters in {@code CODE_POINTS}
*/
public static int numberOfValidInputCharacters() {
return CODE_POINTS.length();
}

/**
* Helper method to calculate the sum for both check character generation and validation.
*
* @param input the input string
* @param factorStart the initial factor to start with (1 or 2)
* @return the calculated sum, reminder, and the numberOfValidInputCharacters
*/
private static int[] calculateSum(String input, int factorStart) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}

int factor = factorStart;
int sum = 0;
int n = numberOfValidInputCharacters();

for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;
factor = (factor == 2) ? 1 : 2;
addend = (addend / n) + (addend % n);
sum += addend;
}
return new int[]{sum, sum % n, n};
}

/**
* Generates a check character for the given input string using the Luhn mod N algorithm.
*
* @param input the input string (non-empty)
* @return the generated check character
* @throws IllegalArgumentException if the input is null or empty
*/
public static char generateCheckCharacter(String input) {
int[] result = calculateSum(input, 2);
return characterFromCodePoint((result[2] - result[1]) % result[2]);
}

/**
* Validates a check character by applying the Luhn mod N algorithm.
*
* @param input the input string (including the check character)
* @return true if the input passes validation, false otherwise
* @throws IllegalArgumentException if the input is null or empty
*/
public static boolean validateCheckCharacter(String input) {
int[] result = calculateSum(input, 1);
return (result[1] == 0);
}
}
```

### Merge Sort

Expand Down Expand Up @@ -435,134 +551,6 @@ public class SieveOfEratosthenesSnippet {
}
}
```
### Luhn Mod N


```java
public class LuhnModnSnippet {


private LuhnModnSnippet() {
throw new IllegalStateException("LuhnModnSnippet class");
}


private static final String CODE_POINTS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


/**
* Generates a check character using the Luhn mod N algorithm.
*
* @param character the input string consisting of valid alphanumeric characters
* @return the generated check character
* @throws IllegalArgumentException if the input contains invalid characters
*/
public static int codePointFromCharacter(char character) {
int index = CODE_POINTS.indexOf(character);
if (index == -1) {
throw new IllegalArgumentException("Invalid character: " + character);
}
return index;
}


/**
* Converts a code point to its corresponding character.
*
* @param codePoint the code point to be converted
* @return the character representation of the code point
* @throws IllegalArgumentException if the code point is out of range.
*/
public static char characterFromCodePoint(int codePoint) {
if (codePoint < 0 || codePoint >= CODE_POINTS.length()) {
throw new IllegalArgumentException("Invalid code point: " + codePoint);
}
return CODE_POINTS.charAt(codePoint);
}


public static int numberOfValidInputCharacters() {
return CODE_POINTS.length();
}


/**
* Generates a check character for the given input string using the Luhn mod N algorithm.
*
* @param input the input string (non-empty)
* @return the generated check character
* @throws IllegalArgumentException if the input is null or empty
*/
public static char generateCheckCharacter(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 2;
int sum = 0;
int n = numberOfValidInputCharacters();


for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;


factor = (factor == 2) ? 1 : 2;


addend = (addend / n) + (addend % n);
sum += addend;
}


int remainder = sum % n;
int checkCodePoint = (n - remainder) % n;


return characterFromCodePoint(checkCodePoint);
}


/**
* Validates a check character by applying the Luhn mod N algorithm.
*
* @param input the input string (including the check character)
* @return true if the input passes validation, false otherwise
* @throws IllegalArgumentException if the input is null or empty
*/
public static boolean validateCheckCharacter(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 1;
int sum = 0;
int n = numberOfValidInputCharacters();


for (int i = input.length() - 1; i >= 0; i--) {
int codePoint = codePointFromCharacter(input.charAt(i));
int addend = factor * codePoint;


factor = (factor == 2) ? 1 : 2;


addend = (addend / n) + (addend % n);
sum += addend;
}


int remainder = sum % n;


return (remainder == 0);
}
}
```

## Array

Expand Down
Loading

0 comments on commit 3169576

Please sign in to comment.