[QUIZ] FizzBuzz (#126)

M

Morton Goldberg

After completing my Ruby Quiz submission, I decided to implement
FizzBuzz in Mathematica. Here is the first version that I wrote.

f[x_] := Print["FizzBuzz"] /; Mod[x, 15] == 0
f[x_] := Print["Buzz"] /; Mod[x, 5] == 0
f[x_] := Print["Fizz"] /; Mod[x, 3] == 0
f[x_] := Print[x]
f /@ Range[100];

I think this example pretty well gives the flavor of Mathematica.
Note that no explicit flow control is needed. Of course, Mathematica,
like Ruby, allows one to write FizzBuzz in many ways, including some
that would look much more procedural and others that are more
functional. Here is an example of the latter.

MapThread[(f[z_] := Print[#1] /; Mod[z, #2] == 0) &,
{{"FizzBuzz", "Buzz", "Fizz"}, {15, 5, 3}}];
f[x_] := Print[x]
f /@ Range[100];

In this second example, the MapThread generates three lines of code
equivalent to the first three lines of the first example.

I apologize to anyone who finds this off topic post distracting, but
I am hoping at least some readers of mailing list will find it
interesting to see FizzBuzz in a language very different both from
Ruby and all the well-known static, procedural languages.

Regards, Morton
 
J

Joshua Ballanco

I hope it's not too late for a new submission!

So, when I first read this quiz, it didn't seem like enough of a
challenge. Immediately, I wondered if someone would come up with a
threaded answer, but then everyone started playing golf, and I got a bit
caught up in that...

Well, since no one did go for the threaded solution (and since I needed
to brush up on my threading skills anyway), I give you THE THREADED
FIZZBUZZER!!! (no, not the dreaded...threaded...) ;)

#!/usr/bin/env ruby -w

require 'monitor'
class FizzThreader
attr_reader :counting

def initialize
@counting = 1
@fizz_went = false
@buzz_went = false
@numbers_went = false
@done_counting = false
@output = ''
@output.extend(MonitorMixin)
end

def fizz
Thread.new do
loop do
@output.synchronize do
if !@fizz_went && !@buzz_went && !@numbers_went
@output << 'Fizz' if @counting%3 == 0
@fizz_went = true
end
end
break if @done_counting && @fizz_went
end
end
end

def buzz
Thread.new do
loop do
@output.synchronize do
if @fizz_went && !@buzz_went && !@numbers_went
@output << 'Buzz' if @counting%5 == 0
@buzz_went = true
end
end
break if @done_counting && @buzz_went
end
end
end

def numbers
Thread.new do
loop do
@output.synchronize do
if @fizz_went && @buzz_went && !@numbers_went
@output << @counting.to_s unless (@counting%3 == 0 ||
@counting%5 == 0)
@numbers_went = true
end
end
break if @done_counting && @numbers_went
end
end
end

def count_upto(i)
Thread.new do
loop do
@output.synchronize do
if @fizz_went && @buzz_went && @numbers_went
@output << "\n"
@counting += 1
@done_counting = true if @counting >= i
@fizz_went = false
@buzz_went = false
@numbers_went = false
end
end
break if @done_counting
end
end
end

def start_counting_upto(i)
@counting_threads = []
@counting_threads << fizz
@counting_threads << buzz
@counting_threads << numbers
@counting_threads << count_upto(i)
@counting_threads.each {|thr| thr.join}
@output
end

end

fizzy_threads = FizzThreader.new
puts fizzy_threads.start_counting_upto(100)
 
M

Morton Goldberg

Quiz 126 is now the most popular Ruby Quiz ever. Quiz 84 was the
previous champion. All Rubyists will grasp the significance of 126 -
84 = 42 :)

Ain't numerology fun?

Regards, Morton
 
R

Rob Biedenharn

Quiz 126 is now the most popular Ruby Quiz ever. Quiz 84 was the
previous champion. All Rubyists will grasp the significance of 126
- 84 = 42 :)

Ain't numerology fun?

Regards, Morton

puts "very popular" if RubyQuiz.number % 42 == 0

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
E

Enrique Comba Riepenhausen

Quiz 126 is now the most popular Ruby Quiz ever. Quiz 84 was the
previous champion. All Rubyists will grasp the significance of 126
- 84 = 42 :)

Ain't numerology fun?

Regards, Morton

Hmmmm 42...

The answer to all questions...

I wonder if on the next (and subsequent ruby quizzes) I could just
answer 42 :)


----
Enrique Comba Riepenhausen
(e-mail address removed)

