Saturday, April 25, 2009

Anagram Solver

First Things First : I am worse at solving Anagrams but a little bit better at writing a Code for solving them :D .

This Post is about a Code that I wrote for solving Anagrams.It all started with this bot friend in my friend list : games@gtalkbots.com.It is a bot that allows users to play anagram with other users.

I was(rather am) really pathetic at solving anagrams instantaneously.Instead of sulking, I thought I must do something about it. I could win each round if I could get an answer within 0.1 secs. (My own Heuristic).

So,The Best way is to write a C++ Code for the same.Then,after little brain storming (random thoughts actually), I just needed a Dictionary and a Data Structure to store it. Since Hashing was out of question for a Quick-Code,STL Set was the second Best with Insertion /Look up of Asymptotic Upper Bound of O(log(n)).So, I just had to check all Permutations of the Random String and look up the Dictionary and if Match is found then Output the Result.

Within first 2 results for Dictionary in Google, I found a US Dictionary List in txt format.
So, The C++ Code for the same ::


#include<iostream>
#include<string>
#include<cstdlib>
#include<set>
#include<fstream>
using namespace std;

// This is a Set that Stores the Dictionary Values.

set<string>s;

/*
* This Method is used to check whether the String matches the given Mask.
* Example : srinivas --i-i--s
*/

bool check(string a, string b) {
for (int i = 0; i < a.size(); i++)
if (isalpha(a[i]) && isalpha(b[i]) && a[i] != b[i])
return false;
return true;
}

/*
* This Method is used to find all Permutations of the String
*/

void perm(string pre, string suff) {

if (!suff.size())
cout << (s.find(pre) != s.end() ? pre + '\n' : "");
else
for (int i = 0; i < suff.size(); i++) {
perm(pre + suff[i], suff.substr(0, i) + suff.substr(i + 1, suff.size()));
}
}

int main() {

s.clear();
string a;
ifstream in;
in.open("US.dic");
while (in >> a)
s.insert(a);
in.close();
cout << s.size() << endl;
while (cin >> a) {
perm("", a);
cout << "----------------------\n";
}
return 0;
}



But then as Luck would have it, The Bot went offline after I wrote the Code. I guess both of them are somehow related. But then Next Time I find the bot online,you wouldn't wonder if you find my name as one of the Top-Scorer's.

Cheers!!!
Do Comment/Post about it.

Friday, April 24, 2009

EasyProb

This is one of the Problems that I did long time ago but then I came back to it today. I had a text file containing the Result that had been generated using the Code though the Code was missing.
So, I had to start everything from scratch and I have become a bit Rusty though got it working in some time.

The Code for the Same ::



#include <iostream>
#include <fstream>
using namespace std;
/*
* String Uses a Recursive Function to Achieve the Effect.
*/
string ret(int a) {

/*
* Base Cases
*/
if (a <= 0)
return "";
else if (a == 1)
return "2(0)";
else if (a == 2)
return "2";
/*
* Recursive Step Begins here.
*/

else {
string s = "";
int count = 1;
while (a > 0) {
if (a % 2 == 1)
//Magic Statement
s = (count <= 2 ? ret(count) : "2(" + ret(count - 1) + ")")+(s == "" ? "" : "+") + s;
count++;
a /= 2;
}
return s;
}
}

int main() {
int a;
while (cin >> a)
cout << a << "=" << ret(a) << endl;
return 0;
}



It feels good to get such Codes working in real quick time ;) .
PS :
1) The Output for all The Inputs in the Problem::

137=2(2(2)+2+2(0))+2(2+2(0))+2(0)
1315=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
73=2(2(2)+2)+2(2+2(0))+2(0)
136=2(2(2)+2+2(0))+2(2+2(0))
255=2(2(2)+2+2(0))+2(2(2)+2)+2(2(2)+2(0))+2(2(2))+2(2+2(0))+2(2)+2+2(0)
1384=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2)+2(2(2)+2(0))+2(2+2(0))
16385=2(2(2+2(0))+2(2)+2)+2(0)

2) If you do have a code that is better than mine Please make it a point to post/Comment about it.

Friday, April 17, 2009

Addicted to Blogging

Well, This is a short Post Considering the fact that I have my Exams going on.
I took a short Online Test to check my addiction to Blogging.
The result is in form of a IMG.

62%How Addicted to Blogging Are You?



Cheers!!!!

Saturday, April 11, 2009

