DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

FizzBuzz

Snippets Manager user avatar by
Snippets Manager
·
Mar. 23, 07 · · Code Snippet
Like (0)
Save
Tweet
2.02K Views

Join the DZone community and get the full member experience.

Join For Free
This is pretty stupid, but it's a response to the article recently Dugg here:
http://www.codinghorror.com/blog/archives/000781.html

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.

The problem is:

'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".'

Now, it can be done like this:

1.upto(100) { |n| puts n % 3 == 0 ? n % 5 == 0 ? "fizzbuzz" : "buzz" : n % 5 == 0 ? "fizz" : n }


But, this is what I ended up with, which turns out actually to be faster and obviously far more flexible:


class FizzBuzz

  def initialize(start_number, end_number)
    @starting = start_number
    @ending = end_number
    @phrase_multiples = []
  end

  def add_phrase_multiple(phrase, multiple)
    @phrase_multiples << [phrase, multiple]
  end

  def print_phrases
    fb_array = process_phrases
    puts fb_array.collect { |e| e[1] || e[0] }.join("\n")
  end

  private

  def process_phrases
    rarray = Array.new(@ending - @starting)
    rarray = rarray.each_with_index { |item, i| rarray[i] = [i + @starting, item] }
    @phrase_multiples.each { |pm| fill_multiples(rarray, pm[1], pm[0]) }
    rarray
  end

  def fill_multiples(fill_array, the_int, printed)
    (the_int - (fill_array[0][0] % the_int)).step(fill_array.size - 1, the_int) do |i|
      fill_array[i][1] = fill_array[i][1].to_s + printed.to_s
    end
  end

end

fb = FizzBuzz.new(1,100)
fb.add_phrase_multiple('fizz', 3)
fb.add_phrase_multiple('buzz', 5)
fb.print_phrases

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Transactions vs. Analytics in Apache Kafka
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • Waterfall Vs. Agile Methodologies: Which Is Best For Project Management?
  • Choosing Between REST and GraphQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo