Sunday, March 24, 2013
Blog Post for 4/7
Recursion is the method where the solution of a problem depends on the solutions to smaller instances of the problem. Although I don't relate coding to food much, I recently watched an episode of Masterchef where the contestants were required to produce a lemon meringue pie from scratch, and it strangely reminded me of the recursive method. In order to get the whole pie to work, the contestants needed to make sure that they had all three parts of the pie nailed, including the crust, the lemon curd, and the meringue on top. The two contestants who were required to make this pie did not impress the judges with their final product because their pies always had one or two components that were subpar. The first contestant's pie had a soggy crust because she didn't give it enough time in the oven, and while her lemon curd was very good because she had been "tasting it constantly" as she made it, her meringue was sparse and raw because she did not torch the egg whites enough before serving it. The overall presentation of her pie was criticized as well by the judges because it lacked the aesthetics. The second contestant's pie had a good crust, and one of the judges commented that the pie "held together well and had good structure." His pie also had a good lemon curd because he had been tasting his as well throughout the process. Unfortunately, his meringue turned out to be airy because the egg whites had separated throughout the cooking process, and therefore his meringue was not as crisp and creamy as it could have been. In the end, the second contestant won because 2/3 of his pie was well-executed, while the first contestant only had 1/3 of her pie that was well-executed. I thought of the recursive method because each part of the solution depends on smaller solutions of the problem, and each solution has a specific recipe to its success. The whole method will only work if each solution turns out to be well-executed like the three stages of a lemon meringue pie, and each stage has specific steps to acquiring the perfect taste and texture. Baking is as precise as using recursion in a code, and the idea of building up layers in a recursive statement can be compared directly to building up the layers of a pastry. The overall effect of recursion relies on the parts that create it just as the success of a pie can rely on the whipping of egg whites, the proper baking time in the oven, and the visual aspects of the pie.
Tuesday, March 19, 2013
Blog Post for 3/17 - Sorting in General
There are obviously many ways to sort things in Java, yet having said that, I don't really see how sorting applies to practical real-life scenarios. I do see a value in learning the basic code to improve our understanding of how logic/code works in the language, so don't get me wrong. There's nothing wrong with what we're doing by starting with the basics and working up from there. However, whenever I try to imagine using sorting in a real-life application, it would seem that game-creators or animators would use pre-written codes to write their programs and not bother with re-writing sort methods at all. After all, what's the point of writing a snippet of code that sorts integers when what you're ultimately trying to create a complex gaming system? I'm not bashing the idea of learning these methods because I'm sure there are more complicated ways of writing them, but I'm wondering how much they actually help in a normal job/career.
Now, when I think about the value of sorting things in more tangible way, sorting makes a little bit more sense. Sorting a card deck, for instance, could be a very useful invention, but it would definitely be difficult to create a device that could perform that task. If one were to sort an augmenting card deck (instead of shuffling cards, they would be doing the complete opposite), there would be both faster and slower methods of sorting the cards, similar to sorting in Java. I can certainly understand how sorting might apply to material objects.
Having said that, I'm probably too naive to understand the magnitude of how important sorting is, but it's something to think about. The problem I have with connecting coding with the real world is that it's too abstract, strict, and logical for me. For example, when I first saw the Bubble Sort method, I had to stare at it and break down every single for loop and > or < sign, and it actually took me a couple of minutes to actually understand it. Sometimes I wonder how other people can understand a huge chunk of code in 5 seconds. Anyway, I like to learn about different things when I am aware of the benefits/potential that they can bring to the real world. Just my two cents!
During Spring break, I caught up on reading, which is something I haven't done in a while. Usually, the only things I'm allowed to read during the school year are worksheets and assigned readings from English. I'll admit that I broke into The Woman Warrior (which is required for English), but I read it just to see how it was. I wanted to read Life of Pi before seeing the movie, but I didn't have any time. Speaking of movies, I saw Wreck-It-Ralph, which turned out to be a lot better than I thought it was going to be. It's one of the movies that is actually better than the trailer.
Luckily, I didn't have to worry about college notifications during Spring break (unlike Winter break). It definitely feels weird getting into my top choice (Vandy) while everyone else is waiting to hear back. Interestingly enough, I was accepted to SMU and Case Western over the break, so that was fun. My brother was waitlisted at 7 schools before he was accepted to Carnegie Mellon (which is #1 in CS, coincidentally!), so I can imagine how he felt about that. Regardless, I'm happy that my second semester senior year played out like this and am grateful for my teachers, friends, and family's support.
Now, when I think about the value of sorting things in more tangible way, sorting makes a little bit more sense. Sorting a card deck, for instance, could be a very useful invention, but it would definitely be difficult to create a device that could perform that task. If one were to sort an augmenting card deck (instead of shuffling cards, they would be doing the complete opposite), there would be both faster and slower methods of sorting the cards, similar to sorting in Java. I can certainly understand how sorting might apply to material objects.
Having said that, I'm probably too naive to understand the magnitude of how important sorting is, but it's something to think about. The problem I have with connecting coding with the real world is that it's too abstract, strict, and logical for me. For example, when I first saw the Bubble Sort method, I had to stare at it and break down every single for loop and > or < sign, and it actually took me a couple of minutes to actually understand it. Sometimes I wonder how other people can understand a huge chunk of code in 5 seconds. Anyway, I like to learn about different things when I am aware of the benefits/potential that they can bring to the real world. Just my two cents!
During Spring break, I caught up on reading, which is something I haven't done in a while. Usually, the only things I'm allowed to read during the school year are worksheets and assigned readings from English. I'll admit that I broke into The Woman Warrior (which is required for English), but I read it just to see how it was. I wanted to read Life of Pi before seeing the movie, but I didn't have any time. Speaking of movies, I saw Wreck-It-Ralph, which turned out to be a lot better than I thought it was going to be. It's one of the movies that is actually better than the trailer.
Luckily, I didn't have to worry about college notifications during Spring break (unlike Winter break). It definitely feels weird getting into my top choice (Vandy) while everyone else is waiting to hear back. Interestingly enough, I was accepted to SMU and Case Western over the break, so that was fun. My brother was waitlisted at 7 schools before he was accepted to Carnegie Mellon (which is #1 in CS, coincidentally!), so I can imagine how he felt about that. Regardless, I'm happy that my second semester senior year played out like this and am grateful for my teachers, friends, and family's support.
Monday, March 4, 2013
Sort Lab/compareTo
One mistake I made on my sort lab that took me forever to figure out was the < vs. > sign when comparing two different arrays. I think I had < before in the if statement below, but the compareTo method compares the first to the second.
If i1 = 5 and i2 = 6, the < sign would switch them, which is what we don't want to do in this case. We want an augmenting list, so we should only swap them if the first is bigger than the second. Therefore, we want the > sign in the if statement instead (just a technical error on my part).
The actual code went something like this. Also, the code was really, really slow, probably because String temp = myArray[j-1]; kept creating a new temp during each pass and that just made the process a whole lot longer. There were probably other things too that made it incredibly slow, but I optimized it by just creating String temp and then temp would just be reassigned in the case that the if statement was true.
In the end, it was actually interesting to see how much slower my code was compared to the Collections sort in java, but it was also kinda sad and funny at the same time. :)
private static void bubblesort(String[] myArray)
{
String temp;
//before: not here^
for(int i=0; i<myArray.length; i++)
{
for(int j=1; j<myArray.length; j++)
{
if(myArray[j-1].compareTo(myArray[j])>0)
{
temp = myArray[j-1];
//before: String temp = myArray[j-1];
myArray[j-1] = myArray[j];
myArray[j] = temp;
}
}
}
}
{
String temp;
//before: not here^
for(int i=0; i<myArray.length; i++)
{
for(int j=1; j<myArray.length; j++)
{
if(myArray[j-1].compareTo(myArray[j])>0)
{
temp = myArray[j-1];
//before: String temp = myArray[j-1];
myArray[j-1] = myArray[j];
myArray[j] = temp;
}
}
}
}
Subscribe to:
Posts (Atom)