Saturday, October 13, 2012

LCA - My attempt in deconstructing the recursive solution

Have given enough comments to explain the solution step by step.

/*LCA in a binary tree

Karumanchi Solution : Recursion (Found in http://www.flipkart.com/data-structures-algorithms-made-easy-java-1466304162/p/itmd34fz5jzb599u
Data Structures and Algorithms Made easy in Java
)

*/

//a, b -> parameters - nodes for which LCA is to be found out, they should remain unchanged throughout
BTNode LCA (BTNode root, BTNode a, BTNode b) {
BTNode left, right;

/*
Base Condition 1: if the root pointer is null - do nothing - return null
*/
if ( root == null ) return null;

/*
Base Condition 2: root pointer in this program is searching for a or b;
  so once we reach a or b, no more search is required.
*/
if ( root == a || root == b ) {
//Pointer comparison is enough here
return root;
}

/*
traverse left and right subtrees
*/

left  = LCA(root.getLeftChild(), a, b);
right = LCA(root.getRightChild(), a , b);

/*
Condition 1:

When nodes a and b are on either side (left and right) of a node,
we know that we have arrived at the LCA
*/
if ( !left && !right ) {
return root;
}

/*
Condition 2:

Condition that is required when both a and b are in the same subtree

        eg: 1
        2
        3

Here let a = 2 and b = 3; LCA of a and b in this case will be 1. 2 and 3 are in the same subtree

*/
return ( left ? left : right);

}

OAuth vs OpenId

Last week when I was in my home-town, I was having a conversation with my brother about OAuth and OpenId. When he asked me the difference between the 2 and asked me to give one line for each protocol I was not able to do so. That is when I realized that I was not clear about both the protocols myself.

As usual, went to the drawing board to figure out what each protocol is for.

Found the following presentation extremely helpful :
http://www.slideshare.net/rmetzler/identity-on-the-web-openid-vs-oauth

Summarizing the content here :

OpenId - is meant for identifying the user in the web-world. Only for identification. (as the name says)

OAuth - is required when a 3rd party is looking to access a user's data present in another server.
               Data access.

OpenId - Identification
OAuth  - Data access

Period.

Sunday, July 15, 2012

My Girl

The warmth, the smell, the touch
The kiss, The cuddle and the talk
She makes me feel alive
She makes me feel good

The courage, the confidence, the smile
The conduct, the style and the face
She helps me clear my head
She helps me feel confident

The smart, the sweet, the cute
The hot, The bad and the sexy
She makes me feel my manhood
She makes me what I am

The sad, the hate, the worries
The cry, The weak and the sob
She helps me understand the world
She helps me know my limits

The pain, the remorse, the mistakes
The regret, the past and the failures
She makes me forgive everything
She makes me learn from it

The dark, the fear, the unknown
The ghosts, the demons and the devils
She helps me stay alert
She helps me be inquisitive

The lessons, the experience, the feelings
The care, the affection and the love
She has given me everything
That I will live my life with her, for her, only her

Tuesday, July 3, 2012

perl - Identifying who is loading a module

So far I was under the impression that it is difficult to identify who is loading a module since all the use statements are executed even before the debugger prompt appears on the terminal.
Ref: http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END

One can setup a break point when a module is loaded by another module.
b load GT/A.pm # this will set a breakpoint on module load of A.pm

But since use statements are evaluated much before the debug prompt appears on the terminal, we will not be able to identify who has actually loaded the module.

However, we can identify the same via a workaround as follows:

1. Start the debugger
2. set a breakpoint on load of a module (eg: b load GT/A.pm)
3. R # This will restart the debugger session - however, the debug point will be retained
4. T # see the stack trace to identify who is trying to load the module


I will upload the script and module used to experiment this, shortly. Meanwhile, one can take a look at the demonstration of this hack below.


ganesathandavamponnuraj@GT-MBP.local:DebugModuleLoad:>>perl -d useAB.pl

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

A BEGIN
A static
A method print
B BEGIN
B static
B method print
Inside BEGIN block
main::(useAB.pl:8): print "Before function call\n";
  DB<1> b load GT/A.pm
Will stop on load of `GT/A.pm'.
  DB<2> R
Warning: some settings and command-line options may be lost!

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

A BEGIN
'GT/A.pm' loaded...
GT::A::CODE(0x825600)(GT/A.pm:8): print "A static\n";
  DB<2> T
$ = require 'GT/A.pm' called from file `useAB.pl' line 1
$ = main::BEGIN() called from file `GT/A.pm' line 0
$ = eval {...} called from file `GT/A.pm' line 0
  DB<2>

Thursday, March 22, 2012

Mac Address Book and gmail LDAP sync

