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

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
=={challenge}== Programmer Test - Fizz/Buzz
« on: August 21, 2014, 02:49:19 PM »
Here's a test some employers use to see if someone can actually think about a program. Mind you it's considered an "entry-level" test, so any decent programmer should be able to get it. Yet even many "professionals" tend to fail because there's a few "tricks" which you need to think about.

The task is as such:
  • You're supposed to write a function which accepts a positive integer
  • The function needs to return a value depending on that integer in the following ways:
    • If the number is divisible by 3 it should return "Fizz"
    • If the number is divisible by 5 it should return "Buzz"
    • If the number is divisible by both 3 and 5 it should return "FizzBuzz"
    • Else it should return the number
There are many ways you can achieve this. The point is usually not to try and get the fastest and/or most novel way, but just to show you can get it "correct".

Example output for 1 through 30:
Code: [Select]
(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))
(1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz")
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #1 on: August 21, 2014, 03:25:43 PM »
irne,

Why no negative or real numbers in the test list ?


ChrisCarlson

  • Guest
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #2 on: August 21, 2014, 03:58:49 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:FizzFuzzBuzz (/ a b c)
  2.         (initget 4)
  3.         (setq a (getint "\n Enter Numero: "))
  4.         (if (= (* (fix (/ a 3)) 3) a)
  5.                 (setq c "Fizz")
  6.                 (princ)
  7.                 ) : _end if    
  8.         (if (= (* (fix (/ a 5)) 5) a)
  9.                 (setq c "Buzz")
  10.                 (princ)
  11.         ) : _end if    
  12.         (if (and (= (* (fix (/ a 3)) 3) a) (= (* (fix (/ a 5)) 5) a))
  13.                 (setq c "FizzBuzz")
  14.                 (princ)
  15.         ) ;_end if     
  16.         (if (or (= c "Fizz") (= c "Buzz") (= c "FizzBuzz"))
  17.                 (princ (strcat "\n" c))
  18.                 (princ (strcat "\nUhOh You Chose: " (rtos a 2 0)))
  19.         ) ; _end if    
  20.         (princ)
  21. ) ; _end fun

My attempt

ronjonp

  • Needs a day job
  • Posts: 7527
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #3 on: August 21, 2014, 04:18:33 PM »
Mine:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun _fb (n)
  3.   (cond ((and (zerop (- (fix (/ n 3.)) (/ n 3.))) (zerop (- (fix (/ n 5.)) (/ n 5.)))) "FizzBuzz")
  4.         ((zerop (- (fix (/ n 3.)) (/ n 3.))) "Fizz")
  5.         ((zerop (- (fix (/ n 5.)) (/ (float n) 5.))) "Buzz")
  6.         (n)
  7.   )
  8. )(mapcar '_fb '(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))
  9.  
« Last Edit: August 21, 2014, 04:25:34 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #4 on: August 21, 2014, 04:23:41 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun FizzBuzz(n / )
  2.  (cond
  3.    ((and (= (/ n 3.) (/ n 3))(= (/ n 5.) (/ n 5))) "FizzBuzz")
  4.    ((= (/ n 3.) (/ n 3)) "Fizz")
  5.    ((= (/ n 5.) (/ n 5)) "Buzz")
  6.    (t n)
  7. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #5 on: August 21, 2014, 04:28:03 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:FB2 (n)
  2.   (if
  3.     (zerop (rem n 3))
  4.     (strcat
  5.       "Fizz"
  6.       (if
  7.         (zerop (rem n 5))
  8.         "Buzz"
  9.         ""
  10.       )
  11.     )
  12.     (if
  13.       (zerop (rem n 5))
  14.       "Buzz"
  15.       n
  16.     )
  17.   )
  18. )

Code - Auto/Visual Lisp: [Select]
  1. (defun ph:FB4 (n)
  2.   (cond
  3.     ((zerop (rem n 15)) "FizzBuzz")
  4.     ((zerop (rem n  5)) "Buzz")
  5.     ((zerop (rem n  3)) "Fizz")
  6.     (n)
  7.   )
  8. )

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.   )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #6 on: August 21, 2014, 04:51:03 PM »
; CAB modified
Code: [Select]
(defun FizzBuzz_A (n / f b)
  (setq f (= (/ n 3.) (/ n 3))  b (= (/ n 5.) (/ n 5)))
  (cond
    ( (and f b) "FizzBuzz")
    ( f "Fizz" )
    ( b "Buzz" )
    ( n )
  )
)
; from others...
Code: [Select]
(defun FizzBuzz_A2 (n / f b)
  (setq f (zerop (rem n 3))  b (zerop (rem n 5)))
  (cond
    ( (and f b) "FizzBuzz")
    ( f "Fizz" )
    ( b "Buzz" )
    ( n )
  )
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #7 on: August 21, 2014, 05:04:00 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun FizzBuzz (n)
  2.   (cond
  3.     ((zerop (rem n 15)) "FizzBuzz")
  4.     ((zerop (rem n 5)) "Buzz")
  5.     ((zerop (rem n 3)) "Fizz")
  6.     (n)
  7.   )
  8. )
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #8 on: August 21, 2014, 05:31:38 PM »
My variation  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun FizzBuzz (l / pick)
  2.   (defun pick (i / r)
  3.     (cond ((and (zerop (rem i 3)) (zerop (rem i 5))) (setq r "FizzBuzz"))
  4.           ((zerop (rem i 3)) (setq r "Fizz"))
  5.           ((zerop (rem i 5)) (setq r "Buzz"))
  6.           (t (setq r i)))
  7.     r
  8.   )
  9.   (if l (cons (pick (car l)) (FizzBuzz (cdr l))))
  10. )
  11.  

