<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://srtsolutions.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Lazy Evaluation</title><link>http://srtsolutions.com/blogs/marinafedner/default.aspx</link><description>Marina&amp;#39;s coding (mis)adventures</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>How should I spend my free book money?</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/08/14/how-should-i-spend-my-free-book-money.aspx</link><pubDate>Thu, 14 Aug 2008 22:41:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:4258</guid><dc:creator>mfedner</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=4258</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/08/14/how-should-i-spend-my-free-book-money.aspx#comments</comments><description>&lt;p&gt;Last week, I went to Eric Ivancich&amp;#39;s awesome &lt;a href="http://www.computersociety.org/portal/"&gt;AACS&lt;/a&gt; talk on Using Ruby to Create Domain-Specific Languages here at the SRT Solutions office.&amp;nbsp; I&amp;#39;d been lazy before, but that day I decided to finally pay my $20 AACS dues and become an official member.&amp;nbsp;&amp;nbsp; As more validation for the fact that I&amp;#39;m the luckiest girl in the world*, I ended up more than recouping my loss when I won the Borders gift card in the members-only drawing at the end of the night!&amp;nbsp; Now I&amp;#39;m faced with a serious dilemma... what to spend it on, from my mile-long Books To Read list?&amp;nbsp; My educational options:&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;A new ruby book, maybe &lt;a href="http://oreilly.com/catalog/9780976694076/"&gt;The Best of Ruby Quiz&lt;/a&gt;?&amp;nbsp; Inspiration after learning more about ruby&amp;#39;s metaprogramming features&lt;/p&gt;  &lt;p&gt;or:&lt;/p&gt;  &lt;p&gt;The software engineering book everyone&amp;#39;s been talking about: &lt;a href="http://oreilly.com/catalog/9780596510046/"&gt;Beautiful Code&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;I&amp;#39;m earmarking this hard-earned money for tech, and making myself stay away from the fiction section (&lt;a href="http://www.borders.com/online/store/TitleDetail?sku=0307278735"&gt;mmm&lt;/a&gt;, very tempting).&amp;nbsp; What does everyone think?&amp;nbsp; I love reading recommendations.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;* Other evidence: I get to work with not just one, but &lt;a href="http://srtsolutions.com/blogs/billwagner/"&gt;three&lt;/a&gt; &lt;a href="http://srtsolutions.com/blogs/patricksteele/default.aspx"&gt;Microsoft&lt;/a&gt; &lt;a href="http://srtsolutions.com/blogs/jaywren/"&gt;MVPs&lt;/a&gt;!&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=4258" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/books/default.aspx">books</category></item><item><title>euler problem #5</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/05/01/euler-problem-5.aspx</link><pubDate>Fri, 02 May 2008 01:40:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:3050</guid><dc:creator>mfedner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=3050</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/05/01/euler-problem-5.aspx#comments</comments><description>&lt;p&gt;&lt;i&gt;&amp;nbsp;What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?&lt;/i&gt;&lt;/p&gt;&lt;p&gt;----&lt;/p&gt;&lt;p&gt;To solve this problem easily, we need to remember that the smallest number that is evenly divisible by two numbers is their least common multiple.&amp;nbsp; Armed with this knowledge (and the fact that ruby already has a least common multiple function!), we can write:&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;numbers=(1..20).to_a&lt;br /&gt;puts numbers.inject{ |x,n| x.lcm(n) } 
&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;So for those of you at SRT (or elsewhere) that still harbor any doubts about ruby, can you beat that?!&amp;nbsp; Fine, ok, so you might say that using a built-in lcm function is kind of lame.&amp;nbsp; So as a bonus, here&amp;#39;s how I implemented lcm myself in a cute way using the greatest common divisor:&lt;/p&gt;

