From: Ken Tilton on


job-271842874(a)craigslist.org wrote:
> Ken Tilton wrote:
>
>> Paul Wallich wrote:
>>
>>> job-271842874(a)craigslist.org wrote:
>>>
>>>> A friend passed on an article regarding the difficulty job
>>>> candidates had in producing even simple programs (simple as in
>>>> should take a minute or less). One example was a program to print
>>>> the numbers 1 to 100 except that "Fizz" should be substituted for
>>>> numbers divisible by 3, "Buzz" should be substituted for numbers
>>>> divisible by 5, and "FizzBuzz" should be substituted for numbers
>>>> divisible by both 3 and 5.
>>>>
>>> I'd probably brute-force the problem with a simple cond with a nested
>>> conditions, along the lines of
>>> (cond ((zerop (mod i 5))
>>> (cond ((zerop (mod i 3)) (print "FizzBuzz"))
>>> (t (print "Buzz"))))
>>> ((zerop (mod i 3))
>>> (print "Fizz"))
>>> (t (print i)))
>>
>>
>> I would object to using cond to implement if, and the redundant code
>> testing for (mod i 3) and associating it with Fizz.
>
>
> I agree. I expect mod is fast, but I don't like to repeat myself.
>
>> Otherwise, I am bothered by the spec. Is the FizzBuzz on 15 tied to it
>> factoring to 3 and 5 and those substitutions, or is that coincidental?
>> ie, Could an RFE come thru for 15 to translate to Bang? Also, is this
>> list expected to expand? If so, will the test always be even
>> divisibility?
>
>
> Yes, the FizzBuzz on 15 is because the spec said to print FizzBuzz
> instead of i for any i that is divisible by both 3 and 5. Regarding
> enhancements,

Sorry, I threw in that translation to 15 myself. But the question still
stands: could an RFE come along saying "if divisible by 3 and 5 print
BANG"? Or was the spec trying (and failing to say): the translations are
3 this and five that, where multiple matches occur print all. Then my
next question is: 45 is FizzFizzBuzz?

I have had managers lunge acroos the table to get at me.

kt

--
Well, I've wrestled with reality for 35 years, Doctor, and
I'm happy to state I finally won out over it.
-- Elwood P. Dowd

In this world, you must be oh so smart or oh so pleasant.
-- Elwood's Mom
From: job-271842874 on
Paul Wallich wrote:
> job-271842874(a)craigslist.org wrote:
>> (defun fizz-buzz (n)
>> (do ((i 1 (+ i 1)))
>> ((> i n))
>> (let
>> ((fizz (= 0 (mod i 3)))
>> (buzz (= 0 (mod i 5))))
>> (if fizz (format t "Fizz"))
>> (if buzz (format t "Buzz"))
>> (format t "~A~%"
>> (if (or fizz buzz) "" i)))))
>
> In both cases, what the local variables add in supposed elegance seems
> to me lost by the clunkiness of setting them in the first place (which
> you haven't done correctly).

Can you elaborate about how I incorrectly set the local variables? The
program functions correctly, so maybe you're referring to using zerop
instead?

> I'd probably brute-force the problem with a simple cond with a nested
> conditions, along the lines of
> (cond ((zerop (mod i 5))
> (cond ((zerop (mod i 3)) (print "FizzBuzz"))
> (t (print "Buzz"))))
> ((zerop (mod i 3))
> (print "Fizz"))
> (t (print i)))
>
> Someone else can do the loop version, the version with a macro that
> generates any possible fizz-buzz function of two small integers, the ver

I haven't made it to the chapter on macros yet, but would it be better
Lisp style to write a macro to be able to handle any two integers, or
just add 2 parameters e.g. (defun fizz-buzz (n a b) ... ), or maybe it
would be more Lisp-like to pass a list as the 2nd param (defun fizz-buzz
(n lst) ... ) where each element in the list would contain an integer
and the replacement string - something like:

(defun fizz-buzz (n lst)
(do ((i 1 (+ i 1)))
((> i n))
(let
((fizzed nil))
(dolist (obj lst)
(let ((a (car obj))
(str (car (cdr obj))))
(when (zerop (mod i a))
(princ str)
(setf fizzed t))))
(if (not fizzed)
(princ i))
(terpri))))

(fizz-buzz 100 '((3 "Fizz") (5 "Buzz")))

I know this is butt ugly, but the principle of generalizing with a 2nd
list parameter seems like a reasonable enhancement. Since I'm not to the
point of "thinking in Lisp", this would be my first approach, but maybe
macros would be the way to go here. Hopefully, I'll have a feel for that
after chapter 10 :)

Brian


From: Vassil Nikolov on

On Tue, 27 Feb 2007 22:30:06 -0500, job-271842874(a)craigslist.org said:

| Ken Tilton wrote:
| ...
|| I would object to using cond to implement if, and the redundant code
|| testing for (mod i 3) and associating it with Fizz.

| I agree. I expect mod is fast, but I don't like to repeat myself.

But this isn't about the speed of MOD (after all, we can justifiably
expect the compiler to arrange for the respective call to be made
no more than once), but---to reuse the expression---about getting
thrown out of the interview because of introducing the DIVISIBLE-BY-3,
DIVISIBLE-BY-5, and ANY-NUMBER classes, and doing it with method
combination to address these concerns:

