English subtitles

← Luhns Algorithm Cont - Software Testing

Get Embed Code
1 Language

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

  1. Here's a look at what you're going to do in a little bit more detail.
  2. Remember that what you're writing is a random tester
  3. for the credit card transaction processing system.
  4. The core of this random tester is going to be a function called generate,
  5. which is what you're going to implement, so generate is going to take two parameters.
  6. The first one is the prefix, which is going to correspond to the issuer identifier.
  7. And this is a sequence of digits which is given to you, which has to appear at the start
  8. of the credit card number that you generate.
  9. The second parameter is the length. That is to say, an integer parameter that determines the total length
  10. in digits of the credit card number that you're going to generate.
  11. So your job then is to construct your credit card number,
  12. which has the given issuer identifier or prefix, has the required total length,
  13. has completely random integers in the middle here, and has a valid checksum digit
  14. as computed using Luhn's algorithm plus this little procedure for turning the Luhn checksum
  15. into a digit, which makes an overall credit card number's checksum
  16. come out to be 0 and therefore be valid.
  17. So let's just look at the code quick. So what you're writing is a function called generate.
  18. It takes the prefix and the length and so you're code is going to go here.
  19. And so we might call it like this. We might say the prefix is 372542.
  20. This happens to be the prefix for a credit card that I have in my pocket.
  21. and a 15-digit credit card number and now you're going to create a valid credit card number
  22. with the required properties.
  23. To do this, you're probably going to want to implement a function called Lahn checksum
  24. which computes the checksum of a number and this axillary function is-luhn-valid simply checks
  25. if the Lahn checksum or a given input is 0.
  26. what I'd like you to do is implement that now.
  27. If you get totally stuck and you like kind of a hint, there's something you can do
  28. which is look up Lahn's algorithm on Wikipedia.
  29. The reason that's kind of cheating is because on this Wikipedia page for Luhn's algorithm
  30. they actually will give you a Python code that works.
  31. But don't go there unless you actually want to see the real answer.
  32. Okay. Take a few minutes now, write the code, and what our API will do is use it
  33. to generate a number of credit cards and check if they're valid.
  34. If they're not valid, it will give you some indication on that the problem is.
  35. And if they are valid, then it will tell you what the N and you can move on to the solution.
  36. Before I let you go hack on this, I just want to give you a quick reminder about something
  37. that should save you some trouble so in the discussion I did here about Luhn's algorithm
  38. I assumed that the digits were numbered starting out 1.
  39. For example, #1 here is an odd-numbered digit.
  40. This is digit #2 which is an even-numbered digit, #3 is odd, etc.
  41. Now on the other hand, Python strings are index starting at 0.
  42. That's something you're going to have to watch out for.
  43. Or else you're going to go a little off by 1 error in your code that makes it a pain to debug.