<?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>Giordano Scalzo&#039;s Personal Blog &#187; RPCFN</title>
	<atom:link href="http://giordano.scalzo.biz/tag/rpcfn/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Mon, 09 May 2011 09:50:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>My solution to RPCFN 2</title>
		<link>http://giordano.scalzo.biz/2009/10/20/my-solution-to-rpcfn-2/</link>
		<comments>http://giordano.scalzo.biz/2009/10/20/my-solution-to-rpcfn-2/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 12:22:03 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[bdd]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[RPCFN]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=188</guid>
		<description><![CDATA[The solutions for RPCFN 2 are now under judgement, so it&#8217;s the time to show my effort. The problem was easy, but a little tricky: given a list of time of day, the program should find the average time. The tricky part was to manage &#8220;am&#8221; and &#8220;pm&#8221; times, mapping them in date format: given [...]]]></description>
			<content:encoded><![CDATA[<p>The solutions for <a href="http://rubylearning.com/blog/2009/10/08/rpcfn-average-arrival-time-for-a-flight-2/">RPCFN 2</a> are now under judgement, so it&#8217;s the time to show my effort.</p>
<p>The problem was easy, but a little tricky: given a list of time of day, the program should find the average time.<br />
The tricky part was to manage &#8220;am&#8221; and &#8220;pm&#8221; times, mapping them in date format: given &#8220;11:59am&#8221; and &#8220;12:00am&#8221;, then average time should be midnight.</p>
<p>My solution is based on the principle than if the range between &#8220;am&#8221; and &#8220;pm&#8221; times is under half a day, they are in the same day, otherwise a day leap occurs; this approach worked well, althought it needed a little bit of explanation in code, as <a href="http://rubylearning.com/blog/2009/10/08/rpcfn-average-arrival-time-for-a-flight-2/comment-page-1/#comment-119656">noted by Chris himself</a> <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>These are my spec:</p>
<pre class='brush: ruby'>
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'average_time_of_day'

describe 'average_time_of_day' do
	context 'for "06:00pm" and "07:00pm"' do
		it 'should be "06:30pm"' do
			avg = average_time_of_day(["06:00pm", "07:00pm"])
			avg.should == "06:30pm"
		end
	end
	context 'for "06:00pm", "07:00pm" and "08:00pm' do
		it 'should be "07:00pm"' do
			avg = average_time_of_day(["06:00pm", "07:00pm", "08:00pm"])
			avg.should == "07:00pm"
		end
	end
	context 'for "06:41am", "06:51am" and "07:01am' do
		it 'should be "06:51am"' do
			avg = average_time_of_day(["06:41am", "06:51am", "07:01am"])
			avg.should == "06:51am"
		end
	end

	context 'for "23:59pm" and "12:01am' do
		it 'should be "12:00am"' do
			avg = average_time_of_day(["23:59pm", "00:01am"])
			avg.should == "12:00am"
		end
	end

	context 'for "11:51pm", "11:56pm", "12:01am", "12:06am" and  "12:11am"' do
		it 'should be "12:01am"' do
			avg = average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])
			avg.should == "12:01am"
		end
	end

	context 'for "11:15pm", "12:03am", "11:30pm", "11:23pm", "11:48pm"' do
		it 'should be "11:35pm"' do
			avg = average_time_of_day(["11:15pm", "12:03am", "11:30pm", "11:23pm", "11:48pm"])
			avg.should == "11:35pm"
		end
	end

	context 'for "05:15am", "06:03am", "05:30am", "05:23am", "05:48am"' do
		it 'should be "05:35am"' do
			avg = average_time_of_day(["05:15am", "06:03am", "05:30am", "05:23am", "05:48am"])
			avg.should == "05:35am"
		end
	end

	context 'for "11:15am", "12:03pm", "11:30am", "11:23am", "11:48am"' do
		it 'should be "11:35am"' do
			avg = average_time_of_day(["11:15am", "12:03pm", "11:30am", "11:23am", "11:48am"])
			avg.should == "11:35am"
		end
	end

	context 'for "6:00pm" and "6:00am' do
		it 'should be "12:00am"' do
			avg = average_time_of_day(["6:00pm", "6:00am"])
			avg.should == "12:00am"
		end
	end

	context 'for "12:01am", "11:51pm", "11:56pm",  "12:06am" and  "12:11am"' do
		it 'should be "12:01am"' do
			avg = average_time_of_day(["12:01am", "11:51pm", "11:56pm", "12:06am", "12:11am"])
			avg.should == "12:01am"
		end
	end

end
</pre>
<p>and this are my code:</p>
<pre class='brush: ruby'>
require 'time'

private
class Array
	def to_sec
		map {|t|t.to_f}
	end

	def avg
		(size > 0) ? to_sec.inject(0.0){|sum,el| sum + el}/size : 0

	end
end

class Time
	def am?
		hour < 12
	end

	def pm?
		not am?
	end

	def to_s
		strftime("%I:%M%p").downcase
	end

end

DAY_IN_SEC = 24*60*60

def adjust_order_for(times)
	to_adjust?(times) ? adjusted(times) : times
end

def to_adjust?(times)
	avg_am = times.select { |t| t.am? }.avg
	avg_pm = times.select { |t| t.pm? }.avg
	avg_pm - avg_am  >= DAY_IN_SEC/2
end

def adjusted(times)
	times.map { |t|(t.am? ? t+DAY_IN_SEC : t) }
end

public
def average_time_of_day(times)
	times = adjust_order_for(times.map { |t| Time.parse(t) })

	Time.at(times.avg).to_s
end
</pre>
<p>Next Quiz is scheduled for 1st Nov. 2009, and will be provided by <a href="http://gautamrege.wordpress.com/">Gautam Rege</a>.</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/RPCFN' rel='tag' target='_self'>RPCFN</a>, <a class='technorati-link' href='http://technorati.com/tag/ruby' rel='tag' target='_self'>ruby</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/10/20/my-solution-to-rpcfn-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Entering RPCFN #2</title>
		<link>http://giordano.scalzo.biz/2009/10/10/entering-rpcfn-2/</link>
		<comments>http://giordano.scalzo.biz/2009/10/10/entering-rpcfn-2/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 17:12:59 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[RPCFN]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=176</guid>
		<description><![CDATA[Quite satisfied for my result in first quiz, I&#8217;m enjoying writing some code for the second Ruby Programming Contest For Newbie, &#8220;Average arrival time for a flight&#8221;. Despite it looks more easy than the previous, it needs some tricks and exploiting some Ruby idioms to resolve it effectively. I&#8217;ll comment my entry when the contest [...]]]></description>
			<content:encoded><![CDATA[<p>Quite satisfied for my result in first quiz, I&#8217;m enjoying writing some code for the <a href="http://rubylearning.com/blog/2009/10/08/rpcfn-average-arrival-time-for-a-flight-2/">second Ruby Programming Contest For Newbie</a>, &#8220;Average arrival time for a flight&#8221;.</p>
<p>Despite it looks more easy than the previous, it needs some tricks and exploiting some Ruby idioms to resolve it effectively.</p>
<p>I&#8217;ll comment my entry when the contest is over.</p>
<p>Thanks to <a href="http://">Chris Strom</a> and <a href="http://satishtalim.com/">Satish Talim</a> for this challenge!</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/RPCFN' rel='tag' target='_self'>RPCFN</a>, <a class='technorati-link' href='http://technorati.com/tag/ruby' rel='tag' target='_self'>ruby</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/10/10/entering-rpcfn-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby Programming Challenge For Newbies: a chance to improve Ruby knowledge</title>
		<link>http://giordano.scalzo.biz/2009/10/05/ruby-programming-challenge-for-newbies-a-chance-to-improve-ruby-knowledge/</link>
		<comments>http://giordano.scalzo.biz/2009/10/05/ruby-programming-challenge-for-newbies-a-chance-to-improve-ruby-knowledge/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 09:05:01 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[RPCFN]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=157</guid>
		<description><![CDATA[Some day ago, I stumbled upon on a post by Satish Talim in his useful Ruby Learning Blog, announcing a Ruby Quiz for Newbies: what a wonderful opportunity to learn better Ruby and Rspec! The first challenge was proposed by a Fabio Akita, a well know Ruby and Rails evangelist, and it&#8217;s about a script [...]]]></description>
			<content:encoded><![CDATA[<p>Some day ago, I stumbled upon on a post by <a href="http://satishtalim.com/">Satish Talim</a> in his useful <a href="http://rubylearning.com/blog/">Ruby Learning Blog</a>, announcing a <a href="http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/">Ruby Quiz for Newbies</a>:<br />
what a wonderful opportunity to learn better Ruby and Rspec!</p>
<p>The first challenge was proposed by a <a href="http://akitaonrails.com/">Fabio Akita</a>, a well know Ruby and Rails evangelist, and it&#8217;s about a script that add, or subtract, an amount of time from a subtitles file.</p>
<p>I discovered the challenge a bit late, so I&#8217;d to rush to complete it in time, but I&#8217;m quite satisfied with the result; I don&#8217;t like the <em>IF</em> in main method, and it&#8217;s lack a robust correctness checking of command line&#8217;s arguments: I&#8217;ll try to remove explicit conditions and clean more in next challenge.</p>
<p>I pushed my attempt to <em>GitHub</em>, <a href="http://github.com/gscalzo/ShiftSubtitle">http://github.com/gscalzo/ShiftSubtitle</a>, so I can improved it, after seeing other partecipants&#8217; entries.</p>
<p>I have to credit solution to check <code>System.exit</code> to <a href="http://www.deploymentzone.com/2009/09/26/ruby-mocking-kernel-exit/">Charles Feduke</a>: Thanx Charles!<br />
Next quiz is scheduled for 9th Oct. 2009 proposed by <a href="http://japhr.blogspot.com/">Chris Strom</a>: I really looking forward to it!</p>
<p><strong>Update</strong><br />
<a href="http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/comment-page-1/#comment-119475">I have to buy a mac</a>!  <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/challenge' rel='tag' target='_self'>challenge</a>, <a class='technorati-link' href='http://technorati.com/tag/RPCFN' rel='tag' target='_self'>RPCFN</a>, <a class='technorati-link' href='http://technorati.com/tag/ruby' rel='tag' target='_self'>ruby</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/10/05/ruby-programming-challenge-for-newbies-a-chance-to-improve-ruby-knowledge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

