<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: codinghorror code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 13:40:43 GMT</pubDate>
    <description>DZone Snippets: codinghorror code</description>
    <item>
      <title>FizzBuzz</title>
      <link>http://snippets.dzone.com/posts/show/3720</link>
      <description>This is pretty stupid, but it's a response to the article recently Dugg here:&lt;br /&gt;http://www.codinghorror.com/blog/archives/000781.html&lt;br /&gt;&lt;br /&gt;It is about how a lot of job applicants have a hard time actually programming. My friend and I were amused by it and started going back and forth with different implementations until it got to this point.&lt;br /&gt;&lt;br /&gt;The problem is:&lt;br /&gt;&lt;br /&gt;'Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".'&lt;br /&gt;&lt;br /&gt;Now, it can be done like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;1.upto(100) { |n| puts n % 3 == 0 ? n % 5 == 0 ? "fizzbuzz" : "buzz" : n % 5 == 0 ? "fizz" : n }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;But, this is what I ended up with, which turns out actually to be faster and obviously far more flexible:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class FizzBuzz&lt;br /&gt;&lt;br /&gt;  def initialize(start_number, end_number)&lt;br /&gt;    @starting = start_number&lt;br /&gt;    @ending = end_number&lt;br /&gt;    @phrase_multiples = []&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def add_phrase_multiple(phrase, multiple)&lt;br /&gt;    @phrase_multiples &lt;&lt; [phrase, multiple]&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def print_phrases&lt;br /&gt;    fb_array = process_phrases&lt;br /&gt;    puts fb_array.collect { |e| e[1] || e[0] }.join("\n")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  private&lt;br /&gt;&lt;br /&gt;  def process_phrases&lt;br /&gt;    rarray = Array.new(@ending - @starting)&lt;br /&gt;    rarray = rarray.each_with_index { |item, i| rarray[i] = [i + @starting, item] }&lt;br /&gt;    @phrase_multiples.each { |pm| fill_multiples(rarray, pm[1], pm[0]) }&lt;br /&gt;    rarray&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def fill_multiples(fill_array, the_int, printed)&lt;br /&gt;    (the_int - (fill_array[0][0] % the_int)).step(fill_array.size - 1, the_int) do |i|&lt;br /&gt;      fill_array[i][1] = fill_array[i][1].to_s + printed.to_s&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;fb = FizzBuzz.new(1,100)&lt;br /&gt;fb.add_phrase_multiple('fizz', 3)&lt;br /&gt;fb.add_phrase_multiple('buzz', 5)&lt;br /&gt;fb.print_phrases&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Mar 2007 16:51:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3720</guid>
      <author>schacon (Scott Chacon)</author>
    </item>
  </channel>
</rss>