Google Ad-Sense Pdf's

This is about a series of Pdf's that I found on Google's UK Server which is about Case Studies of Ad-Sense Program.
I was reading about Configuring robots.txt File from this URL.

Then Began a Series of Re-Direction of URL's (http://google.co.in/robots.txt -->http://www.google.com/sitemaps_webmasters.xml-->http://www.google.co.uk/intl/en/adtoolkit/pdfs/pdf_sitemap.txt)until i reached out for a URL which contained a list of Pdf's.
I was too curious to find the Contents of all Pdf's in a single go.That is when i realized that I could download-them-all (No Pun Intended) real quick if I can use Shell scripts to semi-automate the download Process.

The Process I followed :
1) Copied the Contents of a txt file to a file on the system.

2)I just had to insert a Firefox before each URL.This was the Difficult Part since I was really awful at tweaking shell scripts to achieve that. Instead I wrote a Quick C++ Code to do the same with File Handling(It was guaranteed to work too ).
The C++ Code for the same ::




#include <iostream>
#include <fstream>
using namespace std;

int main() {
/*
* links.txt is the Original File that Contained the list of URL's.
*/
ifstream in("links.txt");
/*
* link.sh is the shell Script which was to automate the Process.
*/
ofstream out("link.sh");

string s = "";
while (getline(in, s)) {
// This line enabled the Insertion of firefox before each URL.
out << "firefox " << s << endl;
}
in.close();
out.close();
return 0;
}




3) I had disabled all Prompts for Download in the Preferences Tab. So, it would Download all of these pdfs without prompting me ;).After this, I just had to run the Script from the Terminal and Voila!! All Pdf's were downloaded within 1-2 minutes.

4) Later, I had to move the pdf's from default download directory to a different Directory.

5) Finally, using wc-l/Word Count for lines,I found out that I had downloaded around 167 pdf's (some of them being confidential :P ). Since it is accessible for public viewing .. it doesn't matter.

I did all these steps within a matter of around 4-5 minutes which is real-quick for me.
The Entire Process was real fun.. In case you had come across such quick-tweak then do post about it..

PS:
1) Advanced Shell Script Tweaking is a definite To Do Thing next Semester.

2) wget was a better option than Firefox :( .. I am on the learning Curve ... Trying to Grasp Few things..

Wednesday, April 8, 2009

W3C Validation for some Interesting Websites.

This is an Interesting Post considering that Most Websites with very high Network Traffic don't pass the W3C Validation.

The Only Website in my Test Space which passes the Validation is the Very Bankable Wikipedia.

Now The Microsoft Flagship Sites:
Microsoft Default Homepage ::
has 180 Errors, 32 warning(s) .
The Microsoft Search Site ::Live has 4 errors.

Google also has a fair share of this non-validation :
Indian Homepage for Google :: google.co.in has 58 Errors, 13 warning(s).

Amazon.com Homepage :: A2Z has 1587 Errors, 157 warning(s).

Indian Page for Amazon India Development Center :: Amazon India has 8 Errors, 5 warning(s).

Yahoo! Homepage :: Yahoo! has 34 Errors, 8 warning(s).

While Facebook site ::Facebook has 42 Errors, 8 warning(s).

Our Very own Indian Railways Site :: Indianrail has 240 Errors, 14 warning(s).

After these Results, I am compelled to think that whether W3C Validation mean much to us anyways? I mean we cannot stop using these sites just because they don't comply with certain standards.

PS: NIT Durgapur Homepage :: NIT DGP has 65 Errors, 6 warning(s).

Friday, April 3, 2009

Google Mail again

I now its kind of creepy but I have become an obsessive user of Google Mail/GMail of late so this post is again about GMail and the introduction of Support for Indian Languages in GMail.

The Google Transliteration Tool for Indian Language Rocks!!! :D and it has added more flexibility to the entire functioning of GMail as well.
You can Read more about this in the Official GMail Blog link here.
Plus There are a few Corrections regarding my Earlier Post about The GMail Bug : It isn't exactly a Bug since the GMail Interface uses Local System Time to show the Time Elapsed But According to me The Interface should be consistent with all GMail Users in India which means that the Time Reference to find the Time Elapsed should be according to the Google Server Time for India which can be found by searching for the String "current Time" on google.co.in !

Thanks to my friend Roshan Singh for pointing out the actual working of the Elapsed Time in GMail Interface though I had posted the Blog a lot earlier so didn't mind to change it.