A warrior never surrenders, A warrior never quits..


Saturday, December 31, 2011

In Memory Hashtable

In web applications you mostly get into this kind of situations where you need a code or API which acts as Hashtable and since you want it lightning fast, it must be in the memory instead of disk storage. So how would you create an In Memory Hashtable for Strings for a large set.

I had enough experience with hashtable to understand that the whole bottleneck is going to be the hash function which maps Strings to LongInts. After some brainstorming I got the solution. So here we go:
1. Create a Trie and provide to methods to set and retrieve the values. Something like this:
     bool insert(String s)
     long retrieve(String s)
     bool exist(String s)

2. Create a list of String to be inserted in the beginning and use a file to perform the operation.

3. Once you are done with the initial setup now you can put your Hashtable live to answer your queries.

4. Create a daemon and put the above code into it.

5. Whenever you want to shoot a query invoke the daemon and call the retrieve function.

I hope the main problem would be figuring out the appropriate solution and writing a code for daemon and hashtable won't be a problem.

This can be implemented in many ways and on any architecture. But since mostly the servers are located on Linux machines, mostly it'll be useful on Linux environment with C being the underlying language. However I admit that if you use Java or Python it will be much easier to code. I used unix message passing mechanism to handle IPC.

Anybody wants the working code then write to me here

Thursday, June 23, 2011

Science of Computer Science

With increasing number of students getting out of college with the same Bachelor degree of Computer Science, its important to focus of some key area of Computer Science to get an edge over others. I created a list (or a wish list in some sense) of topics that I promised myself to learn. However the list itself was volatile and this excuse was sufficient enough to not abide to the promise :D. Finally it is now in stable state. So here we go...

> Good command over a couple of Programming Languages ( C/C++ and/or Java would do)
> One Scripting Language. (Python (preferred), Java Script, Bash anyone)
> Good understanding of OS (Life is as simple as Unix)
> Database handling (need not be an expert but capable of understanding/performing normalizations and basic queries)
> Basic Networking concepts (If time permits and unless your only goal is to get into Cisco :) )
> Finally Data Structures and Algorithms with Coding (No limits)

With these Arms and Ammo in your disposal, you are ready to face any challenge. But war does not always win by the tangible things.
You can apply in Google/ Facebook/ Twitter/ Microsoft/ Adobe etc or better go for research in any of the topic you liked.

Disclaimer: This list is prepared by me, being an average student throughout. It includes only my thoughts & is not targeted towards outstanding and brilliant students. :P

Saturday, June 18, 2011

code for fun

Program to Add/Subtract/Multiply 2 numbers..
Sounds pretty easy, so is the code for it.


#define s(c) if(scanf("%c",&c)){}
#define p(b) if(b+=(c-'0')){} if(b*=10){}
#define w(a,b) while(a>b){
#define i(a,b) if(a=b){}
main(int a,int b,int n,char c,char o)
{
if(scanf("%d",&n)){}
s(c)
w(n--,0)
i(a,0)
i(b,0)
s(c)
w(c,47)
p(a)
s(c)
}
if(a/=10){}
i(o,c)
s(c)
w(c,47)
p(b)
s(c)
}
if(b/=10){}
if(o=='+')
if(a=a+b){}
if(o=='-')
if(a=a-b){}
if(o=='*')
if(a=a*b){}
if(printf("%d\n",a)){}
}
}

However if you have noticed it correctly you would have found it unusual in the sense that it does not have any single semicolon. The unnecessary directives are to make the code shorter but it made the code ugly nevertheless.
I am still trying to make it smaller. If any one has better idea regarding it then comments are welcome.
You can compile the code successfully on any GCC compiler and run it as described below:
3 (3 test cases)
123+456
output: 579
456-123
output:333
123*45
output: 5535

happy coding!

Thursday, June 9, 2011

When pointer to functions fascinated me...

Some time back I was fascinated by pointer to function concept. Not only it gives the flexibility of CallBack, its more faster nevertheless. But instead of writing plain code I thought to write a code that looks different. Here it is...

void _(int *_o,int o_){o_<7?printf("%c",o_[_o]),o_++,_(_o,o_):printf("\n");}
void o_o(void (*_)(int*,int),int *_0,int __){_(_0,__);}
main(){int _O_[]={83,104,117,98,104,97,0x6d},_i_=0;
void (*_o_)(int*,int)=&_;
void (*_0_)(void(*__)(int*,int),int*,int)=&o_o;
_0_(_o_,_O_,_i_);}


All it does is print my name.