Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ParanoidUser committed Jan 8, 2025
1 parent df4b602 commit af09bf5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
- [Minutes to Midnight](minutes-to-midnight)
- [Missing Alphabet](missing-alphabet)
- [Moduli number system](moduli-number-system)
- [Modulus 11 - Check Digit](modulus-11-check-digit)
- [More Zeros than Ones](more-zeros-than-ones)
- [Most Frequent Weekdays](most-frequent-weekdays)
- [Moves in squared strings (II)](moves-in-squared-strings-ii)
Expand Down
16 changes: 9 additions & 7 deletions kata/6-kyu/modulus-11-check-digit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

Some numbers are more important to get right during data entry than others: a common example is product codes.

To reduce the possibility of mistakes, product codes can be crafted in such a way that simple errors are detected. This is done by calculating a single-digit value based on the product number, and then appending that digit to the product number to arrive at the product code.
To reduce the possibility of mistakes, product codes can be crafted in such a way that simple errors are detected. This is done by
calculating a single-digit value based on the product number, and then appending that digit to the product number to arrive at the product
code.

When the product code is checked, the check digit value is stripped off and recalculated. If the supplied value does not match the recalculated value, the product code is rejected.
When the product code is checked, the check digit value is stripped off and recalculated. If the supplied value does not match the
recalculated value, the product code is rejected.

A simple scheme for generating self-check digits, described here, is called Modulus 11 Self-Check.


## Calculation method

Each digit in the product number is assigned a multiplication factor. The factors are assigned ***from right to left***, starting at `2` and counting up. For numbers longer than six digits, the factors restart at `2` after `7` is reached. The product of each digit and its factor is calculated, and the products summed. For example:
Each digit in the product number is assigned a multiplication factor. The factors are assigned ***from right to left***, starting at `2` and
counting up. For numbers longer than six digits, the factors restart at `2` after `7` is reached. The product of each digit and its factor
is calculated, and the products summed. For example:

```
digit : 1 6 7 7 0 3 6 2 5
Expand All @@ -30,12 +34,10 @@ Then the sum of the products is divided by the prime number `11`. The remainder

The result is the **check digit**.


## Your task

Your task is to implement this algorithm and return the input number with the correct check digit appended.


## Examples

```
Expand All @@ -46,4 +48,4 @@ remainder = 81 mod 11 = 4
check digit = 11 - 4 = 7
output: "0365327"
```
```
8 changes: 8 additions & 0 deletions kata/6-kyu/modulus-11-check-digit/main/Modulus11.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import static java.util.stream.IntStream.range;

interface Modulus11 {
static String addCheckDigit(String s) {
int rem = range(0, s.length()).reduce(0, (r, i) -> r + (s.charAt(s.length() - i - 1) - '0') * (i % 6 + 2)) % 11;
return s + (rem == 1 ? "X" : (11 - rem) % 11);
}
}
19 changes: 19 additions & 0 deletions kata/6-kyu/modulus-11-check-digit/test/Modulus11Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class Modulus11Test {
@ParameterizedTest
@CsvSource(textBlock = """
2356, 23566
6789, 6789X
036532, 0365327
12388878, 123888782
111111111, 1111111118
9735597355, 97355973550
""")
void sample(String number, String check) {
assertEquals(check, Modulus11.addCheckDigit(number));
}
}

0 comments on commit af09bf5

Please sign in to comment.