12.22 Study Log

2.5 hours tonight. I finished the last problem from pset1 in cs50 on edx. This was more complicated than I had expected it to be! It was partially implementing the Lund algorithm. That is a quick checksum for seeing if credit card numbers are theoretically valid. It does some math on every other digit and then sums everything and looks for a zero in the end. The concept was something that I had run into before, but never in c. It had been humbling to see how far down you need to go in order to get functionality that I consider pretty basic. My initial thought was to see if I can access digits of a number like indexed chars in a string. That is not possible. It is also not very easy to convert a number into a string. It is also a bit harder to coerce that same number as a string to become a number again. I ended up not doing the num to string to num route, but I found a lot of bad ways to try it before I found even one that I could get to work.

In the end I looped through the number once for every character and subtracted the number from (the number / 10 and then * 10.) Since they are ints and just lose the digit when dividing this was able to get me the last digit. I then set the number to one tenth of itself and looped again.

After the (relatively simple) math was all done you just have to do a modulo operator on the sum in order to see if it ends in a zero. Then you need to do testing on the length and starting characters in order to see if it is a valid mastercard, visa, or amex. I used a lot of conditional logic in this section and I am unsure how it could be cleaned up more. I also stuck a bunch of return statements in there. I know that is controversial, but if I know it is a valid amex, then I should say that and stop thinking about it.

I’m surprised how difficult the second week homework is. I did pick the more difficult problem, but still. This is going to get interesting.

Tell me what you think.

This site uses Akismet to reduce spam. Learn how your comment data is processed.