Monday, December 28, 2009

mysql command prompt auto completion

mysql>\#

Wednesday, December 16, 2009

Oracle trigger question

create or replace trigger trig1
before insert on emp1
for each row
begin
insert into emp1(empid) values (seq1.nextval);
end;
OR
create or replace trigger trig2
before insert on emp1
for each row
begin
select seq1.nextval into :new.empid from dual;
end;
/

Answer : Second one.
First one will result in this :
SQL> insert into emp1 (empid) values (1);
insert into emp1 (empid) values (1)
*
ERROR at line 1:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512

Tuesday, December 15, 2009

Finding whether a number is blessed or not

Problem

1,2,3,4,5,6,....

initialize
Jump=2
then 2,4,6,8,.......

remaining 1,3,5,7,9,11,.......

Jump=3
then 5,11,..........gets removed

remaining: 1,3,7,9,13,15

We carry on for jump infinitely here 1.3 are blessed as they will not be removed. Now, given a number n propose a algorithm to find out whether it is blessed number or not.

Solution :
#include
int counter = 2;
int isBlessed(int k)
{
//printf("k = %d counter = %d\n",k,counter);
if(k< counter) return 1;

if(k%counter == 0)
{
return 0;//it's not blessed
}
else
{
k = k - k/counter;
++counter;
return isBlessed(k);
}
}


int main()
{
int i;
for(i=0;i<25;i++)
{
counter = 2;
if(isBlessed(i))
printf("%d is blessed\n",i);
else
printf("%d is not blessed\n",i);
}
}

Monday, December 14, 2009

Generating readable assembly with gcc

Lifted from here.

>
gcc -g -c test.c
> objdump -d -M intel -S test.o

test
.o: file format elf32-i386


Disassembly of section .text:

00000000
:
#include

int main(void)
{
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: 83 e4 f0 and esp,0xfffffff0
6: 83 ec 10 sub esp,0x10
puts
("test");
9: c7 04 24 00 00 00 00 mov DWORD PTR [esp],0x0
10: e8 fc ff ff ff call 11 <main+0x11>

return 0;
15: b8 00 00 00 00 mov eax,0x0
}
1a: c9 leave
1b: c3 ret

Brace matching : Flex Builder

Source
A feature forever undocumented in Flex Builder is the ability to jump directly to matching braces. To use it, simply place your cursor to the left on an { or the right of an } and press Ctrl/Cmd+shift+P. For the JDT nerds out there, it’s the same key command. Enjoy!

Sunday, December 13, 2009

Finding which place belongs to which district using Ruby

I have a csv file which contains a list of locations with latitude and longitude.
I have a kml file which contains names of districts and the co-ordinates
of their boundary.
I have to find out which location belongs to which district using ruby.
Here is the ruby code, kml(6.33MB) and csv(4.05 MB) file.

Saturday, December 12, 2009

Iterative/Non-recursive version of pre order/in order traversal of a binary tree

struct node
{
int data;
int data1;
int data2;
struct node* left;
struct node* right;
};
struct node** stack;
int curr = -1;
void push(struct node* node)
{
++curr;
stack[curr] = node;
}

struct node* pop()
{
return stack[curr--];
}
void visit(struct node* curr)
{
printf("%d ",curr->data);
}

void preOrderiter(struct node* root)
{
printf("*************\n");
struct node* tmp = root;
while(curr != -1 || tmp)
{
while(tmp)
{
push(tmp);
visit(tmp);
tmp = tmp->left;
}
tmp = pop();
tmp = tmp->right;
}
printf("*************\n");
}


void inOrderiter(struct node* root)
{
printf("*************\n");
struct node* tmp = root;
while(curr != -1 || tmp)
{
while(tmp)
{
push(tmp);
tmp = tmp->left;
}
tmp = pop();
visit(tmp);
/*if(!tmp->right)
{
printStack();
}
else*/
tmp = tmp->right;
}
printf("*************\n");
}
int main()
{
stack = malloc(10*sizeof(struct node*));
}

Blog Archive