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

Figure out how to calculate a check digit #46

Closed
waldoj opened this issue May 22, 2014 · 5 comments
Closed

Figure out how to calculate a check digit #46

waldoj opened this issue May 22, 2014 · 5 comments

Comments

@waldoj
Copy link
Member

waldoj commented May 22, 2014

The SCC sometimes provides a 7-digit ID, sometimes an 8-digit ID. It turns out that the eighth digit is a checksum. This is the SCC's code to calculate that checksum but, unfortunately, it's written in Natural. So we've got to figure out Natural sufficiently to recreate the code in Python.

@waldoj
Copy link
Member Author

waldoj commented May 22, 2014

It looks to me like is done as follows:

  1. If the first character is a letter, calculate it as 8.
  2. Add up the odd positions. (The 1st character, the 3rd character, etc.)
  3. Iterating through each of the even positions and:
    • multiplying it by 2
    • if the result is 2 digits, splitting it into two numbers (e.g., 12 becomes 1 and 2), and adding them together (e.g., 3).
  4. Add all of those together.
  5. Subtract that number from 100.
  6. The least significant digit is the checksum.

I have not tested this.

@waldoj
Copy link
Member Author

waldoj commented May 22, 2014

So, for example, 0431899.

0 + 3 + 8 + 9 = 20.

4 * 2 = 8
1 * 2 = 2
9 * 2 = 18 = 1 + 8 = 9

20 + 8 + 2 + 9 = 39

100 - 39 = 61

The checksum is 1.

...but the checksum is 4. So, that didn't work.

@CountCulture
Copy link

Got this working in Ruby:
https://gist.github.com/CountCulture/0dae80c19f8160159a9d

@waldoj
Copy link
Member Author

waldoj commented May 22, 2014

Oh, wonderful. I'll compare notes and see where my logic is wrong. :) Thank you!

@waldoj
Copy link
Member Author

waldoj commented May 23, 2014

OK, take two, with the addition of a new second step:

  1. If the first character is a letter, calculate it as 8.
  2. Move the last character to the second position.
  3. Add up the odd positions. (The 1st character, the 3rd character, etc.)
  4. Iterating through each of the even positions and:
    • multiplying it by 2
    • if the result is 2 digits, splitting it into two numbers (e.g., 12 becomes 1 and 2), and adding them together (e.g., 3).
  5. Add all of those together.
  6. Subtract that number from 100.
  7. The least significant digit is the checksum.

So, for example, 0431899.

0943189

0 + 4 + 1 + 9 = 14

9 * 2 = 18 = 1 + 8 = 9
3 * 2 = 6
8 * 2 = 16 = 1 + 6 = 7

14 + 9 + 6 + 7 = 36

100 - 36 = 64

The checksum is 4.

Great, so that worked. Let's try with another one: F004214

8004214

8400421

8 + 0 + 4 + 1 = 13

4 * 2 = 8
0 * 2 = 0
2 * 2 = 4

13 + 8 + 0 + 4 = 25

100 - 25 = 75

The checksum is 5. Bingo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants