
Title:
Luhns Algorithm Cont Solution  Software Testing

Description:

We've still got the code the I wrote for the Luhn checksum.

And this is kind of a redundant bad code but I wrote it quickly and I think it works.

So we have the Luhn checksum function.

And the first thing it does is looks at the length of it's argument.

That is to say, it looks at the length of the string over which it's going to compute a checksum.

It's also going to initialize the checksum variable to be 0.

Now, if we're computing a checksum over an even number of digits,

the remainder mod 2 is going to come out to 0.

So in this case, we're going to loop over the digits and the checksum.

That is the range(l) goes from 0 to length 1.

And here what I'm going to do is add 1 to that number

so that we can get a 1base numbering system like I showed you

in the explanation of Luhns algorithm.

So here now, if we're at digit 0 in Python we're at digit number 1

in the 1base human number system and so if that comes out to be 0 when moded with 2

now with an even number digit and so in that case we're going to add

the value of that digit into the sum; otherwise, we're going to call this Luhn digit function

on the same value and the Luhn digit function is just sort of the obvious thing.

It multiplies the number by 2. If it's greater than 9, it returns 9 less than the number.

Otherwise, it just returns the number.

Now, the other case is that we're checksumming an odd number of digits.

In that case, we have exactly the same logic with the even and odd cases flipped around.

So here we're taking the Python zerobased digit adding 1 to it to get a human

1based numbering system moding with 2.

If it comes out to be even, we're doing the Luhn digit computation.

If it's odd, then we're just adding in the value of the number.

And finally, we get a checksum that we've accumulated here.

And we take that mod 10 and that gives us our actual checksum value that we're going to use.

So now, here's the algorithm for generating a valid random credit card number.

So the inputs are remember a prefix and a total length in digits.

So the number of random digits we're going to generate is the total length

minus the length of the prefix minus 1 for the checksum digit.

Now we're just going to assert that that's greater than 0 just to be careful.

And now we're going to set N as our credit card number that's in progress.

So we're going to initialize that to be just the prefix.

Now for the number of random digits we have,

we're just going to make up a random digit and append it to the string.

Finally, we're going to add a zero checksum to it and compute the Luhn checksum.

If that Luhn checksum comes out to be zero, we're done.

If that Luhn checksum comes out to be nonzero, we have to do the little inversion

that I talked about where we take the checksum where we subtract the checksum from 10

and now what this little bit of logic does is it strips off the last character.

That's the zero we added from the credit card number and adds on the checksum number.

So that's how easy it is to generate a valid credit card number.

And now there is one valid function just to check if the Luhn checksum is 0.