Given that majority of the people on this planet, who have a web-mail account, have a gmail-id, it's natural that almost all of our contacts are residing on gmail's address book. But there are obvious advantages associated with using a desktop client for tracking your mails. (http://familyemailhosting.com/advantages-of-using-an-email-client.php)
But it becomes a pain when your Address book on the Mac OS X(Snow Leopard) doesnt contain all the addresses you have on the gmail address book.
( To the contrary, I found this article on the web that hotmail and yahoo mail have a larger user-base when compared to gmail - Incredible!, I say http://www.cbsnews.com/8301-501465_162-20022793-501465.html )
Inorder to access the gmail address book en masse on your Apple Mail client, do the following.
http://www.techrepublic.com/blog/mac/syncing-macs-address-book-and-ical-with-google-apps/266
1. Open Address Book App; Accounts tab - Enable Google Sync
2. Then force sync, for the first time alone by choosing the sync icon from the Finder Bar.
Now you should be able to see the contacts in your gmail address book, from your Apple Mail client!!!

PS: mrplow pointed out that this doesnt work on OS X Lion, and so I have edited the post accordingly. Thanks mrplow for pointing that out. (I havent tried it myself, I am trusting mrplow to be a Lion user, here :) )

Edit 2:
[Update] Kara Moss pointed out (in the comments section) that the solution mentioned here works on OS X Mountain Lion too. \m/. Looks like it doesnt work only on OS X Lion, AFAIK. It works like a charm on both OS X Snow Leopard and OS X Mountain Lion.

GT

Saturday, December 31, 2011

HowTo Install molokai on MacVim

HowTo Install molokai on MacVim Heard about the Molokai theme for vim editors from one of my friends. But the challenge I had was in knowing where to install this .vim file for the MacVim RunTime to detect this theme.
Useful Pointer: Its about knowing the $VIMRUNTIME (intuitive variable name FTW) value. Once you know this, you know where to install the molokai.vim file.

After some search I was able to install using the following steps:
Download molokai.vim and place it in $VIMRUNTIME/colors folder and you are all set.
MacVim lets you use this theme in 2 ways: 1. Traditional vim way of saying :color molokai 2. Edit->Colorscheme->molokai

Happy Molokai. (one of the reasons i like molokai - dark theme)
PS: Also you might need to Enable usage of Experimental Renderer in MacVim.
Preferences-> Advanced -> enable experimental renderer

Monday, August 15, 2011

Be smart, be professional! (In defence of the Indian Cricket Team)

I guess this post is in a way influenced by the fact that I am a big-time supporter of the Indian cricket team. People may find it really odd and might even go to the extent of tagging me as a jobless fellow and all, but still why I felt the need for such a post is because, this experience has taught me the importance of being a smart, professional cricketer. It applies not just for cricket, and this is the biggest take-away here, but for each and every profession, in each and every point in our lives.

Coming to the core-idea, I guess the Indian players have got it all wrong in prioritizing the matches. After having won the world-cup, after a grueling 45 days of cricket, they "Ideally" should have taken some time off to enjoy, relish, cherish the moment of glory. Instead what followed, in just 5 days time after the finals, was the big IPL dhamaka. With it came the injuries, along with the big-bucks and attention.

Looking at the turn of events, I think, the players thought was to take some time off during the WI tour so that they could have a nice time, infact have time to think about the WC glory; and they wanted to come back and play their part in the English tour. Meanwhile, the English were licking their lips and were waiting for their opponents to come home to face some music. In strange waters, Indians found the going tough. To add to the lack of preparation and match-practice is the fact that not many were even match-fit, courtesy taking time-off the WI tour to bask in WC glory; Waist-lines of Harbhajan, Yuvraj and Zaheer is a testimony to this fact.

And so here is the moral of the story: Ideally Indians should have skipped the IPL and should have come all guns blazing for the English series, and who knows the results could have been slightly different. But, its easier said than done. So, the Indians failed to be smart, failed to show professionalism, which cost them the No 1 ranking.

And the problem I guess is deep down in Indian mentality. I always had a feeling that we are emotionally attached to so many thing that we come across in our lives (atleast majority of the people that I have come across in my life, so far) that we fail to act smart in most critical of the times. I believe it is going to take some time for the Indians to overcome this emotional hurdles and to display some smartness during tough times. Ofcourse, I am not saying we are not being smart at all, always. But however, I do think that we could be much better off controlling our emotions and display our smartness instead!!!

(May be I am talking to myself :D, I don't know honestly!. This post came out of the chat I had with my brother over the weekend. Felt like making a blog-entry out of it.)

(Added "In defence of Indian cricket team" in the title, to grab the attention of the casual-onlookers :) )