Thursday, September 9, 2010

What is framework and how it differs from library?

A library is essentially a set of functions that we can use in our projects, libraries are bundled into jar (java) usually organized into classes. Each call does some work and returns control to the client.

A framework is same as libraries in the structure but differs in the design and functionality with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by sub-classing or by plugging in your own classes. The framework's code then calls your code at these points.

In short frame is the best possible use of basic libraries, as the frameworks are usually built on the top of libraries.

Inversion of Control is a key part of what makes a framework different to a library.

Wednesday, August 4, 2010

SQL queries

select employee_id,last_name, salary,department_id
from employees
where manager_id = ∣
--=========================================================
select last_name
from employees
where last_name like '__a%';
--=========================================================
select last_name
from employees
where last_name like ('%a%e%') or last_name like ('%e%a%');
--=========================================================
--is not null
select last_name, salary, commission_pct
from employees
where commission_pct is not null;
--=========================================================
--between two specified dates
select last_name, hire_date
from employees
where hire_date between to_date('1994/01/01',
'yyyy/mm/dd')
AND to_date ('1994/12/31', 'yyyy/mm/dd');

select * from employees
where department_id not in (select department_id
from departments);
--=========================================================
select last_name, department_id,salary
from employees
where department_id IN (20,50) and salary between 5000
and 12000
order by last_name;
--=========================================================
select last_name, job_id

from employees
where last_name IN ('Taylor','Matos');
--=========================================================

Thursday, April 8, 2010

Redireciton of input and output on linux shell..

Syntax:
linux-command redirection-symbol input/output-file-name

There are three basic redirection operators.
  1. output (>)
    ex: $ ls > dirList.txt
    the output of the ls command will write in dirList.txt if the file is already present then it will be overwritten without any warning.

  2. append (>>)
    To output Linux-commands result to the end of file (append). Note that if file exist , it will be opened and new information will be written to end of file, without losing previous information, And if file is not exist, then new file is created. For e.g. To send output of pstree command to already exist file give command
    ex: $ pstree >> processList.txt

  3. input (<) To take input to Linux-command from file instead of key-board. If file does not exist then you have to give using key-board. ex: ./a.out < input.in
In Linux in C/CPP programming Language keyboard, screen etc are all treated as files. these files are:


































File Name

Discriptor

Use

Example

Stdin

0

Standard Input

Keyboard

Stdout

1

Standard Output

Screen

Stderr

2

Standard Error

Screen
In linux every program has three files associated with it, (when we start our program these three files are automatically opened by the shell). The use of first two files (stdin and stdout) , are already seen by us. The last file stderr is used by our program to print error on screen. Error message can't be redirected, for example the Command $ rm file1.txt > getError.txt will not log the error(rm: cannot remove `file1.txt': No such file or directory) into the file getError.txt in case file1.txt does not present, since output is send to error device. But if still want to log the error, use the discriptor for example $ rm file1.txt 2>getError.txt


Friday, April 2, 2010

The logic behind Ethiopian multiplication.

This method of multiplication is also called Egyptian Multiplication (as believed it was developed in Egypt).

How it Works.
  • write two no in the adjacent columns(smaller one in left side,

  • In the left column recursively halve the number, discarding remainders,

  • In the right column recursively double the number and write the result below, do this unlit left column shows 1,

  • Test for the left column if it is odd then add corresponding no in the right column.

Example

A = 34;
B = 12;






















BA
1234
668
3136
1272

_________________________________________
408
_________________________________________

12*34 = 136+272
= 34*4 + 34*8
= 34(4+8)
= 34(22+23)
= 34(12)

The main idea is that break the first no in the power of two and multiplication with 2 is easier. Then add to obtain result.

Useful links for me...

Tuesday, March 30, 2010

Bag Datastructure

An unordered collection of values that may have duplicates.

A bag has a single query function, numberIn(v), which tells how many copies of an element are in the bag, and two modifier functions, add(v) and remove(v).

numberIn(v) = returns no of element in Bag of type v;
add(v) = add a new element v in the bag;
remove(v) = removes all the element from the Bag of type v;
showBag() = prints all the element of the Bag;

Implementation of Bag in cpp(template).

#include < iostream >
#include < string.h >

using namespace std;

template < class Data >

class Bag
{
private:
struct node
{
Data data;
node *link;
}*ptr;
public:
Bag()
{
ptr = NULL;
}

void add(Data a)
{
if(ptr==NULL)
{
ptr = new node;
ptr- > data = a;
ptr- > link = NULL;
}
else
{
node *temp;
temp = new node;
temp- > data = a;
temp- > link = ptr;
ptr = temp;
}
}

void remove(Data data){
int present = 0;
if(ptr==NULL){
cout < < "Error !! Bag is empty" < < endl;
return;
}
else{
node *temp;
node *preTemp;
temp = ptr;
while(temp != NULL){
if(temp- > data == data){
if(temp == ptr){
ptr = ptr- > link;
delete temp;
temp = ptr;
}
else if(temp- > link!=NULL){
preTemp- > link = temp- > link;
delete temp;
temp = preTemp;
}
else if(temp- > link==NULL){
preTemp- > link = NULL;
delete temp;
}
}
else{
preTemp = temp;
temp = temp- > link;
}
}
}
}

int numberIn(Data data){
int count = 0;
if(ptr==NULL)
{
return count;
}
else
{
node *temp;
temp = ptr;
while(temp != NULL){
if(temp- > data == data){
count++;
}
temp = temp- > link;
}
}
return count;
}
void showBag(){
node *temp;
temp = ptr;
while(temp!=NULL){
cout < < " [" < < temp- > data < < "] " < < endl;
temp = temp- > link;
}
}
};



int main()
{
int input,remove;
Bag < int > intBag;

for(int i=0;i < 20;i++)
{
cin > > input;
intBag.add(input);
}
intBag.showBag();
cin > > remove;
cout < < "NumberIN = " < < intBag.numberIn(1) < < endl;
intBag.remove(remove);
cout < < "removed" < < endl;
intBag.showBag();
}

Adaptive Sorting.

A sorting algorithm is called adaptive sort if it takes advantage of previously shorted inputs.The problem with the common sorting algorithms is that they usually don't adapt to the data. Neither quicksort nor merge sort gives optimal results in partially ordered data (quicksort can even give Θ(n2) performance), and adding a preprocessing pass only gets you so far.Adaptive sorting is usually performed by modifying existing sorting algorithms.

This is an attractive algorithm because nearly sorted sequences are common in practice. Thus, the performance of existing sort algorithms can be improved by taking into account the existing order in the input.

A classic example of an adaptive sorting algorithm is Straight Insertion Sort. In this sorting algorithm, we scan the input from left to right, repeatedly finding the position of the current item, and insert it into an array of previously sorted items.

void straightInsertionSort(int *arr, int n){
*(arr+o) = -someLargeValue;
for(int j = 2; j < n; j++){
i = j-1;
int temp = *(arr+j);
while(temp < *(arr+i)){
*(arr+i+1) = *(arr+i);
i++;
}
*(arr+i+1) = temp;
}
}

Friday, March 26, 2010

Search Ranjeet's Blog