&lt;div id="content"&gt;
&lt;p&gt;def gcd(a,b)&lt;br /&gt;&amp;nbsp; if b==0 : a&lt;br /&gt;&amp;nbsp; else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; gcd(b, a % b )&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def lcm(a,b)&lt;br /&gt;&amp;nbsp; (a*b)/gcd(a,b)&lt;br /&gt;end &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=3050" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/euler/default.aspx">euler</category><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/ruby/default.aspx">ruby</category></item><item><title>Geeky gear</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/12/geeky-gear.aspx</link><pubDate>Sun, 13 Apr 2008 03:51:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2760</guid><dc:creator>mfedner</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=2760</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/12/geeky-gear.aspx#comments</comments><description>&lt;p&gt;A while ago, we had a discussion here at the office about nerdy t-shirts.&amp;nbsp; We all know what I&amp;#39;m talking about: usually worn by a male geek/programmer with a neckbeard, a black shirt with a &amp;quot;witty&amp;quot; technical reference.&amp;nbsp; Opinions about these shirts vary among SRT employees: some of us find them funny and even wear them on dates [no report on the results, though], some are skeptical.&amp;nbsp; You might guess that I fall into the second camp.&amp;nbsp; Here are the worst offenders in my book:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://www.thinkgeek.com/images/products/front/geek_love_poem.jpg" alt="geek love poem" align="" border="" height="286" hspace="" width="220" /&gt;&lt;/p&gt;&lt;p&gt;The geek love poem...&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://www.thinkgeek.com/images/products/front/noplace.jpg" alt="No place like home" align="middle" border="" height="286" hspace="" width="220" /&gt; &lt;/p&gt;&lt;p&gt;&amp;nbsp;It&amp;#39;s so bad it&amp;#39;s good. &lt;/p&gt;&lt;p&gt;&lt;img src="http://www.thinkgeek.com/images/products/front/binary-people.jpg" alt="2 types of people" align="middle" border="" height="283" hspace="" width="220" /&gt;&lt;/p&gt;&lt;p&gt;The worst one of all.&amp;nbsp; Nearly ubiquitous in computer science buildings on campuses nationwide, but yet still considered brilliant and unique by the wearer.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;And of course, I can&amp;#39;t leave out geeky women&amp;#39;s gear -- in some ways even more horrifying than the men&amp;#39;s stuff.&amp;nbsp; Evidence presented:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://image0.etsy.com/il_430xN.8181328.jpg" alt="html earrings" align="middle" border="" height="346" hspace="" width="430" /&gt; &lt;/p&gt;&lt;p&gt;HTML tags -- get it?! &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://www.thinkgeek.com/images/products/front/httppanties_4up.jpg" alt="HTTPanties" align="middle" border="" height="363" hspace="" width="250" /&gt;&lt;/p&gt;&lt;p&gt;I have no words.&amp;nbsp; Instead I&amp;#39;ll leave you with ThinkGeek&amp;#39;s description of these: &amp;quot;... we bring you &lt;b&gt;HTTPanties&lt;/b&gt;
for the discriminating woman who would prefer a web-savvy and somewhat-direct 
approach in the romance department.&amp;nbsp; Feeling frisky? Well then don the black &amp;quot;200 OK&amp;quot; panties and see where they take 
you. Alternatively, the white &amp;quot;403 Forbidden&amp;quot; style sends a very different 
and hopefully clear message. We think &amp;quot;411 Length Required&amp;quot; and &amp;quot;413 Requested Entity Too Large&amp;quot; are pretty self-explanatory.&amp;quot;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2760" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/nerd/default.aspx">nerd</category></item><item><title>Euler Problem 4</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/08/euler-problem-4.aspx</link><pubDate>Wed, 09 Apr 2008 00:35:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2699</guid><dc:creator>mfedner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=2699</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/08/euler-problem-4.aspx#comments</comments><description>&lt;p&gt;&lt;i&gt;&amp;nbsp;Find the largest palindrome made from the product of two 3-digit numbers.&lt;/i&gt;&lt;/p&gt;&lt;p&gt;-----&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;First, I defined a palindrome recursively: a number is one if its first and last digits are the same, and if the inside is also a palindrome.&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;def palindrome?(digitArr)&lt;br /&gt;&amp;nbsp; if digitArr.empty?&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; true&lt;br /&gt;&amp;nbsp; elsif digitArr.length == 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; true &lt;br /&gt;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (digitArr.first == digitArr.last &amp;amp;&amp;amp; palindrome?(digitArr[1..-2]))&amp;nbsp; &lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;Note that in Ruby, methods can end with a question mark, a convention that really helps readability.&amp;nbsp; Also note that the &amp;quot;else if&amp;quot; keyword is &lt;b&gt;elsif&lt;/b&gt;, which is weird and unhelpful to me.&amp;nbsp; I want that 20 minutes of staring at my code like &amp;quot;wtf?!&amp;quot; (before figuring that out) back.&lt;/p&gt;&lt;p&gt;&amp;nbsp;Then we do a search of all the multiples of two three-digit numbers until we find the biggest palindrome:&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;def search(i,j)&lt;br /&gt;&amp;nbsp; maxPal=0&lt;br /&gt;&amp;nbsp; while i &amp;gt; 99 do&lt;br /&gt;&amp;nbsp;&amp;nbsp; j=999&lt;br /&gt;&amp;nbsp;&amp;nbsp; while j &amp;gt; 99 do&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if palindrome?((i*j).to_s.split(&amp;quot;&amp;quot;)) then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(i*j)&amp;gt; maxPal then maxPal = i*j&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp; j-=1&lt;br /&gt;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; i-=1&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; return maxPal&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts search(999,999)&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;There&amp;#39;s probably a prettier way to do this.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2699" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/euler/default.aspx">euler</category><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/ruby/default.aspx">ruby</category></item><item><title>Euler Problem 3</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/08/euler-problem-3.aspx</link><pubDate>Wed, 09 Apr 2008 00:07:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2698</guid><dc:creator>mfedner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=2698</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/08/euler-problem-3.aspx#comments</comments><description>&lt;p&gt;&lt;i&gt;&amp;nbsp;What is the largest prime factor of the number 600851475143 ?&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;-----&lt;/p&gt;&lt;p&gt;&amp;nbsp;I wrote this piece of disgusting, un-Rubylike Ruby to get the answer by creating a list of a number&amp;#39;s prime factors:&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;&amp;nbsp;def factor(composite)&lt;br /&gt;&amp;nbsp; i=1&lt;br /&gt;&amp;nbsp; flag=false&lt;br /&gt;&amp;nbsp; primefactors =[]&lt;br /&gt;&amp;nbsp; while (i &amp;lt; composite)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i=i+1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; primefactors.each{|p| flag=true if (i%p).zero?}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if flag then flag=false &amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else # if it is a prime&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; primefactors.push(i)&amp;nbsp;&amp;nbsp; &amp;nbsp;#add the prime (since it&amp;#39;s a prime factor)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while (composite%i).zero?&amp;nbsp;&amp;nbsp; # as long as n is divisbible by the prime&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; composite /=&amp;nbsp; i&amp;nbsp;&amp;nbsp;&amp;nbsp; # divide n by the prime&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; primefactors&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts factor(600851475143).last&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;And then I learned that ruby has a prime number generator.&amp;nbsp; All I need to do is:&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;require &amp;#39;mathn&amp;#39;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;n = 600851475143&lt;br /&gt;factor = 0&lt;br /&gt;primes = Prime.new&lt;br /&gt;while n &amp;gt; 1&lt;br /&gt;&amp;nbsp; factor = primes.next&lt;br /&gt;&amp;nbsp; while (n % factor).zero? &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n /= factor &lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts factor&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;I am slightly ashamed.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2698" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/euler/default.aspx">euler</category><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/ruby/default.aspx">ruby</category><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/incompetence/default.aspx">incompetence</category></item><item><title>Project Euler: Problem 2</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/06/project-euler-problem-2.aspx</link><pubDate>Sun, 06 Apr 2008 23:55:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2669</guid><dc:creator>mfedner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=2669</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/06/project-euler-problem-2.aspx#comments</comments><description>&lt;p&gt;The problem:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Find the sum of all the even-valued terms in the [Fibonacci] sequence which do not exceed four million.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my fibonacci function:&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;def fib(limit =nil)&lt;br /&gt;&amp;nbsp; f1 = 0&lt;br /&gt;&amp;nbsp; f2 = 1&lt;br /&gt;&amp;nbsp; while (not limit or f2 &amp;lt;= limit)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield f2&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f1,f2 = f2, f1+f2&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;And here&amp;#39;s how I use it to get the answer:&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;evens=[]&lt;br /&gt;fib { |x| if x%2==0 : evens.push(x) end ; if x&amp;gt;4000000: break end}&lt;br /&gt;puts evens.inject {|sum, n| sum+n} &lt;/p&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;I learned about the awesomeness of &amp;#39;yield&amp;#39; by doing this problem, so it was totally worth it.&amp;nbsp; yield let me calculate as many fibonacci terms as I needed, but not more, and without having to calculate any term more than once.&amp;nbsp;&amp;nbsp;  &lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2669" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/euler/default.aspx">euler</category><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/ruby/default.aspx">ruby</category></item><item><title>Project Euler in Ruby</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/06/project-euler-in-ruby.aspx</link><pubDate>Sun, 06 Apr 2008 17:31:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2666</guid><dc:creator>mfedner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=2666</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/04/06/project-euler-in-ruby.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m taking Bill Wagner up on his &lt;a href="http://srtsolutions.com/blogs/billwagner/archive/2008/03/25/project-euler-problem-1.aspx" title="project euler" target="_blank"&gt;Project Euler challenge&lt;/a&gt;, but in Ruby.&amp;nbsp; I&amp;#39;ve secretly wanted to learn Ruby for a long time, and this looks like the perfect chance.&amp;nbsp; So if my code makes your eyes bleed, it&amp;#39;s cause I&amp;#39;m a total n00b.&amp;nbsp; Without further ado (because the excitement was becoming unbearable... I agree, Ruby &lt;span style="font-style:italic;"&gt;is&lt;/span&gt; sexy!), here&amp;#39;s my code that prints the answer to Problem 1:&lt;/p&gt;


