Wednesday, December 30, 2009

Insert Ignore and Replace in MySql


Source

Use INSERT IGNORE rather than INSERT. If a record doesn't duplicate an existing record, MySQL inserts it as usual. If the record is a duplicate, the IGNORE keyword tells MySQL to discard it silently without generating an error.

Following example does not error out and same time it will not insert duplicate records.

mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 0 rows affected (0.00 sec)

Use REPLACE rather than INSERT. If the record is new, it's inserted just as with INSERT. If it's a duplicate, the new record replaces the old one:

mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 1 row affected (0.00 sec)
mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 2 rows affected (0.00 sec)

INSERT IGNORE and REPLACE should be chosen according to the duplicate-handling behavior you want to effect. INSERT IGNORE keeps the first of a set of duplicated records and discards the rest. REPLACE keeps the last of a set of duplicates and erase out any earlier ones.

Monday, December 28, 2009

mysql command prompt paging options

mysql --pager=more
mysql --pager=less

or
Source

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*));
}

Wednesday, December 9, 2009

C Dictionary

#include

struct node
{
char data;
struct node* next;
int numChildren;
char isTerminal;
};

int main()
{
struct node* aroot = malloc(sizeof(struct node));
struct node* tmp;
int i = 0;
aroot->data = 'a';
aroot->isTerminal = 1;
aroot->numChildren = 0;
aroot->next = malloc(sizeof(struct node));
++(aroot->numChildren);
aroot->next->data = 'n';
aroot->next->isTerminal = 1;
aroot->next->numChildren = 0;
tmp = (struct node*) realloc(aroot->next,aroot->numChildren + 1);
if(tmp)
aroot->next = tmp;
++(aroot->numChildren);
(aroot->next[1]).data = 'm';
(aroot->next[1]).isTerminal = 0;
//printf("%c\n",(aroot->next[0]).data);
//printf("%c\n",(aroot->next[1]).data);
for(i=0;inumChildren;i++)
{
//if(aroot->next[i].isTerminal)
printf("%c\n",(aroot->next[i]).data);
}
}

A whiteboard built using Flex/PHP/MySql

Here.

Maximum contiguous subarray problem

View and Edit here.
Copied verbatim from here.

Sunday, December 6, 2009

Grep for space and new line

I had to grep the following pattern followed by a new line :
"belongs to "
so it is
grep belongs[[:space:]]to[[:space:]]*$

I had to use '*' as I guess there are more characters than
space at the end.

Friday, December 4, 2009

Quick Sort in C by Kernighan Ritchie

void pArr(int v[],int left,int right)
{
int i;
for(i=left;i<=right;i++) printf("%d ",v[i]); printf("\n"); } void swap(int v[],int i,int j) { int tmp; tmp= v[i]; v[i] = v[j]; v[j] = tmp; } void qsort(int v[],int left,int right) { int i,last; pArr(v,left,right); if (left >= right)
return;
swap(v,left,(left+right)/2);
last = left;
for(i = left + 1;i<=right; i++)
if(v[i] < v[left])
swap(v,++last,i);
swap(v,left,last);
qsort(v,left,last - 1);
qsort(v,last + 1,right);
}

int main()
{

int a[8] = {1,5,8,11,3,4,2,3};
pArr(a,0,7);
qsort(a,0,7);
pArr(a,0,7);
}

A simple hash implementation in C

#include
#include

struct node{
int key;
char *value;
struct node* next;
};

struct node* hash[5];

char* m_strdup(char* tmp)
{
int len = strlen(tmp);
char* newmem = malloc(sizeof(char)*len);
if(!newmem)
{
fprintf(stderr,"problem in memory allocation");
exit(0);
}
strcpy(newmem,tmp);
return newmem;
}

void traverse(struct node* root)
{
printf("traversing\n");
while(root)
{
printf("%s",root->value);
root = root->next;
}
printf("\n");
}
void addnew(int value)
{
int key = value%5;
char tmp[10];
int counter = 0;
tmp[0] = 'a' + value;
tmp[1] = 0;
struct node* curr;
if(!hash[key])
{
hash[key] = malloc(sizeof(struct node));
hash[key]->value = m_strdup(tmp);
hash[key]->next = NULL;
printf("added at %d - %d\n",key,counter);
}
else
{
curr = hash[key];
while(hash[key]->next)
{
hash[key] = hash[key]->next;
counter++;
}
hash[key]->next = malloc(sizeof(struct node));
hash[key]->next->value = m_strdup(tmp);
hash[key]->next->next = NULL;
hash[key] = curr;
printf("added at %d - %d\n",key,counter);
}
}

void lookup(int value)
{
int key = value%5;
int counter = 0;
char tmp[10];
tmp[0] = 'a' + value;
tmp[1] = 0;
struct node* curr = hash[key];
while(curr)
{
if(strcmp(curr->value,tmp)==0)
{
printf("found at %d - %d\n",key,counter);
break;
}
curr = curr->next;
counter++;
}
//hash[key] = curr;
}

int main()
{
int i;
for (i=0;i<5;i++)
hash[i] = NULL;
addnew(1);
addnew(2);
addnew(22);
addnew(12);
addnew(3);
addnew(13);
traverse(hash[3]);
lookup(1);
lookup(2);
lookup(3);
lookup(22);
lookup(12);
lookup(13);
}

Blog Archive