diff --git a/kata/6-kyu/index.md b/kata/6-kyu/index.md index d9c829df..982ad2a0 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -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) diff --git a/kata/6-kyu/modulus-11-check-digit/README.md b/kata/6-kyu/modulus-11-check-digit/README.md index 54ce6b44..8211f8b5 100644 --- a/kata/6-kyu/modulus-11-check-digit/README.md +++ b/kata/6-kyu/modulus-11-check-digit/README.md @@ -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 @@ -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 ``` @@ -46,4 +48,4 @@ remainder = 81 mod 11 = 4 check digit = 11 - 4 = 7 output: "0365327" -``` +``` \ No newline at end of file diff --git a/kata/6-kyu/modulus-11-check-digit/main/Modulus11.java b/kata/6-kyu/modulus-11-check-digit/main/Modulus11.java new file mode 100644 index 00000000..b4817771 --- /dev/null +++ b/kata/6-kyu/modulus-11-check-digit/main/Modulus11.java @@ -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); + } +} \ No newline at end of file diff --git a/kata/6-kyu/modulus-11-check-digit/test/Modulus11Test.java b/kata/6-kyu/modulus-11-check-digit/test/Modulus11Test.java new file mode 100644 index 00000000..695f2e59 --- /dev/null +++ b/kata/6-kyu/modulus-11-check-digit/test/Modulus11Test.java @@ -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)); + } +} \ No newline at end of file