Post Reply 
Fizzbuzz, again
09-25-2017, 09:45 AM (This post was last modified: 09-25-2017 09:58 AM by brickviking.)
Post: #1
Fizzbuzz, again
The last time I saw this in the forums was in a FOCAL discussion, but I first ran into it somewhere else. Those of you who know what FIZZBUZZ is, won't need this explanation, but for everyone else who's not in the loop, fizzbuzz is a children's number game.

Numbers are counted from 1 to ... however many. There's a twist. If the number's divisible by 3, you say "fizz". If the number's divisible by 5, you say "buzz" instead. If it's divisible by both 3 and 5, you say fizzbuzz. Everything else, you simply say the number. A sample run would go like this:
1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, ... etc.

Here's my rough-cut example, I've had a rat-through and removed most of the bugs I know about. Could you suggest improvements?

progname: FBTST
Code:
\<<
@ This takes one arg on stack and reports arg, fizz or buzz
@ fizz if divisible by 3, buzz if divisible by 5 and "fizzbuzz" if both
@ replies the arg otherwise
  'J' STO
  "" 'K' STO
  'J' RCL
  DUP @ copy for if-test
  IF 
    3. MOD NOT 
  THEN
    'K' "FIZZ" STO+
  END
  @ This tests the first J value
  IF 
    5. MOD NOT
 THEN
   'K' "BUZZ" STO+
  END
@ grab this back out for a quick test
  'K' RCL
  IF 
    SIZE 1 < @ Is it empty?
  THEN 
    'J' RCL  @ spit the value back out
  ELSE
    'K' RCL @ otherwise, spit out fizz,buzz or fizzbuzz
  END
\>>

To get the complete sequence of numbers (at least to whatever I specify on the stack), I use the following dead simple loop.

Progname: FBLP
Code:
\<<
@ Accepts two args on stack. 2: 1  1:25  loops from 1 to 25
FOR J J FBTST NEXT
@ alternative would have been 1. 25. FOR J J FBTST NEXT
\>>

My ideal would be to not need two spare vars and to operate using just the stack, but I then have to start learning how to use ROLL, PICK, or ROT in various incarnations. I don't know whether that will make the program larger, but I'll give that a go within the next few days.

(Post 97)

Regards, BrickViking
HP-50g |Casio fx-9750G+ |Casio fx-9750GII (SH4a)
Visit this user's website Find all posts by this user
Quote this message in a reply
09-25-2017, 08:08 PM (This post was last modified: 09-25-2017 08:09 PM by Dave Britten.)
Post: #2
RE: Fizzbuzz, again
Under normal circumstances, you don't have to RCL variable values. Just do K rather than 'K' RCL, for example.

Here's a little quick-and-dirty version I banged out on my 48SX that demonstrates using a local subroutine, and doing more "on the stack".

Code:
\<<
  DUP2 SWAP - 1 +
  \<< DUP 3 MOD NOT "FIZZ" "" IFTE
    OVER 5 MOD NOT "BUZZ" "" IFTE
    + IF DUP SIZE THEN SWAP END DROP \>>
  \-> N FB
  \<< FOR C C FB EVAL NEXT N \->LIST \>>
\>>
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)