Print variables without escaped HTML in Play! framework

15 May

I just realized that when I was trying to embed the youtube videos on HTML I was getting the frame objects as

Check the quotes in src=””””, I wanted to remove that. After some googling I found how to do that in Play! Framework.
Here are the two ways :-

a.) use raw().
for example, the URL value to render is src=”${video.get(“url”)}”, the use src=${video.get(“url”).raw()}

b.) use #verbatim tag
for example, the URL value to render is src=”${video.get(“url”)}”, the use src=#{verbatim}${video.get(“url”)}#{/verbatim}

and you are good to go :). The result now would be

2010 in review

2 Jan

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meter™ reads This blog is doing awesome!.

Crunchy numbers

Featured image

A helper monkey made this abstract painting, inspired by your stats.

A Boeing 747-400 passenger jet can hold 416 passengers. This blog was viewed about 4,000 times in 2010. That’s about 10 full 747s.

 

In 2010, there were 32 new posts, growing the total archive of this blog to 50 posts. There were 3 pictures uploaded, taking up a total of 609kb.

The busiest day of the year was October 20th with 59 views. The most popular post that day was Java : Finding subarray from input array containing all integers in query array.

Where did they come from?

The top referring sites in 2010 were lmodules.com, linkedin.com, en.wordpress.com, google.co.in, and facebook.com.

Some visitors came searching, mostly for fibonacci series in java, java subarray, binary heap java, minimum number of coins, and implement memcpy in c.

Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

Java : Finding subarray from input array containing all integers in query array September 2010

2

Java : find union and intersection of two sorted arrays September 2010

3

Dynamic programming : Finding minimum number of coins for a given sum October 2009

4

Fibonacci Series in Java March 2009
8 comments

5

Implementing Binary Heap in Java September 2010

Java : Finding top-K elements in unsorted array in linear time

17 Oct

Complexity : O(N)

public class Selection {
private static final int TOP_K = 3;
public static void main(String args[]) {
int[] _array = new int[]{9,7,8,2,1,3,5,6,4};
if (_array.length == 0) {
System.out.println("Empty Array");
System.exit(0);
} else if (_array.length == 1) {
System.out.println("Only one element, Array already sorted");
} else {
select(_array, 0, _array.length-1, _array.length-TOP_K);
}
for (int i = _array.length-TOP_K; i <= _array.length - 1; i++) {
System.out.println(_array[i]);
}
}
public static int partition(int[] input, int p, int q) {
int x = input[p];
int i = p;
for (int j = (p + 1); j <= q; j++) {
if (input[j] <= x) {
i = i + 1;
if(i < j){
int temp = input[j];
input[j] = input[i];
input[i] = temp;
}
}
}
int temp1 = input[p];
input[p] = input[i];
input[i] = temp1;
return i;
public static void select(int[] list, int left, int right, int k){
int pivotIndex = partition(list, left, right);
if(pivotIndex == k){
return;
}
else if(k < pivotIndex){
select(list, left, pivotIndex -1, k);
}else{
select(list, pivotIndex +1 , right, k);
}
}
}

Output:
run:789
BUILD SUCCESSFUL (total time: 0 seconds)

Python : finding substring in a string

6 Oct
#!/usr/bin/python

import sys

def is_substring(actual_str, pattern_str):

    if actual_str is None or pattern_str is None:
        print "empty string .. quitting"
	return

    if len(pattern_str) > len(actual_str):
        print "substring pattern is longer than actual string"
	return

    indexes = []
    for i in range(len(actual_str) - len(pattern_str) + 1):
 	if pattern_str[0] == actual_str[i] and\
	    pattern_str[len(pattern_str)-1] == actual_str[i + len(pattern_str)-1]:
	    indexes.append(i)

    if len(indexes) == 0:
        print "Substring couldn't be found!"
	return

    mismatch = False
    for j in range(len(indexes)):
        index = indexes[j]
        for k in range(len(pattern_str)):
	    if(actual_str[index] != pattern_str[k]):
	        #print "Mismatch : %s - %s"%(actual_str[index], pattern_str[k])
		mismatch = True
		break
 	    index += 1

	if mismatch == True:
	    mismatch = False
	    continue
        print "substring found at indexes : ", indexes[j]

if __name__== "__main__":
    if len(sys.argv) != 3:
        is_substring('Hello world!', 'odl')
    else:
	is_substring(str(sys.argv[1]), str(sys.argv[2]))</div>

Output :

[harit@harit-laptop scripts]$ python substr.py ’123123113456456345123′ ’456′
substring found at indexes :  9
substring found at indexes :  12

[harit@harit-laptop scripts]$ python substr.py ‘Hello world!’ ‘orl’

substring found at indexes : 7

[harit@harit-laptop scripts]$ python substr.py 12 12

substring found at indexes : 0

[harit@harit-laptop scripts]$ python substr.py 12 123

substring pattern is longer than actual string

Java : Reversing a singly linkedlist in linear time

17 Sep

Program: Reverse a singlylinked List

complexity : O(n)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package careercup.google;

/**
 *
 * @author Harit
 */
public class SinglyLinkedListNode {
    private int value;
    private int next;

    public SinglyLinkedListNode(int v){
        this.value = v;
        this.next = -1;
    }

    public int getValue(){
        return this.value;
    }

    public int getNext(){
        return this.next;
    }

