Technical Bhaigiri

Learning Spring webMVC using Springframework tutorial

January 17, 2009 · 1 Comment

So unless you like Netbeans, it might be a series of manual steps to create a Spring WebMVC application setup. A good idea is to use the SpringFramework tutorial as given on http://static.springframework.org/docs/Spring-MVC-step-by-step/index.htmlxdev-spring_logo

There is a small error in the tutorial, so to save you time if you use this, watchout for the extra “<beans>” tag.  So your springapp-servlet.xml in Chapter 4 should look like

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans
       xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
       xsi:schemaLocation=”http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd“>

<beans>

<bean id=”……….

Ref: http://static.springframework.org/docs/Spring-MVC-step-by-step/part4.html

→ 1 CommentCategories: Programming Practice · Software
Tagged: , , , ,

Rownum and the Top-N query

November 2, 2008 · 1 Comment

Rownum is a special column that can be used to limit the number of results returned by a query. However, not understanding exactly how it works can lead to unexpected results. Let’s go into some simple scenarios involving the rownum clause.

Here’s our table definition:
create table employee (empname varchar(20), empid number(10));

Let’s say we want to retrieve 5 rows from this table. This is what the query might look like.
select * from employee where rownum <= 5;

Now if we want the top 5 empids from this table, here’s a novice attempt.  This query will not return the top 5 empids as desired, but just some 5 rows sorted by empids.
select * from employee where rownum < 5 order by empid desc;

Here’s what we need to do. What follows is called a Top-N query. This query contains a nested query that retrieves the rows from this table sorted by the empids, and then applies a rownum clause on the results of this query to return the desired result.
select * from (select * from employee order by empid desc) where rownum <=5;

Essentially a Top-N query is a query used to retrieve the top N rows from a table, based on desired conditions. In our example, our query was attempting to retrieve the top 5 empids from the employee table.

(Please note that all queries listed here were run and tested on Oracle.)

→ 1 CommentCategories: Oracle · SQL
Tagged: , , , ,

Programming Practice: Prime Numbers

November 2, 2008 · Leave a Comment

Problem: Compute all prime numbers less than specified integer

Example:
Input: 100
Output: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Algorithm:
We use an algorithm known as the Sieve of Eratosthenes to solve this problem. How it works is that for each prime number starting from 2, we mark all multiples of that number as non prime. At the end of this process, all the prime numbers are identified.

Here’s the detailed steps.
1. Create an array of n items, and mark all values as 1 to mark them as prime.
2. Iterate over the array going from i = 2 to sqrt(n). If i is prime, mark all multiples of i as 0 (non prime)

Java Code:

→ Leave a CommentCategories: Java · Programming Practice
Tagged: , , , , , ,

Programming Practice: Remove specified characters from input string

November 2, 2008 · Leave a Comment

Problem: Given two input strings, remove all occurrences of all the letters in the second string from the first one

Examples:

Input: “Battle of The Vowels”, “aeiou”, Output: “Bttl f Th Vwls” (removing all vowels from the input string)

Algorithm:

We could iterate over the remove string and for each character, remove all occurrences from the input string. Give input string of length ‘n’ and remove string of length ‘m’, this brute force algorithm would lead to a complexity of O(mn).

Here’s a more efficient algorithm. Iterate over the remove string, and store each character in an array. Later as we iterate over the input string, we lookup this array, and if that character is present in this array, then we remove it from the input. This algorithm works if the array lookup is a constant time operation. The net complexity of this algorithm would be O(m+n), much better than the O(mn) brute force algorithm described earlier.

The detailed steps are as folllows:

1. Create an array ‘flags‘ of 256 characters. Entries in this array serve as flags denoting whether character ‘i’ is to be removed or not. Initialize all elements of this array to false.
2. Iterate over the remove string. For each character i in the string, mark flags[i] as true
3. Iterate over the input string. For each character in the string, if flags[i] is true, then copy it to an output array. Otherwise continue to the next character.
4. At the end of this iteration, the output array will contain all characters of input minus all letters of remove.

Java Code:

→ Leave a CommentCategories: Java · Programming Practice
Tagged: , , , ,

Programming Practice: Nth Element in Inorder Tree Traversal

October 24, 2008 · Leave a Comment

Problem: To find the nth position element in an Inorder traversal of a Binary tree. Each node in the Binary tree contains the size of the tree underneath.

