6 kyu

Modulus 11 - Check Digit

366 of 876bmw318mt

Description:

Check Digits

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.

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:

digit     :  1    6    7    7    0    3    6    2    5
factor    :  4    3    2    7    6    5    4    3    2
           ---  ---  ---  ---  ---  ---  ---  ---  ---
             4 + 18 + 14 + 49 +  0 + 15 + 24 +  6 + 10 = 140

Then the sum of the products is divided by the prime number 11. The remainder is inspected, and:

  • if the remainder is 0, the check digit is also 0
  • if the remainder is 1, the check digit is replaced by an uppercase X
  • for all others, the remainder is subtracted from 11

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

input: "036532"

product sum = 2*2 + 3*3 + 5*4 + 6*5 + 3*6 + 0*7 = 81
remainder   = 81 mod 11 = 4
check digit = 11 - 4 = 7

output: "0365327"
Algorithms

More By Author:

Check out these other kata created by bmw318mt

Stats:

CreatedJan 6, 2016
PublishedJan 7, 2016
Warriors Trained1587
Total Skips33
Total Code Submissions3097
Total Times Completed876
C# Completions112
Python Completions366
Ruby Completions39
JavaScript Completions250
Java Completions153
CoffeeScript Completions7
COBOL Completions3
Prolog Completions4
Total Stars29
% of votes with a positive feedback rating94% of 187
Total "Very Satisfied" Votes169
Total "Somewhat Satisfied" Votes15
Total "Not Satisfied" Votes3
Total Rank Assessments16
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • bmw318mt Avatar
  • g964 Avatar
  • anter69 Avatar
  • smile67 Avatar
  • kazk Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • kirull Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad