J-hood's blog

Tuesday, April 17, 2012

What is a surrogate key? It is a key that is always implemented with the help of an identity column. Then what is an identity column? It is a column that is auto-generated by a SQL Server based on its seed value and its incremental value. Identity columns are always INT, which means a surrogate key is always INT as well. It cannot have any NULL values or repeating values. Surrogate key is a logical key, which means it does not exist on a SQL Server.

These days, I'm being trained to be a SQL developer so I'm going to post a lot of stuff about SQL for my sake of understanding and learning and for your sake of entertainment :) So! When you create tables, there are three methods to consider.

Let's look at the first one. The first method has full control over constraint names that are used to modify constraints later. No particular order of table creation is required, which means there is no code dependency.

The second method of creating tables is not as good as other methods. You will have no control over constraint names because they are auto-generated by a SQL Server. The order of creating tables are important, which means there is code dependency.

The last method is better than the second one but not as good as the first method since the first one is the best one. You have the full control over the constraint names but still there is code dependency.

Thursday, March 15, 2012

As I said on the previous post, I have been doing Project Euler. My goal for now is to finish first 50 problems. I have gotten first 10 so far. Then, I will go for next ten and then next ten and hopefully I can reach to the 50th problem. So I was browsing through other 40 problems and the Problem 15 was more intriguing to me than other problems. Maybe it was because it was more visual unlike other problems I had solved. So I dove right into it. Here is the problem:

Starting in the top left corner of a 2 x 2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20x20 grid?

Before talking about a 2x2 grid, let's see a 1 x 1 grid, where top left corner, 1, is the starting point and the bottom right corner, 4, is the goal point.

1-2
| |
3-4

If you can only take right and bottom paths and cannot backtrack as the problem describes, there are only two ways to get to 4 (1 - 2 - 4 and 1 - 3 - 4).

This isn't really a necessary step to find the solution but I redrew the 2 x 2 grid to a tree form, where you can only take down paths, so I could understand better.

1
/ \
2 3
/ \ / \
4 5 6
\ / \ /
7 8
\ /
9

Did the same for the 1 x 1 grid.

1
/ \
2 3
\ /
4

As you noticed, they form a diamond shape. Do the same thing for a 3 x 3 grid. Then you will find out that a 3 x 3 grid has 20 paths. So a 1 x 1 grid has 2 paths, a 2 x 2 grid has 6 paths and a 3 x 3 grid has 20 paths. This progression of 2, 6 and 20 were familiar to me and I found out that these numbers are related to Pascal's Triangle.

So beginning from the 2nd row the going down every other row, the middle number of each row represents the number of paths. Here is the formula to find a certain number in Pascal's Triangle:

r! / i!(r - i)! with rth row from the top and ith position from the left to right.

What we need to find in this problem is the number of paths for a 20x20 grid. Let's plug the numbers into the formula.

40! / 20!(40 - 20)! = 137846528820

The answer is 137,846,528,820 paths. A LOT of paths. Here is an interesting thing. If you place the diamond shape tree, as it is and fitting the top part of the triangle, the very bottom point of the diamond is the same as the number of the paths. For example, let's see the 2x2 grid again.

1
/ \
2 3
/ \ / \
4 5 6
\ / \ /
7 8
\ /
9

If you place this diamond on the Pascal's Triangle…

1
/ \
1 1
/ \ / \
1 2 1
\ / \ /
3 3
\ /
(6)

Very interesting, isn't it?! But how the number of paths and Pascal's Triangle are related? I did some research and I found out that it's because Pascal's Triangle determines the coefficients which arise in binomial expansions and this grid problem is also very related to binomial expansions.

Let's analyze problem again. Assuming there is an n x n grid,

1. all the paths have size 2 x n;
2. you can only go R(right) or B(bottom) from each point. For a 2x2 grid, the paths in R and B are RRDD, RDRD, RDDR, DRRD, DRDR and DDRR;
3. the strings for a grid has the same number of R and D.

So if we rephrase the problem observing the three above points:

How many different strings of size 2xn, consisting of n Rs and n Ds, are there?

This is also solved using binomial expansions: 2n! / (n! x n!) with the size n. And this is essentially the same with the formula above: r! / i!(r - i)! with rth row from the top and ith position from the left to right.

