S
S.Volkov
Daniel Martin said:Anyone care for a friendly game of golf?
I've got this in 67 characters...
56 chars
see 'Smallest FizzBuzz program' thread in comp.lang.ruby
Daniel Martin said:Anyone care for a friendly game of golf?
I've got this in 67 characters...
I'll match your 62, but I'm still thinking that some clever tricks might
get it lower!
My solution has 75 characters, damn, I will have another look at it!
to answer ari's questions:
try this:
case
when s+1==y && s-1==q
What you create above is an array with an element that is a range, so:Apparently this does not work:
array = []
array = array << (1..100)
who knew! Not I,
help?
thanks,
---------------------------------------------------------------|
Also, i seem to be unable to put a range into an array. Yes, i
understand this is very simple, but when I did what I THOUGHT would
work, I can't get a range of numbers to be inserted into an array.
Apparently this does not work:
array = []
array = array << (1..100)
----- Original Message -----
From: "Ari Brown" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Saturday, June 02, 2007 10:12 PM
Subject: Re: [QUIZ] FizzBuzz (#126)
..
Also, i seem to be unable to put a range into an array. Yes, i
understand this is very simple, but when I did what I THOUGHT would
work, I can't get a range of numbers to be inserted into an array.
Apparently this does not work:
array = []
array = array << (1..100)
array = (1..100).to_a
# or
array = [*1..100]
# or (golfers favorite?)
array = *1..100
# or (if you need to add it to existing array):
array.push *1..100
array=*1..?dOn 6/3/07 said:# or (golfers favorite?) not quite
array = *1..100
No idea, the recruiters are not here yet, they will arrive in an hourA non-golf solution of mine :
for n in (1..100)
if n%3==0 && n%5==0
puts "FizzBuzz"
elsif n%3==0
puts "Fizz"
elsif n%5==0
puts "Buzz"
else
puts n
end
end
Does above code fit for a job interview?![]()
S.Volkov said:56 chars
see 'Smallest FizzBuzz program' thread in comp.lang.ruby
There has been some debate on the proper ways to screen programmers you intend
to hire. A common theory is that you really need to have the programmer write
some code for you to accurately gauge their skill. Exactly what to have them
write is another debate, but the blogosphere has recently been abuzz with this
question as a screener:
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.
(1..100).each do |i|
puts i.options
end
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=-=-=There has been some debate on the proper ways to screen
programmers you intend
to hire. A common theory is that you really need to have the
programmer write
some code for you to accurately gauge their skill. Exactly what
to have them
write is another debate, but the blogosphere has recently been
abuzz with this
question as a screener:
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.
My "honest" solution (what I really would have submitted to the
interviewer):
arr = (1..100).map do |i|
i = (("FizzBuzz" if i%15==0) or ("Fizz" if i%3==0) or ("Buzz" if i%
5==0) or i)
end
puts arr
I couldn't resist throwing in the Loki solution. Why Loki? Because
it's a mischievous rascal, and it makes excessive use of the name
"it", albeit misguidedly, which it thinks is fitting giving the recent
hoopla about "it":
require 'date'
class Fixnum
@@shapes = {
3 => "Fizz",
5 => "Buzz"
}
def self.invoke it
@@shapes = it if it.kind_of? Hash
end
def self.reveal
puts @@shapes.value.join( ' ' )
end
def << it
return self if it.empty?
it
end
def options( sep="", it=[] )
@@shapes.keys.sort.each do |k|
it << @@shapes[k] if self%k == 0
end
self << it.join( sep )
end
end
loki_roused = ARGV[0]
if loki_roused
the_simpsons = {
3=>"Homer",
5=>"Marge",
7=>"Bart",
11=>"Lisa",
13=>"Maggie"
}
Fixnum.invoke the_simpsons
end
(1..100).each do |i|
puts i.options
end
if loki_disturbed
jd = Date.today.jd
puts "\n---------------------------------"
puts "\n Out of the simpson family -- on this Julian day of #{jd},
Loki can shape shift into:\n #{(it = jd.options "
").kind_of?(String) ? it : 'none of them'}"
puts "---------------------------------\n"
end
Some thoughts:
This is, obviously, an incredibly easy programming puzzle -- as far as
writing down the pseudo code in english. It took me, however, no lie,
a good half hour just to decide on a course of action. In my head, I
struggled with the virtues of simplicity and the "coolness" of
conciseness, all while trying to avoid mediocrity. Actual programming
was a breeze. From there, though, no lie, at least fifteen minutes to
debug. I'm not kidding.
Now, I'm the first to admit that I'm new to Ruby, and programming is
not my strong suit, but I would never think that I'm one of those
people that couldn't program themselves out of a paper bag.
Thinking about this quiz just reinforced what I already knew about
myself. I'm not a guy who just jumps in and gets his hands dirty. In
fact, I'm the exact opposite. I over-think the problem and often get
nowhere. If this were a test of performance under pressure, I'm
certain I would have failed to impress during the interview.
In any case, to the other newbies out there: don't be intimidated by
such frivolous pursuits of the lofty few such as "golf". In fact, be
so bold as to join in if you dare. But, as in all things, be
steadfast in your Ruby endeavors. Enlightenment will come
Todd
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.