To test :

Code - Auto/Visual Lisp: [Select]
  1. (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))
  2.  

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #9 on: August 21, 2014, 06:07:53 PM »
Another, similar to those posted:
Code - Auto/Visual Lisp: [Select]
  1. (defun fizzbuzz ( n / r )
  2.     (setq r "")
  3.     (if (zerop (rem n 3)) (setq r "Fizz"))
  4.     (if (zerop (rem n 5)) (setq r (strcat r "Buzz")))
  5.     (if (= "" r) n r)
  6. )
  7.  

Just to be different:
Code - Auto/Visual Lisp: [Select]
  1. (defun fizzbuzz ( n )
  2.     (cdr
  3.         (assoc (mapcar '(lambda ( x ) (zerop (rem n x))) '(3 5))
  4.             (list
  5.                '((t  t ) . "FizzBuzz")
  6.                '((t nil) . "Fizz")
  7.                '((nil t) . "Buzz")
  8.                 (cons '(nil nil) n)
  9.             )
  10.         )
  11.     )
  12. )
« Last Edit: August 21, 2014, 06:11:27 PM by Lee Mac »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #10 on: August 21, 2014, 08:33:52 PM »
Just farting around and total noob but trying to get familiar with Vlide editor and just messing with it to rebuild  the list.
Code - Auto/Visual Lisp: [Select]
  1. (defun bizzFizz (lst / num)
  2.   (setq num (car lst))
  3.   (cond
  4.     ((equal '() lst) nil)
  5.     ((zerop (rem num 15)) (cons "FizzBuzz" (bizzFizz (cdr lst))))
  6.     ((zerop (rem num 3)) (cons "Fizz" (bizzFizz (cdr lst))))
  7.     ((zerop (rem num 5)) (cons "Buzz" (bizzFizz (cdr lst))))
  8.     (T (cons num (bizzFizz (cdr lst))))
  9.   )
  10. )
  11.  

Jeff H

  • Needs a day job
  • Posts: 6150
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #11 on: August 21, 2014, 08:35:56 PM »
Speaking of which is there a 'endp' in AutoLisp or a predicate to test if a empty list?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #12 on: August 21, 2014, 08:56:37 PM »
Here is a different approach

Code - Auto/Visual Lisp: [Select]
  1. (defun fb (n)
  2.   (nth (logior (min (rem n 3) 1) (lsh (min (rem n 5) 1) 1))
  3.        (list "FizzBuzz" "Buzz" "Fizz" n)
  4.   )
  5. )
« Last Edit: August 21, 2014, 11:02:39 PM by Keith™ »
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #13 on: August 21, 2014, 09:38:05 PM »
Speaking of which is there a 'endp' in AutoLisp or a predicate to test if a empty list?


Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq emptylist '())
  3.  
  4.  
  5. (if (not emptylist)
  6.   (alert "Blah\nBlah")
  7. )
  8. (if  emptylist
  9.   (alert "Blah\nBlah\nBlah")
  10.   ;;else
  11.   (alert "The list empty list equates to nil")
  12. )
  13.  
  14.  
  15. (if (listp emptylist)
  16.   (alert
  17.     "Danger Will Robinson\nAn empty list is still a list\nIn fact, nil is a list ..."
  18.   )
  19. )
  20.  
  21. (if (listp nil)
  22.   (alert "See ... ")
  23. )
  24.  
« Last Edit: August 21, 2014, 09:41:26 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #14 on: August 21, 2014, 10:37:55 PM »

Bench the routines posted.

Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------
  2. ;;;
  3. (defun benchmark ;;=================================================================
  4.                  ;;
  5.                  ;;  Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
  6.                  ;;
  7.                  ;; Mods by KWB 2005  Ordered with fastest as relative speed 1.00
  8.                  ;;=================================================================
  9.                  ;;
  10.                  ;;  Purpose:
  11.                  ;;
  12.                  ;;      Compare the performance of various statements.
  13.                  ;;
  14.                  ;;  Notes:
  15.                  ;;
  16.                  ;;      I make no claims that this is definitive benchmarking. I
  17.                  ;;      wrote this utility for my own purposes and thought I'd
  18.                  ;;      share it. Many considerations go into evaluating the
  19.                  ;;      performance or suitability of an algorythm for a given
  20.                  ;;      task. Raw performance as profiled herein is just one.
  21.                  ;;
  22.                  ;;      Please note that background dramatically affect results.
  23.                  ;;
  24.                  ;;  Disclaimer:
  25.                  ;;
  26.                  ;;      This program is flawed in one or more ways and is not fit
  27.                  ;;      for any particular purpose, stated or implied. Use at your
  28.                  ;;      own risk.
  29.                  ;;
  30.                  ;;=================================================================
  31.                  ;;
  32.                  ;;  Syntax:
  33.                  ;;
  34.                  ;;      (Benchmark statements)
  35.                  ;;
  36.                  ;;          Where statements is a quoted list of statements.
  37.                  ;;
  38.                  ;;=================================================================
  39.                  ;;
  40.                  ;;  Example:
  41.                  ;;
  42.                  ;;      (BenchMark
  43.                  ;;         '(
  44.                  ;;              (1+ 1)
  45.                  ;;              (+ 1 1)
  46.                  ;;              (+ 1 1.0)
  47.                  ;;              (+ 1.0 1.0)
  48.                  ;;          )
  49.                  ;;      )
  50.                  ;;
  51.                  ;;=================================================================
  52.                  ;;
  53.                  ;;  Output:
  54.                  ;;
  55.                  ;;      Elapsed milliseconds / relative speed for 32768 iteration(s):
  56.                  ;;
  57.                  ;;    (+ 1 1.0).......1452 / 1.1152 <slowest>
  58.                  ;;    (+ 1.0 1.0).....1412 / 1.0845
  59.                  ;;    (+ 1 1).........1332 / 1.023
  60.                  ;;    (1+ 1)..........1302 / 1 <fastest>
  61.                  ;;
  62.                  ;; Mods by KWB 2005
  63.                  ;;
  64.                  ;;=================================================================
  65.                  (statements / _lset _rset _tostring _eval _princ _main)
  66.   ;;=================================================================
  67.   ;;
  68.   ;;  (_LSet text len fillChar)
  69.   ;;
  70.   ;;=================================================================
  71.   (defun _lset (text len fillchar / padding result)
  72.     (setq padding (list (ascii fillchar))
  73.           result  (vl-string->list text)
  74.     )
  75.     (while (< (length (setq padding (append padding padding))) len))
  76.     (while (< (length (setq result (append result padding))) len))
  77.     (substr (vl-list->string result) 1 len)
  78.   )
  79.   ;;=================================================================
  80.   ;;
  81.   ;;  (_RSet text len fillChar)
  82.   ;;
  83.   ;;=================================================================
  84.   (defun _rset (text len fillchar / padding result)
  85.     (setq padding (list (ascii fillchar))
  86.           result  (vl-string->list text)
  87.     )
  88.     (while (< (length (setq padding (append padding padding))) len))
  89.     (while (< (length (setq result (append padding result))) len))
  90.     (substr (vl-list->string result) (1+ (- (length result) len)))
  91.   )
  92.   ;;=================================================================
  93.   ;;
  94.   ;;  (_ToString x)
  95.   ;;
  96.   ;;=================================================================
  97.   (defun _tostring (x / result)
  98.     (if (< (strlen (setq result (vl-prin1-to-string x))) 40)
  99.       result
  100.       (strcat (substr result 1 36) "..." (chr 41))
  101.     )
  102.   )
  103.   ;;=================================================================
  104.   ;;
  105.   ;;  (_Eval statement iterations)
  106.   ;;
  107.   ;;=================================================================
  108.   (defun _eval (statement iterations / start)
  109.     (gc)
  110.     (setq start (getvar "millisecs"))
  111.     (repeat iterations (eval statement))
  112.     (- (getvar "millisecs") start)
  113.   )
  114.   ;;=================================================================
  115.   ;;
  116.   ;;  (_Princ x)
  117.   ;;
  118.   ;;=================================================================
  119.   (defun _princ (x)
  120.     (princ x)
  121.     (princ)
  122.     ;; forces screen update
  123.   )
  124.   ;;=================================================================
  125.   ;;
  126.   ;;  (_Main statements)
  127.   ;;
  128.   ;;=================================================================
  129.   (defun _main (statements /
  130.                 boundary   iterations
  131.                 timings    slowest
  132.                 fastest    lsetlen
  133.                 rsetlen    index
  134.                 count
  135.                )
  136.     (setq boundary 200                       ; 1000
  137.           iterations 1
  138.     )
  139.     (_princ "Benchmarking [M.P. 2005 < revised kdub 2005>] ...")
  140.     (while (or (< (apply 'max
  141.                          (setq timings
  142.                                 (mapcar '(lambda (statement) (_eval statement iterations))
  143.                                         statements
  144.                                 )
  145.                          )
  146.                   )
  147.                   boundary
  148.                )
  149.                (< (apply 'min timings) boundary)
  150.            )
  151.       (setq iterations (* 2 iterations))
  152.       (_princ ".")
  153.     )
  154.     (_princ (strcat "\nElapsed milliseconds for "
  155.                     (itoa iterations)
  156.                     " iteration(s)"
  157.                     "/ relative Timing :\n\n"
  158.             )
  159.     )
  160.     (setq slowest (float (apply 'max timings))
  161.           fastest (float (apply 'min timings))
  162.     )
  163.     (setq lsetlen
  164.            (+ 5
  165.               (apply 'max
  166.                      (mapcar 'strlen
  167.                              (setq statements (mapcar '_tostring statements))
  168.                      )
  169.               )
  170.            )
  171.     )
  172.     (setq
  173.       rsetlen (apply 'max (mapcar '(lambda (ms) (strlen (itoa ms))) timings))
  174.     )
  175.     (setq index 0
  176.           count (length statements)
  177.     )
  178.     (foreach pair (vl-sort (mapcar 'cons statements timings)
  179.                            '(lambda (a b) (> (cdr a) (cdr b)))
  180.                   )
  181.       ((lambda (pair / ms)
  182.          (_princ (strcat "    "
  183.                          (_lset (car pair) lsetlen ".")
  184.                          (_rset (itoa (setq ms (cdr pair))) rsetlen ".")
  185.                          " / "
  186.                          (rtos (/ ms fastest) 2 4)
  187.                          (cond ((eq 1 (setq index (1+ index))) " <slowest>")
  188.                                ((eq index count) " <fastest>")
  189.                                ("")
  190.                          )
  191.                          "\n"
  192.                  )
  193.          )
  194.        )
  195.         pair
  196.       )
  197.     )
  198.     (princ)
  199.   )
  200.   ;;=================================================================
  201.   ;;
  202.   ;;  Program is defined, let's rock and roll ...
  203.   ;;
  204.   ;;=================================================================
  205.   (_main statements)
  206. )
  207.  
  208.  

Code - Auto/Visual Lisp: [Select]
  1. (setq FizzList '(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))
  2.  
  3.       (benchmark
  4.         '((mapcar 'ronjonp:_fb fizzlist)
  5.           (mapcar 'cab:fizzbuzz fizzlist)
  6.           (mapcar 'ph:fb2 fizzlist)
  7.           (mapcar 'ph:fb4 fizzlist)
  8.           (mapcar 'ph:fb5 fizzlist)
  9.           (mapcar 'fizzbuzz_a fizzlist)
  10.           (mapcar 'fizzbuzz_a2 fizzlist)
  11.           (mapcar 'kb:fizzbuzz fizzlist)
  12.           (tharwat:fizzbuzz fizzlist)
  13.           (mapcar 'lm:fizzbuzz1 fizzlist)
  14.           (mapcar 'lm:fizzbuzz2 fizzlist)
  15.           (jh:bizzfizz fizzlist)
  16.           ;(* 1 1)
  17.          )
  18.       )
  19.  
  20.  


_1$
Benchmarking [M.P. 2005 < revised kdub 2005>] ...............
Elapsed milliseconds for 4096 iteration(s)/ relative Timing :

    (MAPCAR (QUOTE LM:FIZZBUZZ2) FIZZLIST).....780 / 3.8424 <slowest>
    (MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)......546 / 2.6897
    (THARWAT:FIZZBUZZ FIZZLIST)................514 / 2.5320
    (MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST).....344 / 1.6946
    (JH:BIZZFIZZ FIZZLIST).....................328 / 1.6158
    (MAPCAR (QUOTE FIZZBUZZ_A) FIZZLIST).......297 / 1.4631
    (MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST).....297 / 1.4631
    (MAPCAR (QUOTE KB:FIZZBUZZ) FIZZLIST)......266 / 1.3103
    (MAPCAR (QUOTE PH:FB2) FIZZLIST)...........265 / 1.3054
    (MAPCAR (QUOTE PH:FB4) FIZZLIST)...........265 / 1.3054
    (MAPCAR (QUOTE FIZZBUZZ_A2) FIZZLIST)......249 / 1.2266
    (MAPCAR (QUOTE PH:FB5) FIZZLIST)...........203 / 1.0000 <fastest>
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.