English subtitles

← Luhns Algorithm Cont Solution - Software Testing

Get Embed Code
1 Language

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. We've still got the code the I wrote for the Luhn checksum.
  2. And this is kind of a redundant bad code but I wrote it quickly and I think it works.
  3. So we have the Luhn checksum function.
  4. And the first thing it does is looks at the length of it's argument.
  5. That is to say, it looks at the length of the string over which it's going to compute a checksum.
  6. It's also going to initialize the checksum variable to be 0.
  7. Now, if we're computing a checksum over an even number of digits,
  8. the remainder mod 2 is going to come out to 0.
  9. So in this case, we're going to loop over the digits and the checksum.
  10. That is the range(l) goes from 0 to length -1.
  11. And here what I'm going to do is add 1 to that number
  12. so that we can get a 1-base numbering system like I showed you
  13. in the explanation of Luhns algorithm.
  14. So here now, if we're at digit 0 in Python we're at digit number 1
  15. in the 1-base human number system and so if that comes out to be 0 when moded with 2
  16. now with an even number digit and so in that case we're going to add
  17. the value of that digit into the sum; otherwise, we're going to call this Luhn digit function
  18. on the same value and the Luhn digit function is just sort of the obvious thing.
  19. It multiplies the number by 2. If it's greater than 9, it returns 9 less than the number.
  20. Otherwise, it just returns the number.
  21. Now, the other case is that we're checksumming an odd number of digits.
  22. In that case, we have exactly the same logic with the even and odd cases flipped around.
  23. So here we're taking the Python zero-based digit adding 1 to it to get a human
  24. 1-based numbering system moding with 2.
  25. If it comes out to be even, we're doing the Luhn digit computation.
  26. If it's odd, then we're just adding in the value of the number.
  27. And finally, we get a checksum that we've accumulated here.
  28. And we take that mod 10 and that gives us our actual checksum value that we're going to use.
  29. So now, here's the algorithm for generating a valid random credit card number.
  30. So the inputs are remember a prefix and a total length in digits.
  31. So the number of random digits we're going to generate is the total length
  32. minus the length of the prefix minus 1 for the checksum digit.
  33. Now we're just going to assert that that's greater than 0 just to be careful.
  34. And now we're going to set N as our credit card number that's in progress.
  35. So we're going to initialize that to be just the prefix.
  36. Now for the number of random digits we have,
  37. we're just going to make up a random digit and append it to the string.
  38. Finally, we're going to add a zero checksum to it and compute the Luhn checksum.
  39. If that Luhn checksum comes out to be zero, we're done.
  40. If that Luhn checksum comes out to be non-zero, we have to do the little inversion
  41. that I talked about where we take the checksum where we subtract the checksum from 10
  42. and now what this little bit of logic does is it strips off the last character.
  43. That's the zero we added from the credit card number and adds on the checksum number.
  44. So that's how easy it is to generate a valid credit card number.
  45. And now there is one valid function just to check if the Luhn checksum is 0.