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

feature: Added LuhnModnSnippet class to Algorithm package #236

Merged
merged 2 commits into from
Jan 6, 2025
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
91 changes: 91 additions & 0 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
115 changes: 115 additions & 0 deletions src/main/java/algorithm/LuhnModnSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package algorithm;

/**
* LuhnModnSnippet.
*/
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);
}
}
85 changes: 85 additions & 0 deletions src/test/java/algorithm/LuhnModnSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package algorithm;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests for {@link LuhnModnSnippet#generateCheckCharacter(String)} and
* {@link LuhnModnSnippet#validateCheckCharacter(String)}.
*/
class LuhnModnSnippetTest {

/**
* Tests the full process of generating and validating a check character.
*/
@ParameterizedTest
@MethodSource("validInputProvider")
public void testGenerateCheckCharacter(String input) {
char checkCharacter = LuhnModnSnippet.generateCheckCharacter(input);
String fullInput = input + checkCharacter;
assertTrue(LuhnModnSnippet.validateCheckCharacter(fullInput),
"Validation should pass for the generated check character.");
}

@ParameterizedTest
@MethodSource("invalidInputProvider")
public void testInvalidInputs(String input, Character checkCharacter, boolean throwException) {
if (throwException) {
assertThrows(IllegalArgumentException.class, () ->
LuhnModnSnippet.generateCheckCharacter(input),
"Exception should be thrown for invalid input.");
assertThrows(IllegalArgumentException.class, () ->
LuhnModnSnippet.validateCheckCharacter(input),
"Exception should be thrown for invalid input.");
} else {
String fullInput = input + checkCharacter;
assertFalse(LuhnModnSnippet.validateCheckCharacter(fullInput),
"Validation should fail for a mismatched check character.");
}
}

private static Stream<String> validInputProvider() {
return Stream.of(
"HELLO",
"12345",
"A1B2C3"
);
}

private static Stream<Arguments> invalidInputProvider() {
return Stream.of(
Arguments.of("", null, true),
Arguments.of("WORLD", 'A', false)
);
}
}