Skip to content

Instantly share code, notes, and snippets.

@globby
Created March 4, 2014 01:23
Show Gist options
  • Save globby/9338424 to your computer and use it in GitHub Desktop.
Save globby/9338424 to your computer and use it in GitHub Desktop.
A slightly LESS confusing version of the Verhoeff algorithm
def verhoeff(string):
string, c = list(string)[::-1], 0
d = (
(0,1,2,3,4,5,6,7,8,9),
(1,2,3,4,0,6,7,8,9,5),
(2,3,4,0,1,7,8,9,5,6),
(3,4,0,1,2,8,9,5,6,7),
(4,0,1,2,3,9,5,6,7,8),
(5,9,8,7,6,0,4,3,2,1),
(6,5,9,8,7,1,0,4,3,2),
(7,6,5,9,8,2,1,0,4,3),
(8,7,6,5,9,3,2,1,0,4),
(9,8,7,6,5,4,3,2,1,0))
p = (
(0,1,2,3,4,5,6,7,8,9),
(1,5,7,6,2,8,3,0,9,4),
(5,8,0,3,7,9,6,1,4,2),
(8,9,1,6,0,4,3,5,2,7),
(9,4,5,3,1,2,6,8,7,0),
(4,2,8,6,5,7,3,9,0,1),
(2,7,9,3,8,0,6,4,1,5),
(7,0,4,6,9,1,3,2,5,8))
inv = (0,4,3,2,1,5,6,7,8,9)
for i, v in enumerate(string):
c = d[c][p[i%8][int(v)]]
return c == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment