Author Topic: FizzBuzz Plus  (Read 7164 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #15 on: February 04, 2021, 12:25:04 am »
I didn't save what I had earlier, but I think this was it....

- Dav

Code: QB64: [Select]
  1. FOR f = 1 TO 100
  2.     IF f MOD 15 <> 0 AND f MOD 5 <> 0 AND f MOD 3 <> 0 THEN PRINT STR$(f) ELSE IF f MOD 15 = 0 THEN PRINT "FizzBuzz" ELSE IF f MOD 3 = 0 THEN PRINT "Fizz" ELSE IF f MOD 5 = 0 THEN PRINT "Buzz"

Edit: i guess you could do a big one line, if f = 1 then else if f = 2 then, etc up to 100.  But, id really have to want that job bad...
-

Oh nice! I didn't think you could do ElseIf's on one line, good to know.

That code works without the first test mod 15 <> 0 because the AND of next 2 cover it mod 5 <> 0 AND mod 3 <> 0.

Update: I looked back and Danilin did try a version of Dav code but he kept putting in an extra line.
« Last Edit: February 04, 2021, 12:32:00 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: FizzBuzz Plus
« Reply #16 on: February 04, 2021, 12:27:05 am »
How about doing it with the fewest possible characters? Lines are too ambiguous now.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #17 on: February 04, 2021, 12:37:15 am »
How about doing it with the fewest possible characters? Lines are too ambiguous now.

Hey is this a spontaneous jam? LOL

Yeah let's see who can do it in least bytes as read in Windows properties or Linux version of that (could there be a discrepancy between the 2? and/or mac.) I am thinking CR+LF versus just LF.

Lines are OK as long as colon isn't allowed, I think.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: FizzBuzz Plus
« Reply #18 on: February 04, 2021, 12:37:44 am »
Here's an odd little experimental version:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. start## = TIMER
  3. DIM answer(10 ^ 7) AS STRING
  4. FOR i& = 3 TO 10 ^ 7 STEP 3: answer(i&) = "Fizz": NEXT
  5. FOR i& = 5 TO 10 ^ 7 STEP 5: answer(i&) = answer(i&) + "Buzz": NEXT
  6. FOR i& = 10 ^ 7 - _HEIGHT * 10 TO 10 ^ 7
  7.     IF LEN(answer(i&)) THEN PRINT answer(i&); ","; ELSE PRINT STR$(i&); ",";
  8. PRINT: PRINT TIMER - start##

Less than a second for the 10^7 test that Danlin was running above, and now we have our fizzes and buzzes all stored in a nice little array for future reference.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #19 on: February 04, 2021, 12:49:09 am »
I tried a neater comma between and oddly it takes a tad longer, also am not seeing any time difference with
$Checking:Off in fact one run was faster without it.

Why is that so fast?
 

Update: Ah ha! No decisions!
« Last Edit: February 04, 2021, 12:55:15 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: FizzBuzz Plus
« Reply #20 on: February 04, 2021, 01:12:02 am »
I tried a neater comma between and oddly it takes a tad longer, also am not seeing any time difference with
$Checking:Off in fact one run was faster without it.

Why is that so fast?
 

Update: Ah ha! No decisions!

I cheated...  Look close, and you'll see my NINJA trick!!  If you can't figure it out by morning, I'll post it for you, but I know you like a chance to study and figure things out for yourself first.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #21 on: February 04, 2021, 01:58:54 am »
I cheated...  Look close, and you'll see my NINJA trick!!  If you can't figure it out by morning, I'll post it for you, but I know you like a chance to study and figure things out for yourself first.  ;)

Ohhhh you only printed the final screen LOL!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #22 on: February 04, 2021, 02:10:05 am »
OK for the least Bytes I have Dav's with some fixing help from me:
Code: QB64: [Select]
  1. FOR f = 1 TO 100
  2.     IF f MOD 5 <> 0 AND f MOD 3 <> 0 THEN PRINT f ELSE IF f MOD 15 = 0 THEN PRINT "FizzBuzz" ELSE IF f MOD 3 = 0 THEN PRINT "Fizz" ELSE IF f MOD 5 = 0 THEN PRINT "Buzz"
  3.  
194 bytes in 3 lines.

bplus after learning that Else If can be used on one line as long as you divide Else with If:
Code: QB64: [Select]
  1. FOR i = 1 TO 100
  2.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN PRINT "FizzBuzz" ELSE IF i MOD 3 = 0 THEN PRINT "Fizz" ELSE IF i MOD 5 = 0 THEN PRINT "Buzz" ELSE PRINT i
  3.  
171 bytes also 3 lines.

Wait if we are doing bytes, colons for line statement separators don't matter? but I don't think there is savings for the 3 liners above.

Edit: I did not need the first line I had.
Anyway here is another bplus submission, a little unusual because it's from his SB Interpreter:
Code: [Select]
[
n i i+1
i i > 100
x
f
i i%5 = 0 and i%3 = 0
, FizzBuzz
e
i i%3 = 0
, Fizz
e
i i%5 = 0
, Buzz
e
, i
f
f
f
]
126 bytes and looks like about 19 lines!

 
126 byte FizzBuzz.PNG







« Last Edit: February 04, 2021, 02:19:34 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: FizzBuzz Plus
« Reply #23 on: February 04, 2021, 02:19:57 am »
Ohhhh you only printed the final screen LOL!

Since these are interview type questions, those are my interview type answers.  It tells them a bit about me, and helps me learn a bit about them.

A boss who looks over that and says, “You didn’t print results from 1 to 10 ^ 7”, without asking me WHY, is one I don’t want to work for.  They’re not into creative solutions, original thinking, or individual problem solving — they just want a monkey that can code up to perfect specifications.  It’s too high stress of a job for me, so, “NO THANKS!”

Now, if he asks why I chose such an approach, I can tell him:

“Our view port is limited by printing to the monitor, so there’s no reason to just run and scroll the screen excessively.  Instead, I printed a whole screen of results, stored the solutions in an array, improved execution times, and now have access to any portion of that data that you’d like to view and work with, on hand.”

I not only completed his task, but I *improved* upon it.

Which in some companies will get you promoted.  In others, it’ll get you fired.

Luckily for me, I don’t want to work for those second BLEEPERS!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: FizzBuzz Plus
« Reply #24 on: February 04, 2021, 02:22:51 am »
While we're showing off interpreters, here's an example I had kicking around. By no means the smallest code. Kindof a lot of formatting going on that doesn't need to happen:

 
ss.png
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #25 on: February 04, 2021, 02:38:32 am »
Now if you want to compare how much time it took for me to write that 126 byte program...  ;(

STx did yours take longer than an QB64 program? maybe you stay current with yours.

« Last Edit: February 04, 2021, 02:47:01 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: FizzBuzz Plus
« Reply #26 on: February 04, 2021, 02:42:03 am »
I doubt any version of sxipt will be faster than qb64 - although the particular implementation we're seeing above is javascript (obviously I guess), which has zero compile time. So in a way, it's quite faster... Meaning, the workflow is faster. Make a tweak, you get the result instantly.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #27 on: February 04, 2021, 02:52:08 am »
Oh you know what? I bet I could setup Notepad++ to run my code through SB.exe all it needs is the filename to Run; that would beat editing and then drag and drop onto SB.exe. I just recently loaded the NppExec extension for editing and running JB code.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: FizzBuzz Plus
« Reply #28 on: February 04, 2021, 02:53:00 am »
If you use Dav's idea from the other day, you can make it directly into an exe, too!
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #29 on: February 04, 2021, 02:57:24 am »
Yes but nothing you can run on SB is worth the trouble of immortalization.