Why is this valid USPS tracking number not verified against their specification?

I am writing a gem to detect tracking numbers (called tracking_number , natch). He searches for text for valid tracking formats, and then runs these formats by calculating the checksum, as indicated in each relevant service specification, to determine the valid numbers.

The other day, I sent a letter using USPS Certified Mail, received an accompanying tracking number from USPS and submitted it to my gem, and it did not pass the verification. I'm pretty sure I'm doing the calculation correctly, but I have run out of ideas.

The number is confirmed using USS Code 128, as described in section 2.8 (page 15) of the following document: http://www.usps.com/cpim/ftp/pubs/pub109.pdf

The tracking number I received from the mail branch was "7196 9010 7560 0307 7385", and the code that I use to calculate the check digit is:

def valid_checksum?
  # tracking number doesn't have spaces at this point
  chars = self.tracking_number.chars.to_a
  check_digit = chars.pop

  total = 0
  chars.reverse.each_with_index do |c, i|
    x = c.to_i
    x *= 3 if i.even?
    total += x
  end

  check = total % 10
  check = 10 - check unless (check.zero?)
  return true if check == check_digit.to_i
end

According to my calculations, based on the specification provided, the last digit must be 3 in order to be valid. However, Google’s auto-detection of the tracking number picks up the number of fines as is, so I can only assume that I am doing something wrong.

+3
source share
2 answers

From my manual calculations, it should match what your code does:

posn: 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2   sum  mult
even:  7     9     9     1     7     6     0     0     7     8    54   162
 odd:     1     6     0     0     5     0     3     7     3       25    25
                                                                       ===
                                                                       187

Therefore, the check digit should be three.

, , , , .

, , , , USPS, .


, 91, , , , 91 . , , :

posn: 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2   sum  mult
even:  9     7     9     9     1     7     6     0     0     7     8    63   189
 odd:     1     1     6     0     0     5     0     3     7     3       26    26
                                                                             ===
                                                                             215

5. , , , , .

, USPS .

+7

Ruby, , 3 ; , 3. . . 20-21.

() . Python 7 , 3 :


#!/usr/bin/python
'check tracking number checksum'
import sys
def check(number = sys.argv[1:]):
 to_check = ''.join(number).replace('-', '')
 print to_check
 even = sum(map(int, to_check[-2::-2]))
 odd = sum(map(int, to_check[-3::-2]))
 print even * 3 + odd
if __name__ == '__main__':
 check(sys.argv[1:])

[ ] , :


jcomeau@intrepid:~$ /tmp/track.py 7196 9010 7560 0307 7385
False
jcomeau@intrepid:~$ /tmp/track.py 91 7196 9010 7560 0307 7385
True
jcomeau@intrepid:~$ /tmp/track.py 71123456789123456787
True
jcomeau@intrepid:~$ cat /tmp/track.py 
#!/usr/bin/python
'check tracking number checksum'
import sys
def check(number):
 to_check = ''.join(number).replace('-', '')
 even = sum(map(int, to_check[-2::-2]))
 odd = sum(map(int, to_check[-3::-2]))
 checksum = even * 3 + odd
 checkdigit = (10 - (checksum % 10)) % 10
 return checkdigit == int(to_check[-1])
if __name__ == '__main__':
 print check(''.join(sys.argv[1:]).replace('-', ''))
+1

Source: https://habr.com/ru/post/1793763/


All Articles