Friday, November 14, 2014

Changes 11/13/14

Java:

  • Took eimacs test 17, which was on inheritance (abstract classes and interfaces)
  • Reviewed sequential and binary search algorithms

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.

Changes 11/11/14

UMD Contest:

  • Worked with Sam on the Vigenere Cipher problem (number 3)
  • He got it almost right away, but I'm still working on my implementation, because I'm having trouble debugging it.

Friday, November 7, 2014

Changes 11/7/14

UMD Programming Contest:

  • Had a conversation with Kevin about more efficient ways to fulfill the functionality requested by the Pythagorean triples problem from the contest.  
  • He suggested converting the array to a hash, using two for loops in order to identify a and b values, then checking if the corresponding c value was present in the hash.  
  • Since getting a specific element in a hash table is an O( 1 ) operation (although I haven't learned that yet), his solution would bring this down to O( n2 ). 
  • He also suggested pre-sorting the array and pre-squaring all the values, which would bring performance benefits for large values of n
  • Looked at UMD's solution to the Pythagorean triples problem
  • I guess I'm not quite clear on the design philosophy behind this contest.  Are we supposed to have the simplest solution, the most elegant, the fastest?  Can I pull in more advanced constructs like Hashes, or am I stuck with only the default imports of Java.  In their solution for this problem, they used Arrays.sort(), but did not use Math.pow().

Thursday, November 6, 2014

Changes 11/6/14

UMD Programming Contest:

  • Realized I had made an incorrect assumption about the data.  The first number of each set to check (such as in the table at the bottom of page 4 of the pdf) appears to be number of integers on that line, and not actually a number to be checked for triple-ness.  
  • This had also caused me to make the incorrect assumption that each number was to be counted only once, which clearly isn't true, because the line 5 13 12 3 4 5 is expected to output both {5 12 13} and {3 4 5}.  Since the first five in that set is the number of integers, the second five has to be counted twice.
  • After making this assumption, I then wasted valuable time making the program set values it had identified were Pythagorean triples to -1 and adding integer > 0 checks to all the for loops so that values of -1 would be ignored.
  • I also feel like the designers were very lazy in their setup of the environment, which leads to problems like this where they have hardcoded the expected length of the line into the file without being explicit about what they are doing.
  • To be fair, they did say:
  • "The first line in the test data file contains the number of test cases (< 100). After that, each line contains one test case: the first number is n (3 ≤ n ≤ 50), the size of the sequence, and the following n numbers are the sequence (not necessarily in any order)"
  • However, that's not worded very intuitively, and I either missed that completely or just didn't understand what they were saying.  One of the lessons I'm taking away from this problem in particular is to read the prompt and explanation very carefully.
  • I eventually got an implementation that worked, although it runs in O( n3 ) since there are three nested for loops.
  • Even in victory, I feel defeated, especially because I had to cheat a little and googled the best way to find the b value in a2 + b2 = c2, where a < b < c. 
  • I found a really simple solution to finding this intermediate value on stackoverflow, which was to add a+b+c and subtract the min and max values, leaving the only value that wasn't an extreme.  I wish I could have thought of this on my own, but I just don't have enough practice with this sort of thing yet.

Changes 11/5/14

UMD Programming Contest:

  • Wow today was horrible
  • The enviroment they have set up for the programming contest makes it almost impossible to debug your code.  They set System.out to output to a file, so if for whatever reason your program doesn't write the file, you have no way to find out why.  I ran into a problem where my for loops were causing endless execution, but the program gave no indication of this.  I checked in a normal java environment, and it turns out that the code:
  • int i = 0;
      for (int j = 0; j < 5; i++)
        System.out.print("");
    
  • does not give any compiler warnings, even though the compiler should be able to detect that this is extremely likely to be an unintended infinite loop. This is the first problem I ran into in my code, and it took me almost 45 minutes to discover the source since I wasn't able to print debug output!
  • I also just don't have a head for this sort of thing.  The problem I'm currently working on is problem number 2 from the 2012 contest, which tasks you with identifying Pythagorean triples from an array of integers.
  • In my current strategy, I've got for loops nested three deep, one to identify each of the three numbers in the formula a2 + b2 = c2. There is probably a better way of finding triples, but I have no idea what it would be.

Monday, November 3, 2014

Changes 11/3/14

UMD Programming Contest:

  • Wasted about 45 minutes trying to import the pythagorean triples project into IntelliJ.  I eventually got it by selecting "Import Project" > "Create Project From Existing Sources".  Previously I had been selecting "Import Project from External Model", which wouldn't be set up correctly.
  • Eventually I'll switch over to Eclipse
  • Working on the Pythagorean Triples problem
  • I'm sure there's plenty of resources on finding Pythagorean triples online, but I don't think I would be allowed to look at those during the contest, so I'm not going to look at those.