To conclude this post, the solving Problem 15 was not hard because luckily I knew the few first number progression of Pascal Triangle. But what I importantly learned from solving this problem was how the Pascal Triangle and binomial coefficient and I look forward to learning experience like this solving next Project Euler problems.

Source:
[1] Wikipedia articles on Pascal's triangle and Binomial coefficient.
[2] The guy who already solved this problem. His way is much simpler and helped me understand the connecting factors between Pascal's triangle and Binomial coefficient (http://joaoff.com/2008/01/20/a-square-grid-path-problem/)

Wednesday, February 29, 2012

I found this website called Project Euler. They have a bunch of math problems that can be only solved with computer programming (maybe it's because some of the problems are too hard or the numbers are very large).

Here is the link for the website: http://projecteuler.net

It's kinda fun because it has this feature that's similar to PS3 trophies and xBox achievements. For example, if you solve the first three problems, you earn this achievement called 'Baby Steps'. Other fun example is 'Fibonacci Fever' that you can earn if you solve the first twelve Fibonacci numbered problems. And here I have solved five first problems. They are not necessarily hard but I wanted to keep up with my math and Lisp skill. It sucks the pre class brush doesn't support Lisp so I'm uploading it as C. Bare with me with that. Even though I didn't put any comment, it's still very easy to understand. I used both recursive and iterative.

Wednesday, February 22, 2012

Gosh, I finally finished the assignment 2. I blame myself for being lazy and I need to stop that. So this assignment is the addition to the assignment 1. You have to add more features to it. I am gonna talk about the more of the main tasks of this assignment.

1) The program needs to be able to accept the variables as operands. As far as the controller and view, you need to have three variable buttons, in my case a,b and x, and three test buttons that set up different values for the variables. Then the model is where the actual implementation happens. I set up a public NSDictionary variable which contains variables and their corresponding values depends on the different test button. So as the program pops the stack it checks if the top of the stack is equal to a, b, or x. Then it accordingly changes the variable to the number.

2) Re-implement the descriptionOfProgram: method from the last lecture to display the passed program in a more user-friendly manner. It is somewhat similar to the given method, + (double)popOperandOffProgramStack:(NSMutableArray *)stack. Check if the top of the stack is a NSNumber or NSString. If it is a NSString, check if the operation is two, one, or 0 operands operation. If it is a two operands operation, use "operand operation operand" format. If it is a one operand operation, use "operation(operand)" format. If it is a non-operand operation, just present the operation itself.

3) Add an Undo button to your calculator. There are two cases think about, when the user is in the middle of typing and not in the middle of typing. The undo button when the user is in the middle of typing is already implemented as the backspace button in the previous assignment. When the user it not typing, you need to pop the stack in the right manner so that the view displays the correct previous value. My algorithm for this might not be the fastest or the best but it works. So you have to pop the stack at least once no matter what. So I use do-while loop. So pop the stack first and while the top of the stack is a NSNumber or equal to the variables, you keep popping so that it reaches to the previous operation. Then you check the top of the stack (without popping) and update the user interface.

That's pretty much it. There are other little stuff you have to implement as well but they are not that hard so I won't talk about it. Here are the code for my assignment 2. Good luck!

Monday, January 30, 2012

It wasn't my first time being exposed to objective-c and Xcode and other Apple stuff because I have this book called "The Complete Idiot's Guide to iPad & iPhone App Development" by Troy Brent. Don't get me wrong, it is a good book. However, the book was too basic and more importantly it covers its materials based on Xcode 3. I mean Xcode 3 and 4 are, of course, pretty much the same thing but I figured getting used to the latest version is always good and wanted to learn all the cool features of the latest version.

So since I have a lot of time (sadly still job hunting *sad face*), I decided to really get into app development. I heard a lot of good stuff about iTunesU app development courses and I picked the one with highest ratings, Stanford CS193p Fall 2011. I just finished the assignment 1 and I would like to talk about it. This wasn't that hard at all. But I was getting used to and still need to know better about objective-c itself and MVC. For example, you usually *call* methods in Java but you send a message TO methods in objective-c. It was somewhat weird to get used to but it wasn't that bad when I think in the opposite way of Java. MVC, I almost understand but still...I need to completely be able to comprehend it and explain it.

So the first assignment was to build a calculator as an iPhone app. There were seven required tasks to do.

