Author Topic: =={challenge}== Programmer Test - Fizz/Buzz  (Read 7659 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #15 on: August 21, 2014, 10:51:25 PM »
Stefan,
ph:FB5 is faster than I'd anticipated. I thought the nth would slow it down a little.
Well done !!
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16727
  • Superior Stupidity at its best
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #16 on: August 21, 2014, 11:02:03 PM »
I'd be interested to see how the various attempts benchmark when utilizing any two random divisors

Something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun fb (n d1 d2)
  2.   (nth (logior (min (rem n d1) 1) (lsh (min (rem n d2) 1) 1))
  3.        (list "FizzBuzz" "Buzz" "Fizz" n)
  4.   )
  5. )
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #17 on: August 22, 2014, 04:30:07 AM »

Javascript in Chrome Console API

Code - Javascript: [Select]
  1. for (i = 0; i < 30;)console.log((++i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i)
  2.  

Difficult to get it any tighter than this I think.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #18 on: August 22, 2014, 04:51:18 AM »
And some C# options
courtesy of stackoverflow :)

Code - C#: [Select]
  1. using System;
  2.  
  3. namespace FizzyButt
  4. {
  5.     internal class Program
  6.     {
  7.         private static void Main(string[] args)
  8.         {
  9.             FizzyButt01();
  10.             FizzyButt02();
  11.         }
  12.  
  13.         public static void FizzyButt01()
  14.         {
  15.             for (int i = 1; i <= 30; i++)
  16.             {
  17.                 bool fizz = i%3 == 0;
  18.                 bool buzz = i%5 == 0;
  19.                 if (fizz && buzz) {Console.WriteLine("FizzBuzz");}
  20.                 else if (fizz)    {Console.WriteLine("Fizz");}
  21.                 else if (buzz)    {Console.WriteLine("Buzz");}
  22.                 else              {Console.WriteLine(i);}
  23.             }
  24.         }
  25.  
  26.         public static void FizzyButt02()
  27.         {
  28.             for (int i = 1; i < 31; i++)
  29.             {
  30.                 Console.WriteLine(
  31.                                   "{0:#;}{1:;;Fizz}{2:;;Buzz}",
  32.                                   i%3*i%5 == 0 ? 0 : i,
  33.                                   i%3,
  34.                                   i%5);
  35.             }
  36.         }
  37.     }
  38. }
  39.  
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #19 on: August 22, 2014, 05:17:06 AM »
This will probably keep any propellerheads happy over the weekend :-D

http://rosettacode.org/wiki/FizzBuzz

//--------------------
piccy stolen from www.propellerhead.com.au
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #20 on: August 22, 2014, 05:20:41 AM »
Powershell:

Code - PowerShell: [Select]
  1. function fizzbuzz ( $n )
  2. {      
  3.     if (($f = ($n%3 -eq 0)) -and ($b = ($n%5 -eq 0))) { write-host 'FizzBuzz' }
  4.     else
  5.     {
  6.         if ($f) { write-host 'Fizz' }
  7.         else
  8.         {
  9.             if ($b) { write-host 'Buzz' }
  10.             else
  11.             {
  12.                 write-host $n
  13.             }
  14.         }
  15.     }
  16. }
  17.  
  18. (1..30) | % {fizzbuzz $_}

EDIT: Oops - forget elseif was available in PowerShell :oops:

Though, the PowerShell code on Rosetta could be shortened slightly to:
Code - PowerShell: [Select]
  1. 1..100 | % {
  2.     if ($_ % 15 -eq 0) {
  3.         "FizzBuzz"
  4.     } elseif ($_ % 5 -eq 0) {
  5.         "Buzz"
  6.     } elseif ($_ % 3 -eq 0) {
  7.         "Fizz"
  8.     } else {
  9.         $_
  10.     }
  11. }
