On Jun 5, 2007, at 8:37 PM, Stephen S. wrote:
Is there a short circuit AND in ruby?
&& does short-circuit.
James Edward G. II
On Jun 5, 2007, at 8:37 PM, Stephen S. wrote:
Is there a short circuit AND in ruby?
&& does short-circuit.
James Edward G. II
James Edward G. II wrote:
On Jun 5, 2007, at 8:37 PM, Stephen S. wrote:
Is there a short circuit AND in ruby?
&& does short-circuit.
as does “and”, but beware the lower precedence:
irb(main):010:0> x = false && 3; x
=> false
irb(main):011:0> x = true && 3; x
=> 3
irb(main):012:0> x = false and 3; x
=> false
irb(main):013:0> x = true and 3; x
=> true
irb(main):014:0> x = (false and 3); x
=> false
irb(main):015:0> x = (true and 3); x
=> 3
After completing my Ruby Q. 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
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)
On Jun 6, 2007, at 2:05 PM, Morton G. wrote:
Quiz 126 is now the most popular Ruby Q. 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
Quiz 126 is now the most popular Ruby Q. ever. Quiz 84 was the
previous champion. All Rubyists will grasp the significance of 126 -
84 = 42
Ain’t numerology fun?
Regards, Morton
On 6 Jun 2007, at 20:05, Morton G. wrote:
Quiz 126 is now the most popular Ruby Q. 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 R.
[email protected]
I always thought Smalltalk would beat Java, I just didn’t know it
would be called ‘Ruby’ when it did.
– Kent Beck
On Jun 6, 2007, at 2:33 PM, Rob B. wrote:
On Jun 6, 2007, at 2:05 PM, Morton G. wrote:
Quiz 126 is now the most popular Ruby Q. 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
It’s too bad, but that theory fails. I wish it were otherwise, but
Ruby Q. 42 was (I think) the least popular of all time.
Regards, Morton
On Jun 6, 2007, at 12:31 PM, Joshua B. wrote:
Well, since no one did go for the threaded solution…
There was one. See:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/254354
James Edward G. II
On Jun 4, 8:03 pm, Daniel M. [email protected] wrote:
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.
On Jun 6, 4:34 pm, Phrogz [email protected] wrote:
On Jun 4, 8:03 pm, Daniel M. [email protected] wrote:
A> I CAN HAS INTERVIEW? I ARE ADVANCED PROGRAMMER.
B> O HAI. U CAN HAS CALLCC? GIMMEH FIZZBUZZ SOLUTION!
ROFLMAOBBQ!
Well done!
That reminds me…
For those that haven’t seen it, our brethren/enemy Pythonistas don’t
have to reserve the above lolcat syntax for comments only:
http://www.dalkescientific.com/writings/diary/archive/2007/06/01/lolpython.html
They are winning the race to amusingly obfuscated code!
James G. wrote:
On Jun 6, 2007, at 12:31 PM, Joshua B. wrote:
Well, since no one did go for the threaded solution…
There was one. See:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/254354
James Edward G. II
Yeah, I noticed that shortly after I sent in mine. Ok, so I can’t claim
the first threaded solution…but maybe at least the first “safely”
threaded solution? (i.e. try running the aforementioned solution in
irb…)
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
On Jun 3, 5:43 pm, darren kirby [email protected] wrote:
print "Fizz " elif n % 3 == 0: sys.stdout.write("Fizz ") elif n % 5 == 0: sys.stdout.write("Buzz ") else: sys.stdout.write("%i " % n)
A slight clean-up of the Python code.
The trailing comma on the print will
add a space as separator.
import sys
for n in range(1, int(sys.argv[1])+1):
if n % 15 == 0:
print “FizzBuzz”,
if n % 3 == 0:
print “Fizz”,
if n % 5 == 0:
print “Buzz”,
else:
print n,
print
On Jun 10, 6:14 am, Paddy3118 [email protected] wrote:
if n % 15 == 0
fizz.py
print "Fizz", if n % 5 == 0: print "Buzz", else: print n,
- Paddy.
I’m a Dork!
I posted my scriblings instead of:
import sys
for n in range(1, int(sys.argv[1])+1):
if n % 15 == 0:
print “FizzBuzz”
elif n % 3 == 0:
print “Fizz”
elif n % 5 == 0:
print “Buzz”
else:
print n
print
On 6/6/07, Brad P. [email protected] wrote:
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
On Sun, 03 Jun 2007 13:55:56 -0400, S.Volkov wrote:
print “FizzBuzz”.
=end
def sol1 maxn=100
end
s = “FizzBuzz”
puts ‘### TC1a’
if s.empty? : i else s end
s = “Fizz” if (n%3).zero?
def toFizzBuzz1.upto(?d){|i|i%3<1&&x=:Fizz;puts i%5<1?"#{x}Buzz":x||i}# 56
Another 56 byter
putsseq 100|sed -e'5~5s/.*/Buzz/;3~3s/^[0-9]*/Fizz/'
Martin DeMello wrote:
b = 5
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.
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
else
puts “fizz”
f += 3
end
elsif i == b
puts “buzz”
b += 5
else
puts i
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs