Sunday, April 10, 2016

IP v4 address matcher (regex)

IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.

image
The pattern is: ^(?:(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)$

Java:
public class IPv4Regex {
	static final String ipV4Pattern = "^(?:(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)\\.){3}"
			+ "(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)$";
	
	public static void main(String[] args) {
		System.out.println("000.123.234.245".matches(ipV4Pattern));
	}
}

Tuesday, March 8, 2016

Tree traversal notes

1. Inorder:

- travel left subtree in inorder
- visit root
- travel right subtree in inorder

Recursive approach:

void inOrder() {
 if(root != null) {
  inOrder(root.left);
  System.out.println(root.data);
  inOrder(root.right);
 }
}

Iterative approach using stacks:

void inOrder() {

    Stack stack = new Stack();

    Node current = root;

    while(current!=null || !stack.isEmpty()) {
        //push to stack and move to left sub-tree
        if(current!=null){
            stack.push(current);
            current = current.left;
        }
        else { //we need to pop out nodes from stack and shift to its right sub-tree
            current = stack.pop(); //visited node
            System.out.println(current.data);
            current = current.right;
        }
    }
}

2. Pre-order:

- visit root
- travel left subtree in preorder
- travel right subtree in preorder

Recursive approach:

void preOrder() {
    if(root != null) {
        System.out.println(root.data);
        preOrder(root.left);
        preOrder(root.right);
    }
}

Iterative approach using stacks:

void inOrder() {

    Stack stack = new Stack();

    Node current = root;

    while(current!=null || !stack.isEmpty()) {
        //print each value, then push its right subtree to stack and move to left subtree
        if(current!=null){
            System.out.println(current.data); //visited node
            stack.push(current.right); //push right subtree
            current = current.left; //shift to left subtree
        }
        else {
            current = stack.pop();
        }
    }
}

Friday, February 12, 2016

Designing your own iterable stuff

Or, simply, implementing iterator logic in your own class, on which you can iterate or even use for-each loop. Because, for-each works only on Iterator based collections.
Following is a generic Java 7 code that takes into account a simple custom linked list implementation that is generic and can be iterated over, using following steps.
1. The stuff we want to iterate upon has to be Iterable and expose
iterator()
2. Design a java.util.Iterator by overriding hasNext(), next() and remove().

Sunday, June 28, 2015

Sorting Key-Value pairs in Java

Saw making your own sorting logic by implementing Comparable or Comparator? If not, click here. Now, how about sorting key-value pairs! Well, we need just a little code modification. See following code sample that sorts key-value pairs, first by key, then values. Look at the compareTo method implementation.

Sunday, June 7, 2015

Comparable and Comparator- When and how to use? [Concise]

When there comes the need of sorting objects, be it using Collections.sort() or just adding elements to Treeset etc, java.lang.Comparable and java.util.Comparator come into picture. For default sorting of objects in the collection, we need to implement Comparable and override below method in that class, unless we are using some pre-defined class that already implements Comparable like String, Integer etc.,
int compareTo(T o)
If we want to provide an external sorting logic to override the default one, the class needs to implement Comparator interface and override the following method:
int compare(T o1, T o2)
The question: Why use Comparator when we already have Comparable?
Answer: If you have authored that class, then you can give a default sorting behavior by implementing Comparable and use that logic to sort whenever needed. Now, let's say that you are using an existing class whose source you can not modify and the default sorting logic of which you are not satisfied with. What now? Just write a Comparator using your own sorting process and use that instance to sort. See below sample programs.

The API information is here:
1. java.lang.Comparable
2. Java.util.Comparator
Lets dig into this with our custom class.