« Last Edit: August 22, 2014, 05:32:19 AM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #21 on: August 22, 2014, 06:19:32 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:FB5 (n)
  2.   (cond
  3.     ((nth (rem n 15) '("FizzBuzz" nil nil "Fizz" nil "Buzz" "Fizz" nil nil "Fizz" "Buzz" nil "Fizz" nil nil)))
  4.     (n)
  5.     )
  6.   )

Another couple of variations of this method:
Code - Auto/Visual Lisp: [Select]
  1. (defun fizzbuzz ( n )
  2.     (cond ((nth (gcd n 15) '(nil nil nil "Fizz" nil "Buzz" nil nil nil nil nil nil nil nil nil "FizzBuzz"))) (n))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun fizzbuzz ( n )
  2.     (nth (gcd n 15) (list nil n nil "Fizz" nil "Buzz" nil nil nil nil nil nil nil nil nil "FizzBuzz"))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun fizzbuzz ( n / g )
  2.     (if (= 1 (setq g (gcd n 15))) n (nth (/ g 5) '("Fizz" "Buzz" nil "FizzBuzz")))
  3. )
« Last Edit: August 22, 2014, 06:23:23 AM by Lee Mac »

Stefan

  • Bull Frog
  • Posts: 221
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #22 on: August 22, 2014, 06:35:32 AM »
Something stolen from rosettacode.org (the gcd thing)
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:fb8 (n / r)
  2.   (cond
  3.     ((=  3 (setq r (gcd 15 n))) "Fizz")
  4.     ((=  5 r) "Buzz")
  5.     ((= 15 r) "FizzBuzz")
  6.     (n)
  7.   )
  8. )

And a variant
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:fb9 (n)
  2.   (or *lst2* (setq *lst2* '((3 . "Fizz") (5 . "Buzz") (15 . "FizzBuzz"))))
  3.   (cond
  4.     ((cdr (assoc (gcd 15 n) *lst2*)))
  5.     (n)
  6.   )
  7. )

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #23 on: August 22, 2014, 11:50:28 AM »
I considered using assoc, but thought it may be slow given the benchmark of my earlier function...  :|

pBe

  • Bull Frog
  • Posts: 377
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #24 on: August 23, 2014, 11:27:40 PM »
a different approach to add to the table  ;D

Code: [Select]
(Defun _fizzybuzzy (n / _n n_ nl _3 _5)
  (setq   n_ n
   _n (vl-string->list (itoa n))
  )
  (Setq   _5 (or (= (setq nl (last _n)) 48)
          (= nl 53)
      )
  )
  (cond
    ((and (setq   _3 (= (Setq n
              (/ (apply '+ (mapcar '(lambda (m) (- m 48)) _n)) 3.0)
            )
            (fix n)
         )
     )
     _5
     )
     "FizzBuzz"
    )
    (_3 "Fizz")
    (_5 "Buzz")
    (T n_)
  )
)

EDIT: Bummer, there's something wrong with the Math.. hang on. editing.....

Code modified
« Last Edit: August 24, 2014, 12:36:23 AM by pBe »

gile

  • Water Moccasin
  • Posts: 2234
  • Marseille, France
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #25 on: August 24, 2014, 05:52:23 AM »
Hi,

My F# attempt (written before reading Kerry's link)

Code - F#: [Select]
  1. let fizzBuzz n =
  2.     match n % 3, n % 5 with
  3.     | 0, 0 -> "FizzBuzz"
  4.     | 0, _ -> "Fizz"
  5.     | _, 0 -> "Buzz"
  6.     | _ -> string n
  7.    
  8. [1 .. 30] |> List.map fizzBuzz

This is very closed to the following LISP code:

Code - Auto/Visual Lisp: [Select]
  1. (defun fizzBuzz (n / l)
  2.   (setq l (list (rem n 3) (rem n 5)))
  3.   (cond
  4.     ((equal l '(0 0)) "FizzBuzz")
  5.     ((zerop (car l)) "Fizz")
  6.     ((zerop (cadr l)) "Buzz")
  7.     (T (itoa n))
  8.   )
  9. )
  10.  
  11. (mapcar 'fizzBuzz  '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30))

You can try the F# code in Try F#
« Last Edit: August 24, 2014, 09:04:15 AM by gile »
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 520
  • Hypersensitive
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #26 on: August 24, 2014, 06:25:04 AM »
This should be fast and unique  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun FizzBuzz (l)
  2.   (mapcar '(lambda (i)(if (zerop (rem i 15))    "FizzBuzz"
  3.                         (if (zerop (rem i 5))   "Buzz"
  4.                           (if (zerop (rem i 3)) "Fuzz"
  5.                             i)
  6.                           )
  7.                         )
  8.              )
  9.           l
  10.           )
  11.   )
  12.  

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #27 on: August 25, 2014, 05:00:37 AM »
So far it seems as if everyone's is passing the test! Great one guys! The level of programming here is obviously "better" than some other places! https://news.ycombinator.com/item?id=3354027

Makes me weep for the "future" of programming! And probably understand why we get so many bugs in our programs.

Anyhoo ... my first "obvious" attempt (i.e. literally following the description of the question):
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:FizzBuzz (n)
  2.   (cond ((= (rem n 3) (rem n 5) 0) "FizzBuzz")
  3.         ((= (rem n 3) 0) "Fizz")
  4.         ((= (rem n 5) 0) "Buzz")
  5.         (n)))
Obviously very inefficient as it does at least 2 (on average nearly 4) division operations (which is one of the slowest CPU instructions you can give). I think that's why that benchmark shows such good results even for the nth version: nth is reasonably fast of short lists (which this definitely is).

So omitting one division (I think most of you got this one):
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:FizzBuzz0 (n)
  2.   (cond ((= (rem n 15) 0) "FizzBuzz")
  3.         ((= (rem n 3) 0) "Fizz")
  4.         ((= (rem n 5) 0) "Buzz")
  5.         (n)))

Trying to reduce on the number of divisions and using assoc instead of nth ... ehhh ... don't truly know which "should" be better
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:FizzBuzz1  (n)
  2.   (cond ((cdr (assoc (rem n 15) '((0 . "FizzBuzz") (3 . "Fizz") (5 . "Buzz") (6 . "Fizz")
  3.                                   (9 . "Fizz") (10 . "Buzz") (12 . "Fizz")))))
  4.         (n)))

