Thursday, March 13, 2014

Understanding Recursion

What is recursion - In computer science, it is essentially a method/function calling itself.

What do you think can go wrong? Like infinite loops (using for, while) you could have an endless program that will run as far as memory limits allows. However like we can prevent infinite loops we can have methods call themselves safely. Based on certain conditions we can return from the method. This condition is the base case (there maybe multiple base cases as well). Other rules must be present so that eventually all other cases reduce towards the base cases(s).

Consider the method:
What will be the output if a positive integer is passed in for n?

Suppose we want the sum of first 5 numbers. It can be broken down as follows:
sum(5) = sum(4) + 5
           = sum(3) + 4 + 5
           = sum(2) + 3 + 4 + 5
           = sum(1) + 2 + 3 + 4 + 5
           = 1 + 2 + 3 + 4 + 5

Recursion is a way of thinking - trying to find the solution to a problem by thinking about a smaller version of the same problem. The simplest problem is the base case.

A popular example is the Fibonacci series. The series goes 1, 1, 2, 3, 5, 8, 13...
The first and second terms are defined to be 1. Subsequent terms are the sum of the previous two terms. To define it more formally:

fib(0) = 1
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)

Writing some code:

Another example - notice the slight difference in the order of calls in the two options. What do you think is the output if both methods are called with the same number?

Factorial - factorial of n is the product of all numbers 1 to n. For example 5! = 5*4*3*2*1=120. More formally:

factorial(0) = 1
factorial(n) = factorial(n-1)*n

Factorial is undefined for negative numbers.

Saturday, January 11, 2014

Java Coding Conventions - Basics

Camel Case
All class names, method (subroutine) names and variable names must use came case. For example: MyClassName, myMethodName, myVariableName.

Note that:
  • Class names must begin with an upper case letter
  • Method and variable names must begin with a lower case letter
File name and Class name
The file name must be the same as your public class <Name>. For example and public class MyClassName

To make code easier to read and understand use a tab or 4 spaces for each “level” your code is indented.
Here is an example. Note how code is indented further in each time a new block of code starts. Also the end “}” aligns with where “{“ began.

Tip: In Eclipse you can use Source > Format to indent your code.

Monday, September 10, 2012

Running multiple Couchbase servers on one physical

So it turns out there is no simple way to run multiple Couchbase nodes on a single machine (purely to play around) unlike other DBs  - eg: mongo. I haven't been able to figure out how to change ports on Couchbase either. So using VirtualBox to run couple VMs. Loaded with Ubuntu. Hope this helps you out!

Step 1 - Create two Ubuntu VMs
I'm running under Win 7. Choose your OS and run VirtualBox. Start with creating a new machine, give it a name and pick Linux -> Ubuntu. Pick a small RAM size (default 512M). Follow the wizard to the end. Upon starting it will prompt you for the image - download your version of choice and use that (I am using the Desktop version). Follow the wizard.

In the Settings change the Network to "Host-only Adapter"

You should end up with a 192.168.* address and allows other VMs and the host to have access.

TIP - You can right-click and "clone" to create a second one quickly! - Just change the host name in /etc/hostname

Step 2 - Install Couchbase
Follow the instructions on Couchbase site to setup the first node. Follow the install on http://localhost:8091 to create a new cluster.

On the second node after installing, pick to join a cluster and you should be set!