    public void setNext(int n){
        this.next = n;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package careercup.google;

/**
 *
 * @author Harit
 */
public class ReverseSinglyLinkedList {
    private static int[] input = new int[]{0,1,2,3,4,5,6,7};
    private static SinglyLinkedListNode[] nodes = new SinglyLinkedListNode[input.length];
    private static int head = -1;
    private static int tail = -1;

    public static void main(String args[]){
        createList();
        printList(head);
        printList(reverseList(head));

    }

    private static void createList(){
        for(int i=0; i<input.length; i++){
            SinglyLinkedListNode node = new SinglyLinkedListNode(input[i]);
            nodes[i] = node;
            if(i==0){
                head = i;
                tail = i;
                continue;
            }
            /* setting parent's next*/
            nodes[i-1].setNext(i);
            tail = i;
        }
    }

    private static int reverseList(int head){
        SinglyLinkedListNode next = null;
        SinglyLinkedListNode current = null;
        int result = -1;

        current = nodes[head];
        while(current != null){
            if(current.getNext() == -1){
                current.setNext(result);
                result = current.getValue();
                break;
            }
            next = nodes[current.getNext()];
            current.setNext(result);
            result = current.getValue();

            current = next;
        }
        return result;
    }

    private static void printList(int head){
        while(head != -1){
            System.out.print(nodes[head].getValue() + " ");
            head = nodes[head].getNext();
        }
        System.out.println("");

    }
}

Output:

run:
0 1 2 3 4 5 6 7 
7 6 5 4 3 2 1 0 
BUILD SUCCESSFUL (total time: 0 seconds)

Implementing Binary Heap in Java

16 Sep
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package careercup.google;

/**
 *
 * @author Harit
 */
public class BinaryHeapNode {

    private int value;
    private int left;
    private int right;

    public BinaryHeapNode(int v){
        this.value = v;
        this.left = -1;
        this.right = -1;
    }

    public int getValue(){
        return this.value;
    }

    public void setChild(boolean left, int index){
        if(left){
            this.left = index;
        }else{
            this.right = index;
        }
    }

    public int getChild(boolean left){
        if(left){
            return this.left;
        }else{
            return this.right;
        }
    }
    public boolean isLeftChild(){
        return (this.left == -1);
    }

    public boolean isRightChild(){
        return (this.right == -1);
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package careercup.google;

import java.util.ArrayList;

/**
 *
 * @author Harit
 */
public class BinaryHeap {

    //private static int[] input = new int[]{-1,3,2,1};
    private static int[] input = new int[]{-1,4,7,6,9,3,2,1};
    private static BinaryHeapNode[] nodes = new BinaryHeapNode[input.length];
    private static int MAX_NODES = 1;
    public static void main(String args[]){

        for(int i=1; i<input.length; i++){
            System.out.print(input[i] + " ");
            BinaryHeapNode node = new BinaryHeapNode(i);
            nodes[MAX_NODES++] = node;
            if(i==1){
                continue;
            }
            setParent(i);
            insertNode(i, input[i]);
        }
        System.out.println(" ");
        /* final binary heap */
        for(int i=1; i<input.length; i++){
            System.out.print(input[i] + " ");
        }
        System.out.println(" ");
    }

    private static void setParent(int index){
        while(true){
            BinaryHeapNode parent = nodes[index/2];
            if(parent.isLeftChild()){
                parent.setChild(true, index);
                return;
            }else if(parent.isRightChild()){
                parent.setChild(false, index);
                return;
            }
        }
    }

    private static void insertNode(int index, int value){
        ArrayList<Integer> indexes = new ArrayList<Integer>();
        indexes.add(index);
        int parent_index = index/2;
        while(parent_index >= 1){
            if(input[parent_index] > value){
                indexes.add(parent_index);
            }
            parent_index /= 2;
        }
        Integer[] replace = (Integer[])indexes.toArray(new Integer[0]);
        for(int i = 0; i <replace.length-1; i++){
            input[replace[i]] = input[replace[i+1]];
        }
        input[replace[replace.length-1]] = value;
    }
}

Output:

run:
4 7 6 9 3 2 1  
1 4 2 9 7 6 3  
BUILD SUCCESSFUL (total time: 0 seconds)

Selection sort in Java

16 Sep

In-place sorting

Complexity: O(n^2)

/** To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package careercup.google;
<div id="_mcePaste">/**
 *
 * @author Harit
 */
public class SelectionSort {
    //private static int[] input = new int[]{5,1,4,2,8};
    private static int[] input = new int[]{9,8,7,4,5,6,3,2,1};

    public static void main(String args[]){
        int last_index = 0;
        int swap_index = -1;

        printArray();

        for(int i=1; i<input.length; i++){
            swap_index = returnIndex(i);
            if(input[last_index] <= input[swap_index]){
                continue;
            }
            int temp = input[last_index];
            input[last_index] = input[swap_index];
            input[swap_index] = temp;
            last_index += 1;
        }
            printArray();
    }

    private static int returnIndex(int index){
        int rindex = index;
        int smallest = input[index];
        for(int i=index+1; i<input.length; i++){
            if(input[i] < smallest){
                rindex = i;
                smallest = input[i];
            }
        }
        return rindex;
    }

    private static void printArray(){
        for(int j=0; j<input.length; j++){
            System.out.print(input[j] + " ");
        }
        System.out.println("");
    }
}
Output:
run:
9 8 7 4 5 6 3 2 1
1 2 3 4 5 6 7 8 9
BUILD SUCCESSFUL (total time: 0 seconds)
Follow

Get every new post delivered to your Inbox.