And then rather using member instead
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:FizzBuzz2 (n / r)
  2.   (cond ((= (setq r (rem n 15)) 0) "FizzBuzz")
  3.         ((member r '(3 6 9 12)) "Fizz")
  4.         ((member r '(5 10)) "Buzz")
  5.         (n)))

Of course the nth function would make it lots less circuitous. ... uh! ... scratch that my FizzBuzz3 is the exact same thing as ph:fb5 ... and I thought I got it so well!

I think none of mine is bringing anything "new" to the table. You guys already covered everything I had thought of myself, and then some!

Actually ... just though of another:
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:FizzBuzz4 (n / L)
  2.   (if (setq L (vl-remove nil (mapcar '(lambda (d s) (cond ((= (rem n d) 0) s))) '(3 5) '("Fizz" "Buzz"))))
  3.     (apply 'strcat L)
  4.     n))
Sorry, it's probably been thought of by some of yourselves as well! Not that it's doing much different really - still performing 2 division operations and then all sorts of list manipulations, so speedwise I'd not hold my breath.

Anyhow, I've recently started looking into Haskell. Here's my first "very obvious" try (i.e. same as my FizzBuzz0 routine):
Code - Haskell: [Select]
  1. fizzBuzz :: Integer -> String
  2. fizzBuzz n | n `mod` 15 == 0 = "FizzBuzz"
  3.            | n `mod` 5  == 0 = "Fizz"
  4.            | n `mod` 3  == 0 = "Buzz"
  5.            | otherwise       = show n
Well ... not "exactly" the same since Haskell is static typed it's a bit difficult making a list with a mix of ints and strings like the Lisp does. I'm probably just not knowledgeable enough yet to get to the point fo doing it "properly" idiomatically! Definitely not to the degree in rosetacode, actually it's "exactly" the same as the 1st one there  :-o! I actually like that lazy infinite list approach ... wonder if it would be much faster since it never uses any divisions.

That drives me to wish we had lazy eval in AutoLisp ... another thing ADesk removed from the XLisp core they stole. I suppose one could simulate it by using a cached list, but it would be much less than optimal as it would need to grow at the end (not at the front, so either lots of reverses or lots of appends). Not to mention it would start degrading on larger numbers due to nth being a bit slow when the list is long.
« Last Edit: August 25, 2014, 05:16:36 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1025
  • Marco
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #28 on: August 25, 2014, 05:08:40 AM »
IMHO,
from my tests is the best version is Stefan's:

Code: [Select]
(defun ph:FB5 (n)
  (cond
    ( (nth (rem n 15) '("FizzBuzz" nil nil "Fizz" nil "Buzz" "Fizz" nil nil "Fizz" "Buzz" nil "Fizz" nil nil)))
       (n)
  )
)

originality 10
concision  10
speed      10

Last tests:
Code: [Select]
Benchmarking [M.P. 2005 < revised kdub 2005>] ...............
Elapsed milliseconds for 4096 iteration(s)/ relative Timing :

    (MAPCAR (QUOTE FIZZBUZZL2) FIZZLIST).....250 / 1.1416 <slowest>
    (MAPCAR (QUOTE FIZZBUZZL2) FIZZLIST).....250 / 1.1416
    (MAPCAR (QUOTE FIZZBUZZL2) FIZZLIST).....249 / 1.137
    (MAPCAR (QUOTE PH:FB5) FIZZLIST).........234 / 1.0685
    (MAPCAR (QUOTE PH:FB5) FIZZLIST).........234 / 1.0685
    (MAPCAR (QUOTE PH:FB5) FIZZLIST).........219 / 1 <fastest>


Code: [Select]
;Lee
 (defun fizzbuzzL2 ( n / g )
           (if (= 1 (setq g (gcd n 15))) n (nth (/ g 5) '("Fizz" "Buzz" nil "FizzBuzz")))
)

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #29 on: August 25, 2014, 05:47:43 AM »
The level of programming here is obviously "better" than some other places! https://news.ycombinator.com/item?id=3354027

 :-o



Great challenge Irné - although relatively simple, I've enjoyed this thread  :-)