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 758ab02
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 204 deletions.
219 changes: 91 additions & 128 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,97 @@ public class LinearSearchSnippet {
}
}
```
### Luhn Mod N


```java
public class LuhnModnSnippet {

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) {
if (CODE_POINTS.indexOf(character) == -1) {
throw new IllegalArgumentException("Invalid character: " + character);
}
return CODE_POINTS.indexOf(character);
}

/**
* 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();
}

/**
* 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 +526,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
85 changes: 23 additions & 62 deletions src/main/java/algorithm/LuhnModnSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,15 @@
* SOFTWARE.
*/


package algorithm;


/**
* LuhnModnSnippet.
*/
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.
*
Expand All @@ -48,14 +39,12 @@ private LuhnModnSnippet() {
* @throws IllegalArgumentException if the input contains invalid characters
*/
public static int codePointFromCharacter(char character) {
int index = CODE_POINTS.indexOf(character);
if (index == -1) {
if (CODE_POINTS.indexOf(character) == -1) {
throw new IllegalArgumentException("Invalid character: " + character);
}
return index;
return CODE_POINTS.indexOf(character);
}


/**
* Converts a code point to its corresponding character.
*
Expand All @@ -70,50 +59,47 @@ public static char characterFromCodePoint(int 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.
* Helper method to calculate the sum for both check character generation and validation.
*
* @param input the input string (non-empty)
* @return the generated check character
* @throws IllegalArgumentException if the input is null or empty
* @param input the input string
* @param factorStart the initial factor to start with (1 or 2)
* @return the calculated sum, reminder, and the numberOfValidInputCharacters
*/
public static char generateCheckCharacter(String input) {
private static int[] calculateSum(String input, int factorStart) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty");
}


int factor = 2;
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;
}


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


return characterFromCodePoint(checkCodePoint);
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.
Expand All @@ -123,32 +109,7 @@ public static char generateCheckCharacter(String input) {
* @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);
int[] result = calculateSum(input, 1);
return (result[1] == 0);
}
}
}
Loading

0 comments on commit 758ab02

Please sign in to comment.