16 November 2011

Puzzle - Mega Substring

Scenario #1
Given two strings - string1 and string2,  write a program to find whether string2 is a part of string1 (substring of string1). The above puzzle may seem to be very simple as it is. If this sound silly, read on.

Scenario #2
Assume that you have to write above substring program to compare the contents of two files whose sizes put together is more than the size of your RAM + SWAP partition (let us say 10GB of contents). If you feel like you can solve this with bit of try, move on to the next scenario.

Scenario #3:
What if I want to exploit the number of cores? Will it be better if I have few threads to do the job. If i choose to go with multi-threaded way, what are the special conditions that i should take care of?

Watch out this place for answer (probably this weekend - 20/Nov/2011)

05 November 2011

Time Server - Network Programming

Happen to flip first few pages of Unix Network Programming by Richard Stevens. Wrote a simple time server and client.

The server listens to specified port for incoming connections and sends the current time in seconds since Epoch (midnight of 1-1-1970 UTC).

The client is written to connect to the server, gets the time and displays it.

Here is the screenshot of the output (server is started as background process, netstat shows the server is listening in port 12000. The client connects to server and gets the current time).

The source files (with comments, so that one can follow) are uploaded to my Dropbox account. Sharing it here, just in case if you want to look/run the piece of code.

Server Source
Client Source

Compile the above programs (gcc client.c -o client & gcc server.c -o server) and run it as shown in the pic.

02 November 2011

Palindrome in Haskell

This is probably a week after i started to read Haskell (a functional programming language). Having tried few examples from the book and writing few on my own, i feel like it is a different world. If i m successful enough to keep my interest with Haskell and continue to practice, i think it is going to improve my thinking at a very least. I m not so sure of using this language at work (but who knows). More on Haskell later.

Here is the code that says a given String (list) is palindrome. (see, how expressive Haskell (FP) is). The first two line is to reverse the list and the last line is the one that decides whether or not the original and reversed string same (you need to write probably 10 lines in imperative language)

rev [] = [] 
rev (x:xs) = reverse xs ++ [x] 
palindrome x = (x == rev x)
 Here is the sample output (from WinGHCi)

*Main> palindrome "test"
*Main> palindrome "madam"
*Main> palindrome "123321"
*Main> palindrome [1..100]
*Main> palindrome "a great good haskell"

You can find my Haskell notebook (and my daily progress) here. (thanks to Dropbox)