<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The Code Dreamer : Nitin Hayaran</title>
	
	<link>http://www.nitinh.com</link>
	<description>Nitin Hayaran's Blog</description>
	<lastBuildDate>Sun, 29 Jan 2012 07:28:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheCodeDreamer" /><feedburner:info uri="thecodedreamer" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>TheCodeDreamer</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Facebook, Twitter, Instapaper Connect Buttons</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/usOqpVu9aE8/</link>
		<comments>http://www.nitinh.com/2011/05/facebook-twitter-instapaper-connect-buttons/#comments</comments>
		<pubDate>Sun, 15 May 2011 15:07:57 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Freebies]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=535</guid>
		<description><![CDATA[For one of my project I had to use social connect buttons, with Facebook, Twitter, and Instapaper. I tried searching for any buttons that fits my bill, but that went in vain. Later I created these buttons myself using Photoshop. I am releasing these buttons source, may be it'll save others time. Here is the [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/10/beautiful-and-simple-buttons-with-css/' rel='bookmark' title='Permanent Link: Beautiful and Simple Buttons with CSS'>Beautiful and Simple Buttons with CSS</a></li>
<li><a href='http://www.nitinh.com/2009/03/news-twitter-friendfeed-and-many-more-in-gtalk-are-chat-client-becoming-new-browsers/' rel='bookmark' title='Permanent Link: News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?'>News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For one of my project I had to use social connect buttons, with Facebook, Twitter, and Instapaper. I tried searching for any buttons that fits my bill, but that went in vain. Later I created these buttons myself using Photoshop. I am releasing these buttons source, may be it'll save others time.<br />
Here is the ScreenShot of these buttons.<span id="more-535"></span></p>
<p style="text-align: center;"><a href="http://www.nitinh.com/wp-content/uploads/2011/05/social-connect-botton.png"><img class="size-full wp-image-536 aligncenter" title="Social Connect Buttons" src="http://www.nitinh.com/wp-content/uploads/2011/05/social-connect-botton.png" alt="Social Connect Buttons" width="400" height="200" /></a></p>
<p>Download it here : <a href="http://www.nitinh.com/static/social-connect-botton.psd">Social Connect Buttons [psd]</a></p>
<p>&nbsp;</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/10/beautiful-and-simple-buttons-with-css/' rel='bookmark' title='Permanent Link: Beautiful and Simple Buttons with CSS'>Beautiful and Simple Buttons with CSS</a></li>
<li><a href='http://www.nitinh.com/2009/03/news-twitter-friendfeed-and-many-more-in-gtalk-are-chat-client-becoming-new-browsers/' rel='bookmark' title='Permanent Link: News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?'>News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/usOqpVu9aE8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2011/05/facebook-twitter-instapaper-connect-buttons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2011/05/facebook-twitter-instapaper-connect-buttons/</feedburner:origLink></item>
		<item>
		<title>How to Properly Hide and Show Elements Using Javascript</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/i8jE143zL1o/</link>
		<comments>http://www.nitinh.com/2011/05/how-to-properly-hide-and-show-elements-using-javascript/#comments</comments>
		<pubDate>Sun, 15 May 2011 14:50:09 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=529</guid>
		<description><![CDATA[What is the first solution comes to your mind, when someone asks you How can I dynamically hide any element using Javascript ? The obvious solutions to this is to set "display" style to "none". Something like this: document.getElementById&#40;'element'&#41;.style.display = 'none'; This will surely work, but is it the correct way to do so? May [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2010/06/energy-saving-javascript/' rel='bookmark' title='Permanent Link: Energy Saving Javascript'>Energy Saving Javascript</a></li>
<li><a href='http://www.nitinh.com/2009/10/show-imdb-ratings-on-torrentzcom-using-greasemonkey-script/' rel='bookmark' title='Permanent Link: Show IMDB Ratings on Torrentz.com using GreaseMonkey Script'>Show IMDB Ratings on Torrentz.com using GreaseMonkey Script</a></li>
<li><a href='http://www.nitinh.com/2010/01/vertical-align-middle-using-javascript-jquery-mootools/' rel='bookmark' title='Permanent Link: Vertical-Align : Middle Using Javascript : JQuery &#038; Mootools'>Vertical-Align : Middle Using Javascript : JQuery &#038; Mootools</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>What is the first solution comes to your mind, when someone asks you</p>
<blockquote><p>How can I dynamically hide any element using Javascript ?</p></blockquote>
<p><span id="more-529"></span></p>
<p>The obvious solutions to this is to set "display" style to "none". Something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will surely work, but is it the correct way to do so? May be not, as you would face problems in showing that element again. The problem in showing that element is that you don't know the original display property of that element. Whether it was a "Block" level element or an "inline" element. So, basically you'll have to do something like this, depending upon type of element:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'inline'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Can we do it in some better way?</p>
<h3>Now do it proper way</h3>
<p>An improved way to achieve similar thing  is to create a css class like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">.<span style="color: #993333;">hide</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span> !important<span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>And now just add or remove this class to hide and show elements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hide'</span><span style="color: #339933;">;</span></pre></div></div>

<p>or write $("element").addClass('hide'); if you are using mootools.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2010/06/energy-saving-javascript/' rel='bookmark' title='Permanent Link: Energy Saving Javascript'>Energy Saving Javascript</a></li>
<li><a href='http://www.nitinh.com/2009/10/show-imdb-ratings-on-torrentzcom-using-greasemonkey-script/' rel='bookmark' title='Permanent Link: Show IMDB Ratings on Torrentz.com using GreaseMonkey Script'>Show IMDB Ratings on Torrentz.com using GreaseMonkey Script</a></li>
<li><a href='http://www.nitinh.com/2010/01/vertical-align-middle-using-javascript-jquery-mootools/' rel='bookmark' title='Permanent Link: Vertical-Align : Middle Using Javascript : JQuery &#038; Mootools'>Vertical-Align : Middle Using Javascript : JQuery &#038; Mootools</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/i8jE143zL1o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2011/05/how-to-properly-hide-and-show-elements-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2011/05/how-to-properly-hide-and-show-elements-using-javascript/</feedburner:origLink></item>
		<item>
		<title>GitHub Setup on Webfaction : How To ?</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/jGqnykNQ6G4/</link>
		<comments>http://www.nitinh.com/2011/02/github-setup-on-webfaction-how-to/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 08:00:47 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[revision]]></category>
		<category><![CDATA[versioning]]></category>
		<category><![CDATA[webfaction]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=521</guid>
		<description><![CDATA[Git is a very popular distributed revision control system with an emphasis on speed. Recently for my projects I need to setup up a private Git Setup on Webfaction Server. So here is the way to Install and start using Git on Webfaction. How to Install Git on Webfaction Log into your Webfaction Control Panel [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://git-scm.com/">Git</a> is a very popular distributed revision control system with an emphasis on speed. Recently for my projects I need to setup up a private Git Setup on <a href="http://www.webfaction.com/">Webfaction</a> Server. So here is the way to Install and start using Git on Webfaction.<span id="more-521"></span></p>
<h3>How to Install Git on Webfaction</h3>
<ol>
<li>Log into your Webfaction Control Panel</li>
<li>Navigate to <strong>Domains/Websites » Applications</strong></li>
<li>Click <strong>Add New</strong></li>
<li>Enter <em>git</em> as the application name in the <strong>Name</strong> field</li>
<li>Select <em>git</em> from the <strong>App category</strong> menu</li>
<li>Enter a password for the default user in the <strong>Extra Info</strong> field</li>
<li>Click the <strong>Create</strong> button</li>
</ol>
<p>The Git application provides a subdirectory, <tt>bin</tt>, which contains the Git executables. It also provides a subdirectory, <tt>repos</tt>, to store your Git repositories. <tt>repos/proj.git</tt> has been created automatically as an example.</p>
<p>Later you'll have to create a Website (Domain/Website -&gt; Website, in Control Panel), which maps this newly created Git application to some subdomain. For further explanation i'll assume this domain to be <em><strong>"git.domain.com"</strong></em></p>
<p>If you want you can delete existing proj.git by simply deleting this directory.<br />
<code>rm -r ~/webapps/<em>git</em>/repos/<em>proj</em>.git</code></p>
<h3>Create a new Git Repository</h3>
<ol>
<li>Open an SSH session to your webfaction account: <code>ssh username@username.webfactional.com</code></li>
<li><code>cd ~/webapps/git/</code></li>
<li><code>./bin/git init --bare ./repos/<em>reponame</em>.git</code><br />
This will create a new repository, where <em>reponame</em>is the name of your repository</li>
<li><code>cd repos/<em>repo</em>.git</code></li>
<li><code>../../bin/git config http.receivepack true</code><br />
This will enable HTTP push to repository</li>
</ol>
<h3>Pushing local code to this repository</h3>
<ol>
<li><code>cd /path/to/your/local/repository/</code></li>
<li><code>git init</code> If it’s not already under version control</li>
<li><code>git add .</code></li>
<li><code>git commit -a -m 'Initial commit.'</code></li>
<li>If you want to use <strong>SSH mode</strong> then<br />
<code>git remote add origin ssh://username@git.domain.com/~/webapps/git/repos/reponame.git</code><br />
For <strong>HTTP Mode</strong><br />
<code>git remote add origin http://username@git.domain.com/reponame.git</code></li>
<li><code>git push origin master</code></li>
<li><code>git push</code></li>
</ol>
<p>Final Command should give you <code>Everything up-to-date</code> message.</p>
<h3>Introduce Yourself to Git Server</h3>
<p>It is a good idea to introduce yourself to git with your name and public email address before doing any operation. The easiest way to do so is:<br />
<code>git config --global user.name "Nitin Hayaran"</code><br />
<code>git config --global user.email you@yourdomain.example.com</code></p>
<h4>Troubleshooting</h4>
<p>On giving <code>git push origin master</code> command git may throw error <code>error: RPC failed; result=22, HTTP code = 411</code>. It will show you message like this:</p>
<blockquote><p>Compressing objects: 100% (792/792), done.<br />
error: RPC failed; result=22, HTTP code = 411<br />
Writing objects: 100% (1148/1148), 18.79 MiB | 13.81 MiB/s, done.<br />
Total 1148 (delta 356), reused 944 (delta 214)</p></blockquote>
<p>This error comes when either your code size is big, or there is any file with big size. To solve this problem you need to increase buffer size in Git configuration.<br />
<code>git config http.postBuffer 524288000</code></p>
<h3>Clone Your Repository Somewhere New</h3>
<p>With SSH Mode Setup<br />
<code>git clone username@git.domain.com:webapps/git/repos/reponame.git</code><br />
For HTTP Push Mode<br />
<code>git clone http://username@git.domain.com/reponame.git</code></p>
<h4>Further Information</h4>
<ul>
<li><a href="http://docs.webfaction.com/software/git.html">Webfaction’s Git Documentation</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/jGqnykNQ6G4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2011/02/github-setup-on-webfaction-how-to/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2011/02/github-setup-on-webfaction-how-to/</feedburner:origLink></item>
		<item>
		<title>Facebook Hacker Cup : Studious Student Problem Solution</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/B9kwHEn6isU/</link>
		<comments>http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 16:25:40 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hacker cup]]></category>
		<category><![CDATA[studious student]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=517</guid>
		<description><![CDATA[Photo Credit : Helen Morgan The Third and final problem of the Facebook Qualification 2011 Round was Studious Student. The problem goes like this: Studious Student You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Peg Game Problem Solution'>Facebook Hacker Cup : Peg Game Problem Solution</a></li>
<li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Double Squares Problem Solution'>Facebook Hacker Cup : Double Squares Problem Solution</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p align="center"><img style="display: block; float: none; margin-left: auto; margin-right: auto" title="Studious Student" alt="Photo Credit" src="http://farm1.static.flickr.com/47/133110591_ef97eb0f4e.jpg" /><span style="font-size: 11px">Photo Credit : <a href="http://www.flickr.com/photos/96499767@N00/133110591/">Helen Morgan</a></span></p>
<p><span id="more-517"></span></p>
<p>The Third and final problem of the Facebook Qualification 2011 Round was Studious Student. The problem goes like this: </p>
<h3>Studious Student</h3>
<blockquote><p>You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and instead make up pointless games based on them. One game you've come up with is to see how you can concatenate the words to generate the lexicographically lowest possible string.</p>
<h5>Input</h5>
<p>As input for playing this game you will receive a text file containing an integer <strong>N</strong>, the number of word sets you need to play your game against. This will be followed by <strong>N</strong> word sets, each starting with an integer <strong>M</strong>, the number of words in the set, followed by <strong>M</strong> words. All tokens in the input will be separated by some whitespace and, aside from <strong>N</strong> and <strong>M</strong>, will consist entirely of lowercase letters.</p>
<p><strong>Output</strong></p>
<p>Your submission should contain the lexicographically shortest strings for each corresponding word set, one per line and in order.</p>
<p><strong>Constraints</strong></p>
<p>1 &lt;= <strong>N</strong> &lt;= 100       <br />1 &lt;= <strong>M</strong> &lt;= 9       <br />1 &lt;= all word lengths &lt;= 10</p>
<p><strong>Example input        <br /></strong></p>
<p>5      <br />6 facebook hacker cup for studious students       <br />5 k duz q rc lvraw       <br />5 mybea zdr yubx xe dyroiy       <br />5 jibw ji jp bw jibw       <br />5 uiuy hopji li j dcyi</p>
<p><strong>Example output</strong></p>
<p>cupfacebookforhackerstudentsstudious      <br />duzklvrawqrc       <br />dyroiymybeaxeyubxzdr       <br />bwjibwjibwjijp       <br />dcyihopjijliuiuy</p>
</blockquote>
<h4>Analysis and Solution</h4>
<p>This problem is a most confusing out of the lot. Me myself made a silly mistake while solving this problem. On first look it looks like the problem should get solved by sorting and joining the strings, but its not like that.</p>
<p>Consider this following input:</p>
<blockquote><p><b>jibw ji jp bw jibw</b></p>
</blockquote>
<p>By sorting and concatenate, the answer should be is:</p>
<blockquote><p><b>bwjijibwjibwjp</b></p>
</blockquote>
<p>However, lexicographically lowest possible string will be:</p>
<blockquote><p><b>bwjibwjibwjijp</b></p>
</blockquote>
<p>Notice in the above words, &quot;<b>ji</b>&quot; is the prefix of &quot;<b>jibw</b>&quot;. The &quot;<i>sort and concatenate</i>&quot; method definitely does not work when there is a case where a word is a prefix of one or more other words.</p>
<p>Some of the contestants solved this problem with brute force, as input range was not that big. With M&lt;=9 one could have only 9!= <b>362880</b> possible permutations. And answer can be obtained by sorting these permutations. </p>
<p>However, I didn’t went this approach. I used python internal Sort function with custom compare. Here is my code</p>
<p> <script src="https://gist.github.com/776381.js?file=studious_student.py"></script></p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Peg Game Problem Solution'>Facebook Hacker Cup : Peg Game Problem Solution</a></li>
<li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Double Squares Problem Solution'>Facebook Hacker Cup : Double Squares Problem Solution</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/B9kwHEn6isU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/</feedburner:origLink></item>
		<item>
		<title>Facebook Hacker Cup : Peg Game Problem Solution</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/SDNlMOpOb-4/</link>
		<comments>http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 16:03:35 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hacker cup]]></category>
		<category><![CDATA[peg game]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=515</guid>
		<description><![CDATA[Continuing with my earlier solution of the First Problem “Double Square”, here is solution and analysis for Second Problem “Peg Game” Peg Game At the arcade, you can play a simple game where a ball is dropped into the top of the game, from a position of your choosing. There are a number of pegs [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Studious Student Problem Solution'>Facebook Hacker Cup : Studious Student Problem Solution</a></li>
<li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Double Squares Problem Solution'>Facebook Hacker Cup : Double Squares Problem Solution</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img style="display: block; float: none; margin-left: auto; margin-right: auto;" src="http://i.imgur.com/Gbi90.jpg" alt="" /></p>
<p><span id="more-515"></span></p>
<p>Continuing with my earlier solution of the First Problem “<a href="http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/">Double Square</a>”, here is solution and analysis for Second Problem “<strong>Peg Game</strong>”</p>
<h3>Peg Game</h3>
<blockquote><p>At the arcade, you can play a simple game where a ball is dropped into the top of the game, from a position of your choosing. There are a number of pegs that the ball will bounce off of as it drops through the game. Whenever the ball hits a peg, it will bounce to the left with probability 0.5 and to the right with probability 0.5. The one exception to this is when it hits a peg on the far left or right side, in which case it always bounces towards the middle.<br />
When the game was first made, the pegs where arranged in a regular grid. However, it's an old game, and now some of the pegs are missing. Your goal in the game is to get the ball to fall out of the bottom of the game in a specific location. Your task is, given the arrangement of the game, to determine the optimal place to drop the ball, such that the probability of getting it to this specific location is maximized.</p>
<p>The image below shows an example of a game with five rows of five columns. Notice that the top row has five pegs, the next row has four pegs, the next five, and so on. With five columns, there are four choices to drop the ball into (indexed from 0). Note that in this example, there are three pegs missing. The top row is row 0, and the leftmost peg is column 0, so the coordinates of the missing pegs are (1,1), (2,1) and (3,2). In this example, the best place to drop the ball is on the far left, in column 0, which gives a 50% chance that it will end in the goal.</p>
<pre>x.x.x.x.x
 x...x.x
x...x.x.x
 x.x...x
x.x.x.x.x
 G  

'x' indicates a peg, '.' indicates empty space.</pre>
<h5>Input</h5>
<p>You should first read an integer <strong>N</strong>, the number of test cases. Each of the next <strong>N</strong> lines will then contain a single test case. Each test case will start with integers <strong>R</strong> and <strong>C</strong>, the number of rows and columns (<strong>R</strong> will be odd). Next, an integer <strong>K</strong> will specify the target column. Finally, an integer <strong>M</strong> will be followed by <strong>M</strong> pairs of integer <strong>r<sub>i</sub></strong> and <strong>c<sub>i</sub></strong>, giving the locations of the missing pegs.</p>
<p>Constraints</p>
<ul>
<li>1 ≤ <strong>N</strong> ≤ 100</li>
<li>3 ≤ <strong>R</strong>,<strong>C</strong> ≤ 100</li>
<li>The top and bottom rows will not have any missing pegs.</li>
<li>Other parameters will all be valid, given <strong>R</strong> and <strong>C</strong></li>
</ul>
<h5>Output</h5>
<p>For each test case, you should output an integer, the location to drop the ball into, followed by the probability that the ball will end in columns <strong>K</strong>, formatted with exactly six digits after the decimal point (round the last digit, don't truncate).</p>
<p><strong>Notes</strong></p>
<p>The input will be designed such that minor rounding errors will not impact the output (i.e. there will be no ties or near &#8212; up to 1E-9 &#8212; ties, and the direction of rounding for the output will not be impacted by small errors).</p>
<h5>Example input</h5>
<p>4<br />
5 4 0 1 2 2<br />
3 3 1 2 1 1 1 0<br />
3 4 0 2 1 0 1 1<br />
3 4 0 1 1 1</p>
<h5>Example Output</h5>
<p>0 0.375000<br />
1 1.000000<br />
0 1.000000<br />
0 0.500000</p></blockquote>
<h4>Analysis and Solution</h4>
<p>This is the hardest problem of the lot, and I also spent hours to understand the problem statement. If you are also among those who haven’t got this problem then, I would recommend you to read its explanation <a href="http://illya-keeplearning.blogspot.com/2011/01/peg-game-explanation.html">here</a>.</p>
<p>Once I understood problem statement, next hurdle was to have map this in programming paradigm. I went with a double size two-dimension array [row*2, col*2], to hold complete Input. Basically it was holding both <strong>Pegs, </strong>and <strong>Pits</strong>(blank space from where ball can pass).</p>
<p>For solving the problem I approached it from bottom, assuming that to get ball at X and probability 1. Then we move to above row and back calculate possible probabilities for every <strong>pit.</strong></p>
<p>The code to solve this problem is:</p>
<p><script src="https://gist.github.com/776348.js?file=peg_game.py"></script></p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Studious Student Problem Solution'>Facebook Hacker Cup : Studious Student Problem Solution</a></li>
<li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Double Squares Problem Solution'>Facebook Hacker Cup : Double Squares Problem Solution</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/SDNlMOpOb-4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/</feedburner:origLink></item>
		<item>
		<title>Facebook Hacker Cup : Double Squares Problem Solution</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/DZ9H3rAz2Q4/</link>
		<comments>http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 15:31:40 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[double square]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hacker cup]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=511</guid>
		<description><![CDATA[A lot of people have participated and qualified in Qualification Round of Facebook Hacker Cup. Luckily I am also qualified for First Round. Facebook Hacker Cup Qualification Round consisted 3 problems. All of these problems were programmatically easy but tricky. Lots of people made very simple mistakes while solving these problem(including me). First Problem of [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Studious Student Problem Solution'>Facebook Hacker Cup : Studious Student Problem Solution</a></li>
<li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Peg Game Problem Solution'>Facebook Hacker Cup : Peg Game Problem Solution</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img style="margin: 0px 10px 10px 0px; float: left;" src="http://i.imgur.com/ReZ6Z.jpg" alt="" /></p>
<p>A lot of people have participated and qualified in <a href="http://www.facebook.com/hackercup/problems.php?round=4">Qualification Round</a> of <a href="http://www.facebook.com/hackercup">Facebook Hacker Cup</a>. Luckily I am also qualified for <a href="http://www.facebook.com/event.php?eid=108867785836213&amp;index=1">First Round</a>. Facebook Hacker Cup Qualification Round consisted 3 problems. All of these problems were programmatically easy but tricky. Lots of people made very simple mistakes while solving these problem(including me).</p>
<p><span id="more-511"></span></p>
<p>First Problem of the round was:</p>
<h3>Double Squares</h3>
<blockquote><p>A double-square number is an integer <strong>X</strong> which can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10 = 3<sup>2</sup>+ 1<sup>2</sup>. Your task in this problem is, given <strong>X</strong>, determine the number of ways in which it can be written as the sum of two squares. For example, 10 can only be written as 3<sup>2</sup> + 1<sup>2</sup> (we don't count 1<sup>2</sup> + 3<sup>2</sup> as being different). On the other hand, 25 can be written as 5<sup>2</sup> + 0<sup>2</sup> or as 4<sup>2</sup> + 3<sup>2</sup>.</p>
<p><strong>Input</strong></p>
<p>You should first read an integer <strong>N</strong>, the number of test cases. The next <strong>N</strong> lines will contain <strong>N</strong> values of <strong>X</strong>.</p>
<h5>Constraints</h5>
<p>0 ≤ <strong>X</strong> ≤ 2147483647<br />
1 ≤ <strong>N</strong> ≤ 100</p>
<p><strong>Output</strong></p>
<p>For each value of <strong>X</strong>, you should output the number of ways to write <strong>X</strong> as the sum of two squares.</p>
<p>Example input</p>
<p>5<br />
10<br />
25<br />
3<br />
0<br />
1</p>
<p>Example output</p>
<p>1<br />
2<br />
0<br />
1<br />
1</p></blockquote>
<h4>Analysis and Solution</h4>
<p>This problem was by far the easiest of the lot. But as Facebook title for the problem said “<em>Too hard for brute force, switching to dp</em>”, this problem was almost impossible to be solved with Brute Force.</p>
<p>The strategy I used was quite simple. If we consider M is the given number, and we want to find out x, y such that:</p>
<blockquote><p>M = x^2 + y^2</p></blockquote>
<p>So basically we’ll loop from 0 to sqrt(M) and will find out D such that:</p>
<blockquote><p>D = M – i*i</p></blockquote>
<p>and if D is square root is an integer then, this number is a double square.</p>
<p>The python code of this problem is:</p>
<p><script src="https://gist.github.com/776298.js?file=double_square.py"></script></p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-studious-student-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Studious Student Problem Solution'>Facebook Hacker Cup : Studious Student Problem Solution</a></li>
<li><a href='http://www.nitinh.com/2011/01/facebook-hacker-cup-peg-game-problem-solution/' rel='bookmark' title='Permanent Link: Facebook Hacker Cup : Peg Game Problem Solution'>Facebook Hacker Cup : Peg Game Problem Solution</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/DZ9H3rAz2Q4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2011/01/facebook-hacker-cup-double-squares-problem-solution/</feedburner:origLink></item>
		<item>
		<title>RegEx to Convert String in TitleCase (Python / Django)</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/dj4h3MBb1yo/</link>
		<comments>http://www.nitinh.com/2010/11/regex-to-convert-string-in-titlecase-python-django/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 16:16:43 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[capitalization]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[title case]]></category>
		<category><![CDATA[titlecase]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=504</guid>
		<description><![CDATA[During web development I very often come across a problem to convert any string into TitleCase. Those who don't know title case is the style of writing having first letter as a majuscule (upper-case letter) and the remaining letters in minuscules (lower-case letters). So here is the python function to do that: def title&#40;value&#41;: &#34;&#34;&#34;Converts [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
<li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>During web development I very often come across a problem to convert any string into <a href="http://en.wikipedia.org/wiki/Capitalization">TitleCase</a>. Those who don't know title case is the style of writing having first letter as a majuscule (upper-case letter) and the remaining letters in minuscules (lower-case letters).</p>
<p><span id="more-504"></span></p>
<p>So here is the python function to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> title<span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Converts a string into titlecase.&quot;&quot;&quot;</span>
    t = <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;([a-z])'([A-Z])&quot;</span>, <span style="color: #ff7700;font-weight:bold;">lambda</span> m: m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, value.<span style="color: black;">title</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\d</span>([A-Z])&quot;</span>, <span style="color: #ff7700;font-weight:bold;">lambda</span> m: m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, t<span style="color: black;">&#41;</span></pre></div></div>

<p>Happy Coding</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/02/django-orm-what-to-do-when-the-memory-requirement-goes-high/' rel='bookmark' title='Permanent Link: Django ORM : What to do when the memory requirement goes high?'>Django ORM : What to do when the memory requirement goes high?</a></li>
<li><a href='http://www.nitinh.com/2009/10/implementing-search-in-django-site-using-haystack-and-xapian-whoosh/' rel='bookmark' title='Permanent Link: Implementing Search in Django Site using HayStack and Xapian / Whoosh'>Implementing Search in Django Site using HayStack and Xapian / Whoosh</a></li>
<li><a href='http://www.nitinh.com/2009/02/installing-memcached-for-django-application-on-windows-xp/' rel='bookmark' title='Permanent Link: Installing Memcached for Django Application on Windows XP'>Installing Memcached for Django Application on Windows XP</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/dj4h3MBb1yo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2010/11/regex-to-convert-string-in-titlecase-python-django/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2010/11/regex-to-convert-string-in-titlecase-python-django/</feedburner:origLink></item>
		<item>
		<title>Introducing PNRCheck.com</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/XHZcv7dWrNU/</link>
		<comments>http://www.nitinh.com/2010/06/introducing-pnrcheck-com/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 08:07:49 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[WebResource]]></category>
		<category><![CDATA[indian]]></category>
		<category><![CDATA[indian railways]]></category>
		<category><![CDATA[PNR]]></category>
		<category><![CDATA[pnr check]]></category>
		<category><![CDATA[railways]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/2010/06/introducing-pnrcheck-com/</guid>
		<description><![CDATA[I don’t know when this picture was taken, or is it actually of India or not, but condition of Indian Railways is not a lot different from this. The trains are always crowded, and it is difficult to get reservation in train. Every train traveler might faced the problem of non-availability of tickets in Indian [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2009/03/news-twitter-friendfeed-and-many-more-in-gtalk-are-chat-client-becoming-new-browsers/' rel='bookmark' title='Permanent Link: News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?'>News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.timesonline.co.uk/multimedia/archive/00589/India-train_589356a.jpg" alt="" /></p>
<p>I don’t know when this picture was taken, or is it actually of India or not, but condition of Indian Railways is not a lot different from this. The trains are always crowded, and it is difficult to get reservation in train. Every train traveler might faced the problem of non-availability of tickets in Indian Railways. Anyways, here I am not going to discuss the condition of Indian Railways.</p>
<p><span id="more-500"></span></p>
<p>In this post I want to Introduce one of the web-service which I built for my use, and I think it would be useful for you as well.</p>
<p>The name of the website is <a href="http://www.pnrcheck.com"><strong>PNRCheck.com.</strong></a> The website will help you to keep track of your <strong>waiting list / RAC </strong>tickets. The process is very simple:</p>
<ol>
<li>You need to login to the website.</li>
<li>Submit your 10 digit PNR Number for tracking, and relax.</li>
<li>We’ll send you an SMS and E-mail as and when the <strong>ticket gets confirmed</strong>. Even if the ticket doesn’t gets confirmed we’ll send you the <strong>final status of ticket</strong>(on chart preparation).</li>
</ol>
<p>By using this service you won’t have to go and check for ticket confirmation again and again, and I think this will definitely save some of your time.</p>
<h3>How does it work</h3>
<p>Its plain and simple, the service keeps checking for any update of your ticket from indianrail.gov.in. And whenever it sees that your ticket is confirmed, it shoots you an email and SMS about latest status of the ticket.</p>
<p>As this service is dependent on availability from IndianRail.gov.in, there might be some instances where, you are not able to see updated status, or the website is not allowing you  to submit new tickets. In such instances I’d request you to kindly wait for some time and try again later, otherwise you can also try checking your ticket status from <a href="http://indianrail.gov.in/">indianrail.gov.in</a>.</p>
<h4>Why PNRCheck.com?</h4>
<p>The only reason, for creating pnrcheck.com is that, such a service is very much required. This service must be provided by Indian Railways in first place. They take decent amount of service charges from us, and can’t simply send us update about our ticket.</p>
<p>Lets start using this service may be Indian Railways will get some lessons from this and may be later they start sending us these updates. So I’d request you to please tell your friends about this website, so that more and more people can use this service.</p>
<h4>PNRCheck.com Beta</h4>
<p>The website is presently is in beta, so there might be some glitches here and there. I’d request you to kindly post your comments, feedback, errors through feedback button on left side of your screen.</p>
<p>Let me here thoughts about this service through comments.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2009/03/news-twitter-friendfeed-and-many-more-in-gtalk-are-chat-client-becoming-new-browsers/' rel='bookmark' title='Permanent Link: News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?'>News, Twitter, FriendFeed and Many more in GTalk : Are Chat Client Becoming new Browsers ?</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/XHZcv7dWrNU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2010/06/introducing-pnrcheck-com/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2010/06/introducing-pnrcheck-com/</feedburner:origLink></item>
		<item>
		<title>Energy Saving Javascript</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/fBHh19o2X9w/</link>
		<comments>http://www.nitinh.com/2010/06/energy-saving-javascript/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 18:30:38 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[black]]></category>
		<category><![CDATA[black-screen]]></category>
		<category><![CDATA[Effect]]></category>
		<category><![CDATA[energy]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/?p=483</guid>
		<description><![CDATA[Title of this post may seem strange to you but this is true, You can actually save some energy by using this Javascript on your website. Let me tell you how. This javascript turns the webpage black if there is no user activity on the page for some time. As the page predominantly turn black [...]


Related posts:<ol><li><a href='http://www.nitinh.com/2010/03/beautiful-nag-screen-using-jquery-css3/' rel='bookmark' title='Permanent Link: Beautiful Nag Screen using JQuery &amp; CSS3'>Beautiful Nag Screen using JQuery &amp; CSS3</a></li>
<li><a href='http://www.nitinh.com/2009/02/wtf-what-the-framework-bookmarklet-find-the-javascript-framework-being-used/' rel='bookmark' title='Permanent Link: WTF : What the Framework bookmarklet : Find the javascript framework being used'>WTF : What the Framework bookmarklet : Find the javascript framework being used</a></li>
<li><a href='http://www.nitinh.com/2009/02/dictionary-on-double-click-bookmarklet/' rel='bookmark' title='Permanent Link: Dictionary on double click : BookMarklet'>Dictionary on double click : BookMarklet</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-485 alignleft" style="margin: 0 10px 10px 0;" title="Energy Saving Script" src="http://www.nitinh.com/wp-content/uploads/2010/06/black.jpg" alt="Energy Saving Script" width="191" height="191" />Title of this post may seem strange to you but this is true, You can actually save some energy by using this Javascript on your website. Let me tell you how.</p>
<p>This javascript turns the webpage black if there is no user activity on the page for some time. As the page predominantly turn black the script in-effect saves energy.</p>
<blockquote><p>Image displayed is primarily a function of the user's color settings and desktop graphics, as well as the color and size of open application windows; a given monitor requires more power to display a white (or light) screen than a black (or dark) screen.</p></blockquote>
<p>Roberson et al, 2002.</p>
<h2><span id="more-483"></span></h2>
<p>Just have a look at the Demo to see how it looks:</p>
<p><a class="button" href="/static/energy/">Energy Saving Javascript Demo</a></p>
<h2>How to Use</h2>
<p>Using this script is damn simple just place following script tag anywhere in the page. It would be good if you place this script just before <strong>&lt;/body&gt;</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://apps.nitinh.com/energy.js?time=100&amp;amp;credit=0&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<h3>Configuration</h3>
<p>There are two configurable parameters:</p>
<ul>
<li><strong>time</strong> : This is the amount of idle time before which the screen turn black. The time is in number of seconds and its value should be greater than 10.</li>
<li><strong>credit</strong> : This is boolean field to show / hide credits in Black Screen. Values could be 1 or 0</li>
</ul>
<p>Hope this script saves some of the valuable world energy.</p>


<p>Related posts:<ol><li><a href='http://www.nitinh.com/2010/03/beautiful-nag-screen-using-jquery-css3/' rel='bookmark' title='Permanent Link: Beautiful Nag Screen using JQuery &amp; CSS3'>Beautiful Nag Screen using JQuery &amp; CSS3</a></li>
<li><a href='http://www.nitinh.com/2009/02/wtf-what-the-framework-bookmarklet-find-the-javascript-framework-being-used/' rel='bookmark' title='Permanent Link: WTF : What the Framework bookmarklet : Find the javascript framework being used'>WTF : What the Framework bookmarklet : Find the javascript framework being used</a></li>
<li><a href='http://www.nitinh.com/2009/02/dictionary-on-double-click-bookmarklet/' rel='bookmark' title='Permanent Link: Dictionary on double click : BookMarklet'>Dictionary on double click : BookMarklet</a></li>
</ol></p><img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/fBHh19o2X9w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2010/06/energy-saving-javascript/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2010/06/energy-saving-javascript/</feedburner:origLink></item>
		<item>
		<title>WordPress Hacks : Export and Redirect Posts based on their Category</title>
		<link>http://feedproxy.google.com/~r/TheCodeDreamer/~3/_ybIjU_ujSQ/</link>
		<comments>http://www.nitinh.com/2010/03/wordpress-hacks-export-and-redirect-posts-based-on-their-category/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 16:03:06 +0000</pubDate>
		<dc:creator>nitin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[301Redirect]]></category>
		<category><![CDATA[Category]]></category>
		<category><![CDATA[Export]]></category>
		<category><![CDATA[Migration]]></category>
		<category><![CDATA[Posts]]></category>
		<category><![CDATA[Redirect]]></category>

		<guid isPermaLink="false">http://www.nitinh.com/2010/03/wordpress-hacks-export-and-redirect-posts-based-on-their-category/</guid>
		<description><![CDATA[WordPress is an excellent blogging engine. It is easy to setup and even easier to use. It has got a huge collection of themes and plugins. Because of all these things WordPress has got so widespread use. WordPress has the concept of categories to segregate posts in different types.  When number of posts in some [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.nitinh.com/wp-content/uploads/2010/03/wordpress.jpg"><img style="margin: 0px 10px 10px 0px; float: left;" title="wordpress" src="http://www.nitinh.com/wp-content/uploads/2010/03/wordpress_thumb.jpg" border="0" alt="wordpress" width="204" height="204" align="left" /></a></p>
<p><a class="zem_slink" title="WordPress" rel="homepage" href="http://wordpress.org/">WordPress</a> is an excellent blogging engine. It is easy to setup and even easier to use. It has got a huge collection of themes and plugins. Because of all these things WordPress has got so widespread use.</p>
<p>WordPress has the concept of categories to segregate posts in different types.  When number of posts in some category becomes really huge, its good to make a separate website for this particular category.</p>
<p>I faced similar problem for one of my website and I decided to migrate all of my posts in “XYZ” category to a different domain. But, unfortunately WordPress Admin Dashboard doesn’t provide any direct method to export all posts in a particular category.</p>
<p><span id="more-477"></span></p>
<p>So, to perform such migration we’ll have to go through several steps. For better understanding we can basically divide this full migration in two steps:</p>
<ol>
<li>Export Posts / Comments based on its category</li>
<li>Redirect posts of this category</li>
</ol>
<h3>Export Posts based on its category</h3>
<p>WordPress admin panel is very rich, and it does provide direct method to export posts, pages, comments, custom fields, categories, and tags. But you can only export it based on its author, there isn’t any way based on category. So, basically what I did was :</p>
<ol>
<li>Create a new user “AUTHOR_1”</li>
<li>Change author of all the posts in category “CATEGORY_XYZ” to “AUTHOR_1”</li>
<li>Export posts of “AUTHOR_1”</li>
</ol>
<p>Step 1 and 3 are quite obvious and straight, the only challenge is with Step 2. The best way I felt was to dig into the <strong>WordPress database</strong> and change author on table level itself.</p>
<h4>Database Manipulation</h4>
<p>WordPress stores all posts in a table called <em>“wp_xxxxxx_posts”. </em>This table consists a column named “<em>post_author”. </em>So basically we’ll have to change this column for our posts. To do this just run following queries on your wordpress database.</p>
<p><strong>NOTE :</strong> Before running any query on database level, I’ll recommend you to take backup of the datebase.</p>
<h5>Get TERM_ID</h5>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>  <span style="color: #ff0000;">`term_id`</span> <span style="color: #66cc66;">,</span>  <span style="color: #ff0000;">`name`</span> <span style="color: #66cc66;">,</span>  <span style="color: #ff0000;">`slug`</span>
<span style="color: #993333; font-weight: bold;">FROM</span>  <span style="color: #ff0000;">`wp_h8kac5_terms`</span>
<span style="color: #993333; font-weight: bold;">WHERE</span>  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">LIKE</span>  <span style="color: #ff0000;">'CATEGORY_XYZ'</span></pre></div></div>

<p>Here you'll have to note the "<strong>term_id</strong>" In following query put term_id taken form above query.</p>
<h5>Get TERM_TAXONOMY_ID</h5>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>  <span style="color: #ff0000;">`term_taxonomy_id`</span> <span style="color: #66cc66;">,</span>  <span style="color: #ff0000;">`term_id`</span> <span style="color: #66cc66;">,</span>  <span style="color: #ff0000;">`taxonomy`</span> <span style="color: #66cc66;">,</span>  <span style="color: #ff0000;">`count`</span>
<span style="color: #993333; font-weight: bold;">FROM</span>  <span style="color: #ff0000;">`wp_h8kac5_term_taxonomy`</span>
<span style="color: #993333; font-weight: bold;">WHERE</span>  <span style="color: #ff0000;">`term_id`</span> <span style="color: #66cc66;">=</span> %term_id%
<span style="color: #993333; font-weight: bold;">AND</span> taxonomy <span style="color: #66cc66;">=</span>  <span style="color: #ff0000;">'category'</span></pre></div></div>

<p>Here you have to note <em>"term_taxonomy_id"</em>. Secondly you can see a column <em>"count"</em>, this count should be equal to the posts returned in :</p>
<p>http://www.example.com/wp-admin/edit.php?category_name=CATEGORY_XYZ</p>
<h5>Update AUTHOR_ID in Posts Table</h5>
<p>This is the final sql query to update author. Before this you need to find the user_id of the user we just created. This can be found in table "wp_h8kac5_users".</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> wp_h8kac5_posts
<span style="color: #993333; font-weight: bold;">SET</span> post_author <span style="color: #66cc66;">=</span> %USER_ID%
<span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> object_id <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`wp_h8kac5_term_relationships`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`term_taxonomy_id`</span> <span style="color: #66cc66;">=</span> %TERM_TAXONOMY_ID% <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You need to replace %TERM_TAXONOMY_ID% from the <em>"term_taxonomy_id" </em>returned from the previous query.</p>
<h4>Export Posts</h4>
<p>Hurry !! half the battle is won. We have changed author of all the desired posts. Now we just have to log on to admin panel and export the posts of this <strong>author</strong>. This can be done from:</p>
<p>http://www.example.com/wp-admin/export.php</p>
<h4>Imports Posts on New Domain</h4>
<p>Now log on to the admin dashboard of new domain and <strong>import</strong> these posts from:</p>
<p>http://www.example.com/wp-admin/import.php</p>
<p>At at this point we have done with export part of this migration process. Now the only thing left is to redirect these posts. For redirection we’ll use .htaccess rules.</p>
<h3>Redirect posts of the category</h3>
<p>On my website I have following permalink format :</p>
<pre>/%year%/%monthnum%/%category%/%postname%/</pre>
<p>So, basically “Category” is the 3rd parameter in my URL Scheme. You need to keep similar URL Scheme on your new website. According to this URL pattern we have to write a rule in <strong><a href="http://en.wikipedia.org/wiki/Htaccess" target="_blank">.htaccess</a></strong> file.</p>
<pre># BEGIN Redirection

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/\d{4}/\d{2}/)([^/]+)(/.*)$
RewriteCond %2 ^CATEGORY_XYZ$
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [redirect=permanent,last]

# END Redirection</pre>
<p>Just put above code in starting of .htaccess, and you are all done.</p>
<p>You can test our implementation by visiting any post of “CATEGORY_XYZ”, it should be redirected to your new domain.</p>
<p>Feel free to shoot any queries through comments.</p>
<img src="http://feeds.feedburner.com/~r/TheCodeDreamer/~4/_ybIjU_ujSQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nitinh.com/2010/03/wordpress-hacks-export-and-redirect-posts-based-on-their-category/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.nitinh.com/2010/03/wordpress-hacks-export-and-redirect-posts-based-on-their-category/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 1.191 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-01-31 22:45:39 --><!-- Compression = gzip -->