Examples:

For a tree with root:node1, left child: node2, right child: node3

Input: 1, Output: node2
Input: 3, Output: node3

Algorithm:

1.Traverse the tree to find the node where required inorder position is equal to weight of a node
2.Weight of each node is determined by keeping track of the size of the tree traversed.
3.If node’s weight is less than inorder position then go left else go right and carry the weight of the  current node

Java Code:

→ Leave a CommentCategories: Java · Programming Practice · Software · Technology
Tagged: , , , , , , ,

Programming Practice: Palindrome Test

October 24, 2008 · 1 Comment

Problem: Detect whether a given string is a palindrome or not. A palindrome is a word that spells the same if it is reversed.

Examples:
Input: “madam”, Output: true
Input: “test”, Output: false
Input: “a”, Output: true
Input: “aabbaa”, Output: true

Algorithm:
1. Initialize two counters, one pointing to the first character, and the second to the last one
2. Iterate over the string. Compare the characters at the two locations. If the characters match then advance the front counter by 1 and the back counter by -1. If the characters don’t match, the string is not a palindrome.
3. If the two counters reach the same location, and everything has matched so far, the string is a palindrome

Java Code:

→ 1 CommentCategories: Java · Programming Practice · Technology
Tagged: , , , , ,

Programming Practice: Reverse a Sentence

October 23, 2008 · Leave a Comment

Problem: Given a string of words, reverse the string such that the last word becomes the first one. The word itself should not be changed

Examples:

Input: “Hello world”, Output: ” World Hello”
Input: “This blog rocks”, Output: ” rocks blog this”

Algorithm:

1. Initialize two counters, front and back, to the end of the input string
2. While we havent reached the start of the string, move down front we encounter a space. Copy the part of the string from this point until back into the output.
3. Move back down by one character to account for the space. Set front to back, and repeat till front is more than 0.

Java Code:

→ Leave a CommentCategories: Java · Programming Practice · Technology
Tagged: , , , , ,

Programming Practice: String To Integer

October 23, 2008 · Leave a Comment

We earlier saw how to convert an integer number to its string representation. Lets look at the reverse problem.

Problem: Given a number as a string, convert it to the corresponding integer representation

Examples:

Input: “123″, Output: 123
Input” “0″, Output: 0
Input: “-1″, Output: -1
Input: “+100″, Output: 100

Algorithm:
1. The first character could potentially be a ‘+’ or ‘-’. Check for this and record this information.
2. Iterate over the string characters. For each character, convert it to its integer form by subtracting ‘0′ from it.
3. Initialize output to 0. For each digit we read, multiply output by 10 and add this digit to it. This will make sure that starting from the most significant digit, each digit reaches its corresponding place in the final number.
4. In the final output, account for the sign on the number.

Java Code:

→ Leave a CommentCategories: Java · Programming Practice · Technology
Tagged: , , ,

Programming Practice: Integer To String

October 23, 2008 · 1 Comment

This is a new section we are going to start. We will be talking about how to solve some typical and commonly asked (in interviews) programming questions. To start off, this one is a relatively simple one.

Problem: Given an integer value, convert it to its string representation.

Examples:
Input: 127, Output: “127″
Input: -123, Output: “-123″

Algorithm:
1. Determine if the number is negative. If yes, multiply by -1 to make it positive. Lets call this ‘n’.
2. Compute n%10 (remainder of the division). This is the value of the  current digit, starting off with the least significant one. Store this digit in a temp array location.
3. Divide n by 10. Assign this to n.
4. Repeat steps 2 and 3 till n becomes 0.
5. At this point the output could potentially contain the reversed string representation. We can go through this output to create the output in the correct order.

Java Code:

There are other ways of solving this problem which would not require reversing the output in the end. This is just one possible solution.

→ 1 CommentCategories: Programming Practice · Technology
Tagged: , , ,

Execute C Programs on Unix from Windows

July 15, 2008 · 1 Comment

Appended below is the C program to execute a shell script using rsh on a remote Unix/Linux machine. This C program can run on Windows/Unix/Linux.

#include <stdlib.h>
int main(int argc, char *argv[])
{
system(“rsh hostname -l username script/path/on/unix/test.sh”);
return 0;
}

→ 1 CommentCategories: Software · Technology
Tagged: , , , , , ,