&lt;p&gt;&lt;i&gt;If we list all the natural numbers below 10 that are multiples of 3
or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&amp;nbsp; Find the sum of all the multiples of 3 or 5 below 1000.&lt;/i&gt;&lt;/p&gt;

&lt;div id="content"&gt;&lt;p&gt;answer = (0..999).&lt;b&gt;select&lt;/b&gt; { |a| a%3 ==0 || a%5==0 }&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;puts&lt;/span&gt; answer.&lt;span style="font-weight:bold;"&gt;inject&lt;/span&gt; { |sum, n| sum+n }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;I love the built-in ruby function &lt;b&gt;inject&lt;/b&gt;, which is like foldl in some functional languages.&amp;nbsp; (See a cute demo &lt;a href="http://foldl.com/" title="Foldl"&gt;here&lt;/a&gt;.) &amp;nbsp; Overall the code is very similar to Bill&amp;#39;s C# version.&amp;nbsp; I wonder if that&amp;#39;ll still be the case as we get to the more complex problems.&lt;/p&gt;&lt;p&gt;&amp;nbsp;[4/08/08: correction of a typo in the code] &lt;br /&gt;&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2666" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/euler/default.aspx">euler</category><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/ruby/default.aspx">ruby</category></item><item><title>The Google Data query interface</title><link>http://srtsolutions.com/blogs/marinafedner/archive/2008/03/29/the-google-data-query-interface.aspx</link><pubDate>Sun, 30 Mar 2008 00:01:00 GMT</pubDate><guid isPermaLink="false">727bb5a1-3d8b-4cbc-a411-ac1a71136f7d:2595</guid><dc:creator>mfedner</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://srtsolutions.com/blogs/marinafedner/rsscomments.aspx?PostID=2595</wfw:commentRss><comments>http://srtsolutions.com/blogs/marinafedner/archive/2008/03/29/the-google-data-query-interface.aspx#comments</comments><description>&lt;p&gt;I know I&amp;#39;m not the only one with tons of my data stored somewhere on Google&amp;#39;s servers these days (frightening, I know.)&amp;nbsp; Luckily, there&amp;#39;s the Google Data API, which lets us access and modify our Google data programmatically, should we decide to, say, display it on our websites or build custom applications to process it. &lt;/p&gt;&lt;p&gt;The API uses HTTP requests to transfer data, building on top of RSS and Atom the ability to send a query and receive a feed of matching results.&amp;nbsp; This is cool, because it means we can access our data easily, just by creating the appropriate HTTP query.&amp;nbsp; Here&amp;#39;s what I mean:&lt;/p&gt;&lt;p&gt;This is the XML feed of our SRT Events Google Calendar:&amp;nbsp; http://www.google.com/calendar/feeds/v2433p4md2226qtd3ja1annifc%40group.calendar.google.com/public/basic&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;We can add query parameters to the end of this URL:&amp;nbsp; http://www.google.com/calendar/feeds/v2433p4md2226qtd3ja1annifc%40group.calendar.google.com/public/basic?max-results=10&amp;amp;orderby=starttime&lt;/p&gt;&lt;p&gt;That new-and-improved feed is the full calendar feed of events, filtered to leave only 10 results and ordered by the start date of the event.&amp;nbsp; Note that in addition to a standard set of parameters for all
Google Data, there are some special ones defined for each app, like
&amp;quot;racy&amp;quot; in YouTube.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;/p&gt;&lt;p&gt;Here&amp;#39;s the metadata for the most-viewed YouTube videos:&lt;/p&gt;&lt;p&gt;http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed&lt;/p&gt;&lt;p&gt;and adding these query parameters gives us just the family-friendly ones, ordered by rating.&lt;/p&gt;&lt;p&gt;http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?orderby=rating&amp;amp;racy=exclude&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Now that we&amp;#39;ve got these custom feeds, we can just use any standard libraries to parse or display them. Pretty cool.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;img src="http://srtsolutions.com/aggbug.aspx?PostID=2595" width="1" height="1"&gt;</description><category domain="http://srtsolutions.com/blogs/marinafedner/archive/tags/google/default.aspx">google</category></item></channel></rss>