Re: [k4] Nick Psaris, author of Q-Tips, to speak at Kx Community NYC Meetup

Congratulations to Anjani Singh for winning the coding competition.

The implementation used the following techniques to win:

  1. placing an attribute on the key of the character mapping dictionary

  2. vectorizing the conversion from character to number by first razing the list of VINs

  3. reducing the number of computations by skipping invalid checkdigits

validvin:{
 m:(`u#“0123456789ABCDEFGHJKLMNPRSTUVWXYZ”)!0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 7 9 2 3 4 5 6 7 8 9f;
 x:$[t:type x;enlist x;x];
 if[count x@:k:where j:x[;8]in c:“0123456789X”;j[k]:x0[8+17*til count x]=c"i"$mod[;11f](0N 17#m x0:raze x)$8 7 6 5 4 3 2 10 0 9 8 7 6 5 4 3 2f];
 $[t;first j;j]}

q)vins:(1000000#17)?:.Q.nA except “IOQ”
q)\ts validvin vins

another technique which could make this implementation even faster is to use a lookup table instead of the ‘mod’ operator.

a cleaned up solution using all these techniques:

validvin:{
 if[type x;:first .z.s enlist x];
 m:(`u#.Q.nA except “IOQ”)!“f”$(40#til 10) _/ 31 30 28 26 20 19 10;
 w:8 7 6 5 4 3 2 10 0 9 8 7 6 5 4 3 2f;
 c:“0123456789X”;
 v:x[;8]in c;
 if[count k:where v;v[k]:r[8+17*til count x]=(802#c)“j”$(17 cut m r:raze x@:k)$w];
 v}

Kostyantyn Oliynyk came in a close second by flipping the list of VINs before performing the weighted sum.

Nick