|| Otherwise, I am bothered by the spec. Is the FizzBuzz on 15 tied to it
|| factoring to 3 and 5 and those substitutions, or is that coincidental?
|| ie, Could an RFE come thru for 15 to translate to Bang? Also, is this
|| list expected to expand? If so, will the test always be even divisibility?

|| This is how I get thrown out of most tech interviews.

---Vassil.

--
mind mate, n.
One of two persons mentally compatible with each other (cf. soul mate).
From: job-271842874 on
Ken Tilton wrote:
> job-271842874(a)craigslist.org wrote:
>> Ken Tilton wrote:
>>> Paul Wallich wrote:
>>>> job-271842874(a)craigslist.org wrote:
>>>>> A friend passed on an article regarding the difficulty job
>>>>> candidates had in producing even simple programs (simple as in
>>>>> should take a minute or less). One example was a program to print
>>>>> the numbers 1 to 100 except that "Fizz" should be substituted for
>>>>> numbers divisible by 3, "Buzz" should be substituted for numbers
>>>>> divisible by 5, and "FizzBuzz" should be substituted for numbers
>>>>> divisible by both 3 and 5.
>>>>>
>>> Otherwise, I am bothered by the spec. Is the FizzBuzz on 15 tied to
>>> it factoring to 3 and 5 and those substitutions, or is that
>>> coincidental? ie, Could an RFE come thru for 15 to translate to Bang?
>>> Also, is this list expected to expand? If so, will the test always be
>>> even divisibility?
>>
>> Yes, the FizzBuzz on 15 is because the spec said to print FizzBuzz
>> instead of i for any i that is divisible by both 3 and 5. Regarding
>> enhancements,
>
> Sorry, I threw in that translation to 15 myself. But the question still
> stands: could an RFE come along saying "if divisible by 3 and 5 print
> BANG"? Or was the spec trying (and failing to say): the translations are
> 3 this and five that, where multiple matches occur print all. Then my
> next question is: 45 is FizzFizzBuzz?

Gotcha. Well, I just posted a reply to Paul with a generalization of
passing in a 2nd list parameter containing pairs of (integer, string)
with the intent of concatenating the strings for all integers that
evenly divide i.

So for the list '((2 "a")(3 "b")(5 "c")) we'd have
1
a
b
ac
5
ab
7
ac
b
a
11
abc

So, 45 would just be "FizzBuzz"

>
> I have had managers lunge acroos the table to get at me.
>
> kt
>
From: job-271842874 on
Thierry Pirot wrote:
> Thierry Pirot writes:
>
>> job-271842874 writes:
>>
>>> (defun fizz-buzz (n)
>>> (do ((i 1 (+ i 1)))
>>> ((> i n))
>>> (let
>>> ((fizz (= 0 (mod i 3)))
>>> (buzz (= 0 (mod i 5))))
>>> (if fizz (format t "Fizz"))
>>> (if buzz (format t "Buzz"))
>>> (format t "~A~%"
>>> (if (or fizz buzz) "" i)))))
>>>
>>> (fizz-buzz 100)
>>>
>> In a more functional style, my 2 cents,
>>
> Ups, it's late here...
>> CL-USER> (defun printing (i)
>> (format t "~a~%"
>> (cond
> ((= 0 (mod i (* 3 5))) "FizzBuzz")
>> ((= 0 (mod i 3)) "Fizz")
>> ((= 0 (mod i 5)) "Buzz")
>> (t i))))
>

Maybe I'm reading this incorrectly, but wouldn't that always print i?
Printing i is mutually exclusive with printing "Fizz" and/or "Buzz".