# Causes Puzzle Solution: Improvements needed

my ( \$self, \$newMember ) = @_;
my \$len = length(\$newMember->{'_data'});
my \$newMemberIndex = push(@{\$self->{'_MemberList'}}, \$newMember) - 1;
\$self->{'_MemberHash'}->{\$newMember->{'_data'}} = \\$newMember;

#Fetch bucket of same length and check for distance
my \$memberList = \$self->{'_Bucket'}->getBucket(\$len);

foreach my \$index (@\$memberList){
if(\$index eq \$newMemberIndex) {
next;
}
my \$memberObj = \$self->{'_MemberList'}[\$index];
if(\$self->isFriend(\$newMember->{'_data'}, \$memberObj->{'_data'}) eq true) {
}
}

#Fetch bucket of length - 1 and check for distance
\$memberList = \$self->{'_Bucket'}->getBucket(\$len-1);
foreach my \$index (@\$memberList){
if(\$index eq \$newMemberIndex) {
next;
}
my \$memberObj = \$self->{'_MemberList'}[\$index];
if(\$self->isFriend(\$newMember->{'_data'}, \$memberObj->{'_data'}) eq true) {
}
}
#Fetch Bucket of length + 1 and check for distance
\$memberList = \$self->{'_Bucket'}->getBucket(\$len+1);
foreach my \$index (@\$memberList){
if(\$index eq \$newMemberIndex) {
next;
}
my \$memberObj = \$self->{'_MemberList'}->[\$index];
if(\$self->isFriend(\$newMember->{'_data'}, \$memberObj->{'_data'}) eq true) {
}
}

}

More Causes Puzzle Code...

-
 As per FAQ: "This site is for code reviews, which are hard to do when the code is behind a link somewhere out there on the internet. If you want a code review, you must post the relevant snippets of code in your question. It is fine to post a "see more" link (though, do be careful — very few reviewers will be willing to click through and read thousands of lines of your code), but the most important parts of the code must be placed directly in the question." – Michael K Mar 19 '12 at 15:37 Thanks for pointing that out. Will make sure it going forward. – Swapnil Tailor Mar 20 '12 at 2:07

Here is one idea: You could first put the words into a hashmap, the length of them being the key. O(n)

Then, since you are only interested in "causes", grab that word and make a node and remove it from the hashmap.

Then, a recursive function

• adds all friends to the node that are in the hashmap (only looking at the relevant lengths)
• removes those from the hashmap
• calls itself on those new nodes

The end result is the social graph of "causes".

It really depends what you are interested in in improving though: For instance, one could assume the whole thing is only called once for a fixed string, or it could be called many many times for different inputs. Depending on that, one might precompute more.

-
 I need to give it a try, will let you know the results – Swapnil Tailor Mar 23 '12 at 23:25