Re: FizzBuzz
- From: "J Thomas" <jethomas5@xxxxxxxxx>
- Date: 9 Mar 2007 19:41:47 -0800
On Mar 9, 8:49 pm, "Doug Hoffman" <dhoff...@xxxxxxxxxxxxxxx> wrote:
On Mar 9, 8:34 pm, "J Thomas" <jethom...@xxxxxxxxx> wrote:
I like most of the solutions that have been presented. But if you want
the ultimate of maintainability, something like this is likely to be
good:
10 FOR I = 1 TO 100
20 IF (I MOD 3 = 0) THEN PRINT "Fizz";
30 IF (I MOD 5 = 0) THEN PRINT "Buzz";
40 IF ((I MOD 3 <> 0) AND (I MOD 5 <> 0)) THEN PRINT I;
50 PRINT
60 NEXT I
I don't really think that BASIC is easier to learn than Forth. But a
lot of people think it is, and a lot of people learn it very quickly
-- maybe partly because their confidence is so high. Practically
anybody can quickly learn enough BASIC to maintain this program.
You didn't solve the problem. The problem was clearly defined as
FizzBuzzBoomZapp. From 1 to 1000. Read up a few posts if you didn't
catch that. (Hint: It might help to use Forth. I'm fairly certain
this is a Forth newsgroup.)
Have I done something that offended you? I think I'm detecting a
certain coolness to your tone. I remember years when this newsgroup
was nice and mellow, experts and hobbyists and newbies got along real
well. And only a few years ago the only real irritants were Jeff Fox
and John Passaniti. Oh well.
I like your solution to the 4 variable problem. If there's any
improvement to be made it would be factoring your 4 subroutines, which
repeat the exact same code 4 times and change only the data.
Here's one way:
: makesub ( n -- ) ( E: i -- 0|n )
CREATE C, PARSE-NAME STRING,
DOES> TUCK C@ MOD TUCK 0= IF 1+ COUNT TYPE 0 THEN DROP ;
3 makesub fizz? Fizz
5 makesub buzz? Buzz
7 makesub boom? Boom
9 makesub zapp? Zapp
I find this code harder to read, but I'm guessing it might be slightly
more maintainable since instead of copying and pasting the definition
you could just do
11 makesub zilch? Zilch
etc.
On the other hand, if you're going to have a whole series of them, why
name them? You could put the addresses of the data structures into an
array, and loop through the array. But this is overkill.
I think this is somewhat less readable. But the code should be
smaller. We re-use the same code 4 times. When should we sacrifice
readability for size? On memory-limited systems, one of Forth's
traditional niches?
Now, the bit-array approach is very nice but it depends on 3 5 *
fitting into a cell, and 3 5 * 7 * does not fit except in a few
systems. We can do without the double, triple, and quadruple bit-
arrays. I'll use a similar method that I think is more readable.
in place of .... 3 MOD DUP 0= IF ." Fizz" THEN
we can use
..... 1 fizzle DUP 2@ = IF ! ." Fizz" FALSE EXIT THEN +! TRUE ....
We count to three, when we get there we reset the counter.
One advantage of this approach is that it can be maintained by
somebody who isn't real clear what MOD does. And it's faster when MOD
is slow compared to this long expression.
Anyway, I liked your solution. And if you want readability, nothing
can beat the BASIC version which I won't bother to modify for you.
.
- Follow-Ups:
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- References:
- Re: FizzBuzz
- From: Andrew Haley
- Re: FizzBuzz
- From: J Thomas
- Re: FizzBuzz
- From: John Passaniti
- Re: FizzBuzz
- From: atashi
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- From: Bruce McFarling
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- From: J Thomas
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- From: J Thomas
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- From: J Thomas
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- From: J Thomas
- Re: FizzBuzz
- From: Doug Hoffman
- Re: FizzBuzz
- Prev by Date: Re: so forth is useless except for limited mem environments?
- Next by Date: Re: so forth is useless except for limited mem environments?
- Previous by thread: Re: FizzBuzz
- Next by thread: Re: FizzBuzz
- Index(es):
Relevant Pages
|