Wednesday, November 12, 2014

Changes 11/12/14

UMD Programming Contest:

  • Uploaded Vigenere Cipher problem to github
  • It took me a while to figure out because I was having trouble with a certain line.
  • The correct implementation is:
  • sb.append((char)('A' + ((keyChar + plainChar) % 26)));
  • At first, I didn't quite understand what I was doing.  Sam and I had gone through the problem together, but I didn't fully understand his solution, so I decided to work on it on my own.  I was pretty good on everything else in the program, but couldn't quite remember what this line had done in his program.
  • Basically what this line does is take a character from a target string and offset it by the distance between a character in a "key" string and the letter A, which is the fundamental of a Vigenere Cipher.
  • I kept thinking that the keyChar should be subtracted from the plainChar, because I felt like they should be a range.  Obviously, that wasn't correct, and it threw me through a loop because I couldn't figure out what I was doing wrong
  • Eventually I decided to work through the example they gave in the problem description while also looking at my code, which is how I figured out what I was doing wrong
  • Doing this problem also made me realize I need to get more experience with the modulus operator (%).  
  • Sam used them really well in his implementation to loop the index for the character we were looking at in the key string and making sure that the offset for the character loops back around when it happens to offset past Z.

1 comment:

  1. It appears to me from this post that you already have a good feel for what I was trying to explain in my feedback from yesterday's post - that you will learn to get better and better at algorithmic thinking like that required for these problems only through doing it, and the more the better. Your process of studying the problem first, looking at a solution (Sam's in this case), and then taking the time to deeply understand why that solution works, is the right approach. I predict it will be fruitful.

    ReplyDelete