I always thought Smalltalk would beat Java, I just didn't know it
would be called 'Ruby' when it did.
-- Kent Beck
 
M

Morton Goldberg

puts "very popular" if RubyQuiz.number % 42 == 0

It's too bad, but that theory fails. I wish it were otherwise, but
Ruby Quiz 42 was (I think) the least popular of all time.

Regards, Morton
 
B

Brian Adkins

#!/bin/env ruby

=begin
..
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".

Pretend you've just walked into a job interview and been hit with this
question.
Solve it as you would under such circumstances for this week's Ruby Quiz.

The task itself is quite boring, so I decided to imagine how
different programmers may try to pass the interview.
I hope we'll see what the recruiter may think.

Oh! And I played golf, just for fun, hope you will enjoy it too.
=end

##
# Q126 solution
# by Sergey Volkov
##

##
# job interview style
##
# Java programmer
def sol1 maxn=100
for i in 1..maxn
if i%3 == 0 && i%5 == 0
puts "FizzBuzz"
elsif i%3 == 0
puts "Fizz"
elsif i%5 == 0
puts "Buzz"
else
puts i
end
end
end
puts '### TC1'
sol1 15

##
# Same as above,
# but the code is more manageable
def sol1a maxn=100
for i in 1..maxn
if i%3 == 0 && i%5 == 0
s = "FizzBuzz"
elsif i%3 == 0
s = "Fizz"
elsif i%5 == 0
s = "Buzz"
else
s = i.to_s
end
puts s
end
end
puts '### TC1a'
sol1a 15

##
# Lisp programmer
def sol2 maxn=100
puts( (1..maxn).map{ |i|
i2s=lambda{ |n,s|
if (i%n).zero? : s else '' end
}
lambda{ |s|
if s.empty? : i else s end
}.call i2s[3,'Fizz'] + i2s[5,'Buzz']
} )
end
puts '### TC2'
sol2 15

##
# 1 year of Ruby experience
def sol3 maxn=100
1.upto(maxn){ |n|
s = "Fizz" if (n%3).zero?
(s||='') << "Buzz" if (n%5).zero?
puts s||n
}
end
puts '### TC3'
sol3 15

##
# Trying to get extra points for reusability..
class Fixnum
def toFizzBuzz
s = 'Fizz' if modulo(3).zero?
s = "#{s}Buzz" if modulo(5).zero?
s || to_s
end
end
def sol4 maxn
1.upto(maxn){ |n| puts n.toFizzBuzz }
end
puts '### TC4'
sol4 15

##
# Extra points for expandability
#.. who knows what else recruiters are looking for?

__END__

##
# Golf style
1.upto(?d){|i|puts ["#{x=[:Fizz][i%3]}Buzz"][i%5]||x||i}# 56
1.upto(?d){|i|x=[:Fizz][i%3];puts i%5<1?"#{x}Buzz":x||i}# 56
1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}# 56

Another 56 byter :)
puts `seq 100|sed -e'5~5s/.*/Buzz/;3~3s/^[0-9]*/Fizz/'`
 
P

Phrogz

# A> I CAN HAS INTERVIEW? I ARE ADVANCED PROGRAMMER.
#
# B> O HAI. U CAN HAS CALLCC? GIMMEH FIZZBUZZ SOLUTION!

ROFLMAOBBQ!
Well done!


I particularly like the 3 meme crossover in:
# B> I SEE WHAT YOU DID THERE
#
# A> WHAT YOU SAY !!
#
# B> U CAN HAS INJECT, RLY? NOT ALL SIDE EFFECTZ?


Oh, and your solutions are ftw, too. :)
 
R

Ryan Sobol

My solution that I'd submit in an interview:

1.upto(100) do |i|
buffer = i % 3 == 0 ? "Fizz" : nil
buffer = buffer.to_s + "Buzz" if i % 5 == 0
p buffer || i
end

--RYAN
 
M

Martin DeMello

I am sure I just submitted this with a lengthy explanation but it
didn't appear in the list ??? So here it is again with no explanation
except that it minimizes method calls as much as possible by
avoiding modulo operation and maintaining separate counters.


f = 3
b = 5

for i in 1..100 do
if i == f
if i == b
puts "fizzbuzz"
f += 3
b += 5

Sadly, + (and hence +=) is a method call too :)

martin
 
B

Brad Phelan

Martin said:
Sadly, + (and hence +=) is a method call too :)

martin

True but I think the overall method count will be lower
as the addition ops are only executed when the main
counter meets the correct conditions instead of
redundantly calculating the modulo for each loop
iteration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top