<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>The Ruby Programming Blog</title>
	<atom:link href="http://rishabhdua.com/ruby-prog/feed/" rel="self" type="application/rss+xml" />
	<link>http://rishabhdua.com/ruby-prog</link>
	<description>all the algorithms revisited in ruby !</description>
	<lastBuildDate>Tue, 20 Sep 2011 17:58:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>facebook careers puzzle &#8211; Hoppity Hop! &#8211; in Ruby</title>
		<link>http://rishabhdua.com/ruby-prog/09/facebook-careers-puzzle-hoppity-hop-in-ruby/</link>
		<comments>http://rishabhdua.com/ruby-prog/09/facebook-careers-puzzle-hoppity-hop-in-ruby/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 17:54:23 +0000</pubDate>
		<dc:creator>rishabh</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Facebook Careers Puzzles]]></category>
		<category><![CDATA[facebook careers puzzle]]></category>
		<category><![CDATA[Facebook Puzzles]]></category>
		<category><![CDATA[Ruby Program]]></category>

		<guid isPermaLink="false">http://rishabhdua.com/ruby-prog/?p=31</guid>
		<description><![CDATA[https://www.facebook.com/careers/puzzles.php?puzzle_id=7 A pretty simple puzzle just to get things started. Hoppity Hop! View Code RUBYdef hoppity_hop #fetch the file name from the command prompt input file_name = ARGV&#91;0&#93; #read the contents from the file and convert to integer file_content = File.read&#40;file_name&#41;.to_i &#8230; <a href="http://rishabhdua.com/ruby-prog/09/facebook-careers-puzzle-hoppity-hop-in-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a title="Facebook Puzzle Hoppity Hop ! in Ruby Solution" href="https://www.facebook.com/careers/puzzles.php?puzzle_id=7" target="_blank">https://www.facebook.com/careers/puzzles.php?puzzle_id=7</a></p>
<p>A pretty simple puzzle just to get things started. Hoppity Hop!</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code2'); return false;">View Code</a> RUBY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p312"><td class="code" id="p31code2"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> hoppity_hop
	<span style="color:#008000; font-style:italic;">#fetch the file name from the command prompt input</span>
	file_name = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
	<span style="color:#008000; font-style:italic;">#read the contents from the file and convert to integer</span>
	file_content = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>file_name<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_i</span>
&nbsp;
	<span style="color:#008000; font-style:italic;">#run loop from 1 to the number in file</span>
	<span style="color:#006666;">1</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>file_content<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
		<span style="color:#9966CC; font-weight:bold;">if</span> i <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">3</span> == <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&amp;</span>amp;<span style="color:#006600; font-weight:bold;">&amp;</span>amp; i <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">5</span> == <span style="color:#006666;">0</span>
			<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hop<span style="color:#000099;">\n</span>&quot;</span>
		<span style="color:#9966CC; font-weight:bold;">else</span>
			<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hoppity<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> i <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">3</span> == <span style="color:#006666;">0</span>
			<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hophop<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> i <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">5</span> == <span style="color:#006666;">0</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
hoppity_hop</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://rishabhdua.com/ruby-prog/09/facebook-careers-puzzle-hoppity-hop-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Search &#8211; Ruby Programming Language</title>
		<link>http://rishabhdua.com/ruby-prog/08/binary-search-ruby-programming-language/</link>
		<comments>http://rishabhdua.com/ruby-prog/08/binary-search-ruby-programming-language/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 17:56:20 +0000</pubDate>
		<dc:creator>rishabh</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Ruby Algorithms]]></category>

		<guid isPermaLink="false">http://rishabhdua.com/ruby-prog/?p=22</guid>
		<description><![CDATA[Binary Search, takes in sorted array and search the key element and returns the index of the key element. View Code RUBYdef binarySearch&#40;sortedArray, first, last, key&#41; until first &#62; last do mid = &#40;first + last&#41;/2 if &#40;key &#62; sortedArray&#91;mid&#93;&#41; &#8230; <a href="http://rishabhdua.com/ruby-prog/08/binary-search-ruby-programming-language/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Binary Search, takes in sorted array and search the key element and returns the index of the key element.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code5'); return false;">View Code</a> RUBY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p225"><td class="code" id="p22code5"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sortedArray, first, last, key<span style="color:#006600; font-weight:bold;">&#41;</span> 
   <span style="color:#9966CC; font-weight:bold;">until</span> first <span style="color:#006600; font-weight:bold;">&gt;</span> last <span style="color:#9966CC; font-weight:bold;">do</span>
       mid = <span style="color:#006600; font-weight:bold;">&#40;</span>first <span style="color:#006600; font-weight:bold;">+</span> last<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2</span>
       <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>key <span style="color:#006600; font-weight:bold;">&gt;</span> sortedArray<span style="color:#006600; font-weight:bold;">&#91;</span>mid<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
           first = mid <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>;
           binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sortedArray, first, last, key<span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#006600; font-weight:bold;">&#40;</span>key <span style="color:#006600; font-weight:bold;">&lt;</span> sortedArray<span style="color:#006600; font-weight:bold;">&#91;</span>mid<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
           last = mid <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>;
           binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sortedArray, first, last, key<span style="color:#006600; font-weight:bold;">&#41;</span>
       <span style="color:#9966CC; font-weight:bold;">else</span>
           <span style="color:#0000FF; font-weight:bold;">return</span> mid;
      <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
sorted_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">6</span>, <span style="color:#006666;">18</span>, <span style="color:#006666;">19</span>, <span style="color:#006666;">25</span>, <span style="color:#006666;">46</span>, <span style="color:#006666;">78</span>, <span style="color:#006666;">102</span>, <span style="color:#006666;">114</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sorted_array, <span style="color:#006666;">0</span>, sorted_array.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Another attempt, optimized binary search without while or any loop. There is no loop. Let me know your view point on this</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code6'); return false;">View Code</a> RUBY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p226"><td class="code" id="p22code6"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sortedArray, first, last, key<span style="color:#006600; font-weight:bold;">&#41;</span> 
   <span style="color:#9966CC; font-weight:bold;">if</span> first <span style="color:#006600; font-weight:bold;">&lt;</span> last
       mid = <span style="color:#006600; font-weight:bold;">&#40;</span>first <span style="color:#006600; font-weight:bold;">+</span> last<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2</span>
       <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>key == sortedArray<span style="color:#006600; font-weight:bold;">&#91;</span>mid<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0000FF; font-weight:bold;">return</span> mid
       <span style="color:#9966CC; font-weight:bold;">else</span>
         <span style="color:#9966CC; font-weight:bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>key <span style="color:#006600; font-weight:bold;">&gt;</span> sortedArray<span style="color:#006600; font-weight:bold;">&#91;</span>mid<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
           first = mid <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
         <span style="color:#9966CC; font-weight:bold;">else</span>
           last = mid <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span>
         <span style="color:#9966CC; font-weight:bold;">end</span>
       <span style="color:#9966CC; font-weight:bold;">end</span>
       <span style="color:#0000FF; font-weight:bold;">return</span> binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sortedArray, first, last, key<span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
sorted_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">6</span>, <span style="color:#006666;">18</span>, <span style="color:#006666;">19</span>, <span style="color:#006666;">25</span>, <span style="color:#006666;">46</span>, <span style="color:#006666;">78</span>, <span style="color:#006666;">102</span>, <span style="color:#006666;">114</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC0066; font-weight:bold;">p</span> binarySearch<span style="color:#006600; font-weight:bold;">&#40;</span>sorted_array, <span style="color:#006666;">0</span>, sorted_array.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>, <span style="color:#006666;">50</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://rishabhdua.com/ruby-prog/08/binary-search-ruby-programming-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Astrix (*) Patterns in Ruby</title>
		<link>http://rishabhdua.com/ruby-prog/08/hello-world/</link>
		<comments>http://rishabhdua.com/ruby-prog/08/hello-world/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 10:21:15 +0000</pubDate>
		<dc:creator>rishabh</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Just a simple program]]></category>
		<category><![CDATA[Astrix Patterns]]></category>
		<category><![CDATA[Diamond patterns]]></category>
		<category><![CDATA[Ruby Algorithms]]></category>
		<category><![CDATA[Ruby Program]]></category>

		<guid isPermaLink="false">http://rishabhdua.com/ruby-prog/?p=1</guid>
		<description><![CDATA[This simple program shows the astrix in diamond structure. It takes input from the user and displays a diamond structure astrix. This program is made using Ruby Language. I used Ruby 1.8.7 but it should work on all Ruby versions. &#8230; <a href="http://rishabhdua.com/ruby-prog/08/hello-world/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This simple program shows the astrix in diamond structure. It takes input from the user and displays a diamond structure astrix. This program is made using Ruby Language. I used Ruby 1.8.7 but it should work on all Ruby versions.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1code8'); return false;">View Code</a> RUBY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18"><td class="code" id="p1code8"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> patterns n
	<span style="color:#006666;">1</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
		space = <span style="color:#996600;">''</span>
		astrix = <span style="color:#996600;">''</span>
		i.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> space=space<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">' '</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>
		<span style="color:#006666;">1</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> astrix=astrix<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">'*'</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>
		<span style="color:#006666;">2</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> astrix=astrix<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">'*'</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>
		<span style="color:#CC0066; font-weight:bold;">puts</span> space<span style="color:#006600; font-weight:bold;">+</span>astrix
	<span style="color:#9966CC; font-weight:bold;">end</span>
	i= n<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
	<span style="color:#9966CC; font-weight:bold;">until</span> i <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">do</span>
		space = <span style="color:#996600;">''</span>
		astrix = <span style="color:#996600;">''</span>
		i.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> space=space<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">' '</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>
		<span style="color:#006666;">1</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> astrix=astrix<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">'*'</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>
		<span style="color:#006666;">2</span>.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> astrix=astrix<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">'*'</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>
		<span style="color:#CC0066; font-weight:bold;">puts</span> space<span style="color:#006600; font-weight:bold;">+</span>astrix
		i=i<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Enter Number of Iterations&quot;</span>
a = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#9900CC;">to_i</span>
&nbsp;
patterns a</pre></td></tr></table></div>

<p>Output:</p>
<p><a href="http://rishabhdua.com/ruby-prog/wp-content/uploads/2011/08/1_pattern.jpg"><img class="alignnone size-full wp-image-19" title="1_pattern" src="http://rishabhdua.com/ruby-prog/wp-content/uploads/2011/08/1_pattern.jpg" alt="Ruby Program for Astrix Pattern " width="222" height="128" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://rishabhdua.com/ruby-prog/08/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