1. Follow the walk-through instructions (separate document) to build and run the calculator in the iPhone Simulator. Do not proceed to the next steps unless your calculator functions as expected and builds without warnings or errors.

First, you just need to follow the walkthrough document that is provided by the instructor. For my first time finishing the walkthrough, my app was giving me an error and since I thought it was gonna take actually longer to find a bug than redo the whole thing, I decided to do it again lol. Then I successfully finished the basic calculator.

2. Your calculator already works with floating point numbers (e.g. if you touch the buttons 3 Enter 4 / it will properly show the resulting value of 0.75), however, there is no way for the user to enter a floating point number. Remedy this. Allow only legal floating point numbers to be entered (e.g. “192.168.0.1” is not a legal floating point number). Don’t worry too much about precision in this assignment.

There is a hint section that talks about this problem but I didn't do it that way. I thought it was easier to do set up a boolean flag that tells if there is a dot or not on the current display. So if there is no dot, you can place a dot or do nothing.

3. Add the following 4 operation buttons: • sin : calculates the sine of the top operand on the stack. • cos : calculates the cosine of the top operand on the stack. • sqrt : calculates the square root of the top operand on the stack. • π: calculates (well, conjures up) the value of π. Examples: 3 π * should put three times the value of π into the display on your calculator, so should 3 Enter π *, so should π 3 *. Perhaps unexpectedly, π Enter 3 * + would result in 4 times π being shown. You should understand why this is the case. NOTE: This required task is to add π as an operation (an operation which takes no arguments off of the operand stack), not a new way of entering an operand into the display.

This isn't hard either. You make a new operation button sin, cos, sqrt, and pi. Then you write the code for each operation accordingly. In order to understand the last bullet point, just think about how a stack works (First In, First Out) and look at the code and see how it is being pushed and popped. You can always draw! The best way to understand things.

4. Add a new text label (UILabel) to your user-interface which shows everything that has been sent to the brain (separated by spaces). For example, if the user has entered 6.3 Enter 5 + 2 *, this new text label would show 6.3 5 + 2 *. A good place to put this label is to make it a thin strip above the display text label. Don’t forget to have the C button clear this too. All of the code for this task should be in your Controller (no changes to your Model are required for this one). You do not have to display an unlimited number of operations and operands, just a reasonable amount.

For this, I added a new display called brainDisplay that pretty much does the same thing as display. You just need to make it to display operations to the label. I will talk about C next.

5. Add a “C” button that clears everything (for example, the display in your View, the operand stack in your Model, any state you maintain in your Controller, etc.). Make sure 3 7 C 5 results in 5 showing in the display. You will have to add API to your Model to support this feature.

Just initialize everything. Set display to 0 and brainDisplay to blank. Also, I made a function on the model called emptyStack which empties the operand stack. I used that in order to empty the it. One more thing, don't forget to set the flags (in my case, userIsInTheMiddleOfEnteringANumber and thereIsADot) to NO.

6. If the user performs an operation for which he or she has not entered enough operands, use zero as the missing operand(s) (the code from the walkthrough does this already, so there is nothing to do for this task, it is just a clarification of what is required). Protect against invalid operands though (e.g. divide by zero).

As the problem says, it is already done. About protecting against dividing by zero, I didn't do anything because the code itself doesn't do anything if that happens. I could make it show an error message but I just didn't do it.

7. Avoiding the problems listed in the Evaluation section below is part of the required tasks of every assignment. This list grows as the quarter progresses, so be sure to check it again with each assignment.

OK!

So that's that. Here are my source code for this assignment. I did all the extra credit too! If you've gotten yours done this far, the extra credit problems would not be hard at all. By the way, I really don't think this is perfect at all but it works well so far (might not work for something I haven't tested). If you see any suggestion or error, please let me know. I'm not really a great programmer!! (...yet)

Thursday, January 26, 2012

As explained on the previous linked list post, doubly linked lists have two links for each node, which are the next link and previous link. So when it comes to inserting a new node in the middle of the list, you have to be careful with the order of assigning the links. Otherwise, you could easily lose the link so the whole list would be messed up. I'll explain how to but see the figure first. You can assume that there are multiple nodes before the node 1 and after the node 2.

First of all, you gotta link the new node to its previous and next nodes AND THEN you link the new node's previous and next to the new node. In Java code, it would be like this.