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

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #45 on: August 27, 2014, 09:18:21 AM »
While strictly speaking Kerry is correct if read to my OP, I feel ElpanovEvgeniy's code is acceptable in the spirit of the test. Especially since it works for most the original wordings when it is stated as a programming test:

"An algorithm to print the 1st 100 positive integers replacing each divisible by 3 with Fizz, each divisible by 5 by Buzz and each divisible by both 3 and 5 by FizzBuzz".

It doesn't state what inputs, though the understanding is that it has no inputs. Most solutions are not even using a function, but rather just looping inside the main program entry-point.

Anyhow, my OP was more intended to show a function which acts like a player in the FizzBuzz game: http://en.wikipedia.org/wiki/Fizz_buzz
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 377
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #46 on: August 28, 2014, 02:26:50 AM »
Nice challenge Irné , Couldn't have chosen a better place to post it.

BTW: the code i posted trumps anyone else's in terms of speed  :lmao:

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #47 on: August 28, 2014, 05:49:09 AM »
Nice challenge Irné , Couldn't have chosen a better place to post it.
Don't I know it! The level of abilities here at the swamp is way higher than in some other places!

Anyhow trying the imperative version of eea ... I thought using append might degrade performance, but it seems not too bad:
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:FizzBuzzList  (L / band result)
  2.   (setq band '(nil nil "Fizz" nil "Buzz" "Fizz" nil nil "Fizz" "Buzz" nil "Fizz" nil nil "FizzBuzz"))
  3.   (while L
  4.     (setq result (append result (mapcar (function (lambda (a b) (cond (a) (b)))) band L))
  5.           L (cddddr (cddddr (cddddr (cdddr L))))))
  6.   result)

Of course benchmarking only the calculation ... not benchmarking the generation of the list from 1 to N (which is unnecessary for the other functions) might skew it to eea and mine. And it does seem to be the case (as I'd expect).

Code: [Select]
Benchmarking on numbers from 1 to 30.
Benchmarking ............................................ done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(MAPCAR (QUOTE IB:FIZZBUZZ3) FIZZLIST)       16384      1918      1918      1.00
(IB:FIZZBUZZLIST FIZZLIST)                   16384      1919      1919      1.00
(EEA FIZZLIST)                               16384      1919      1919      1.00
(EEA FIZZLIST)                               16384      1935      1935      1.01
(MAPCAR (QUOTE IB:FIZZBUZZ3) FIZZLIST)       16384      1950      1950      1.02
(IB:FIZZBUZZLIST FIZZLIST)                   16384      1966      1966      1.03
(MAPCAR (QUOTE PH:FB5) FIZZLIST)             16384      1982      1982      1.03
(MAPCAR (QUOTE IB:FIZZBUZZ1) FIZZLIST)        8192      1014      2028      1.06
(MAPCAR (QUOTE PH:FB5) FIZZLIST)             16384      2028      2028      1.06
(MAPCAR (QUOTE IB:FIZZBUZZ1) FIZZLIST)        8192      1045      2090      1.09
(MAPCAR (QUOTE PH:FB2) FIZZLIST)              8192      1139      2278      1.19
(MAPCAR (QUOTE PH:FB2) FIZZLIST)              8192      1170      2340      1.22
(MAPCAR (QUOTE IB:FIZZBUZZ0) FIZZLIST)        8192      1232      2464      1.28
(MAPCAR (QUOTE IB:FIZZBUZZ0) FIZZLIST)        8192      1232      2464      1.28
(MAPCAR (QUOTE KERRY:FIZZBUZZ) FIZZL...)      8192      1264      2528      1.32
(MAPCAR (QUOTE KERRY:FIZZBUZZ) FIZZL...)      8192      1264      2528      1.32
(MAPCAR (QUOTE PH:FB4) FIZZLIST)              8192      1279      2558      1.33
(MAPCAR (QUOTE MAA:FIZZBUZZ_A2) FIZZ...)      8192      1294      2588      1.35
(MAPCAR (QUOTE IB:FIZZBUZZ) FIZZLIST)         8192      1295      2590      1.35
(MAPCAR (QUOTE PH:FB4) FIZZLIST)              8192      1295      2590      1.35
(MAPCAR (QUOTE IB:FIZZBUZZ) FIZZLIST)         8192      1326      2652      1.38
(MAPCAR (QUOTE MAA:FIZZBUZZ_A2) FIZZ...)      8192      1326      2652      1.38
(MAPCAR (QUOTE LM:FIZZBUZZ) FIZZLIST)         8192      1373      2746      1.43
(MAPCAR (QUOTE LM:FIZZBUZZ) FIZZLIST)         8192      1389      2778      1.45
(MAPCAR (QUOTE IB:FIZZBUZZ2) FIZZLIST)        8192      1419      2838      1.48
(MAPCAR (QUOTE IB:FIZZBUZZ2) FIZZLIST)        8192      1436      2872      1.50
(JH:BIZZFIZZ FIZZLIST)                        8192      1513      3026      1.58
(MAPCAR (QUOTE KEITH:FB) FIZZLIST)            8192      1513      3026      1.58
(MAPCAR (QUOTE MAA:FIZZBUZZ_A) FIZZL...)      8192      1529      3058      1.59
(JH:BIZZFIZZ FIZZLIST)                        8192      1544      3088      1.61
(MAPCAR (QUOTE KEITH:FB) FIZZLIST)            8192      1575      3150      1.64
(MAPCAR (QUOTE MAA:FIZZBUZZ_A) FIZZL...)      8192      1622      3244      1.69
(MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST)        8192      1654      3308      1.72
(MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST)        8192      1716      3432      1.79
(MAPCAR (QUOTE CHRISCARLSON:FIZZFUZZ...)      8192      1950      3900      2.03
(THARWAT:FIZZBUZZ FIZZLIST)                   8192      1965      3930      2.05
(MAPCAR (QUOTE CHRISCARLSON:FIZZFUZZ...)      4096      1014      4056      2.11
(THARWAT:FIZZBUZZ FIZZLIST)                   4096      1030      4120      2.15
(MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)         4096      1123      4492      2.34
(MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)         4096      1124      4496      2.34
(MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST)        2048      1670     13360      6.97
(MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST)        2048      1716     13728      7.16
(MAPCAR (QUOTE IB:FIZZBUZZ4) FIZZLIST)        1024      1154     18464      9.63
(MAPCAR (QUOTE IB:FIZZBUZZ4) FIZZLIST)        1024      1185     18960      9.89
--------------------------------------------------------------------------------

But it seems that both eea and my imperative alternative perform a bit better on the 1st 100 integers.
Code: [Select]
Benchmarking on numbers from 1 to 100.
Benchmarking ............................................ done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(EEA FIZZLIST)                               16384      1576      1576      1.00
(EEA FIZZLIST)                               16384      1591      1591      1.01
(IB:FIZZBUZZLIST FIZZLIST)                   16384      1623      1623      1.03
(IB:FIZZBUZZLIST FIZZLIST)                   16384      1623      1623      1.03
(MAPCAR (QUOTE PH:FB5) FIZZLIST)             16384      1763      1763      1.12
(MAPCAR (QUOTE PH:FB5) FIZZLIST)             16384      1763      1763      1.12
(MAPCAR (QUOTE IB:FIZZBUZZ3) FIZZLIST)       16384      1779      1779      1.13
(MAPCAR (QUOTE IB:FIZZBUZZ3) FIZZLIST)       16384      1779      1779      1.13
(MAPCAR (QUOTE IB:FIZZBUZZ1) FIZZLIST)       16384      1966      1966      1.25
(MAPCAR (QUOTE IB:FIZZBUZZ1) FIZZLIST)       16384      1981      1981      1.26
(MAPCAR (QUOTE IB:FIZZBUZZ0) FIZZLIST)        8192      1139      2278      1.45
(MAPCAR (QUOTE IB:FIZZBUZZ0) FIZZLIST)        8192      1139      2278      1.45
(MAPCAR (QUOTE IB:FIZZBUZZ) FIZZLIST)         8192      1264      2528      1.60
(MAPCAR (QUOTE IB:FIZZBUZZ) FIZZLIST)         8192      1264      2528      1.60
(MAPCAR (QUOTE MAA:FIZZBUZZ_A2) FIZZ...)      8192      1279      2558      1.62
(MAPCAR (QUOTE PH:FB4) FIZZLIST)              8192      1279      2558      1.62
(MAPCAR (QUOTE PH:FB2) FIZZLIST)              8192      1279      2558      1.62
(MAPCAR (QUOTE PH:FB2) FIZZLIST)              8192      1279      2558      1.62
(MAPCAR (QUOTE KERRY:FIZZBUZZ) FIZZL...)      8192      1280      2560      1.62
(MAPCAR (QUOTE MAA:FIZZBUZZ_A2) FIZZ...)      8192      1280      2560      1.62
(MAPCAR (QUOTE KERRY:FIZZBUZZ) FIZZL...)      8192      1295      2590      1.64
(MAPCAR (QUOTE PH:FB4) FIZZLIST)              8192      1295      2590      1.64
(MAPCAR (QUOTE LM:FIZZBUZZ) FIZZLIST)         8192      1544      3088      1.96
(MAPCAR (QUOTE LM:FIZZBUZZ) FIZZLIST)         8192      1545      3090      1.96
(MAPCAR (QUOTE MAA:FIZZBUZZ_A) FIZZL...)      8192      1575      3150      2.00
(MAPCAR (QUOTE MAA:FIZZBUZZ_A) FIZZL...)      8192      1591      3182      2.02
(MAPCAR (QUOTE IB:FIZZBUZZ2) FIZZLIST)        8192      1701      3402      2.16
(MAPCAR (QUOTE IB:FIZZBUZZ2) FIZZLIST)        8192      1716      3432      2.18
(MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST)        8192      1794      3588      2.28
(JH:BIZZFIZZ FIZZLIST)                        8192      1825      3650      2.32
(JH:BIZZFIZZ FIZZLIST)                        8192      1825      3650      2.32
(MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST)        8192      1841      3682      2.34
(MAPCAR (QUOTE KEITH:FB) FIZZLIST)            4096      1092      4368      2.77
(MAPCAR (QUOTE KEITH:FB) FIZZLIST)            4096      1092      4368      2.77
(MAPCAR (QUOTE CHRISCARLSON:FIZZFUZZ...)      4096      1202      4808      3.05
(THARWAT:FIZZBUZZ FIZZLIST)                   4096      1388      5552      3.52
(THARWAT:FIZZBUZZ FIZZLIST)                   4096      1388      5552      3.52
(MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)         4096      1529      6116      3.88
(MAPCAR (QUOTE CHRISCARLSON:FIZZFUZZ...)      4096      2247      8988      5.70
(MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)         2048      1170      9360      5.94
(MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST)        2048      1201      9608      6.10
(MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST)        2048      1217      9736      6.18
(MAPCAR (QUOTE IB:FIZZBUZZ4) FIZZLIST)        2048      1560     12480      7.92
(MAPCAR (QUOTE IB:FIZZBUZZ4) FIZZLIST)        2048      1560     12480      7.92
--------------------------------------------------------------------------------

Though my guess is that on larger lists it might go another way
Code: [Select]
Benchmarking on numbers from 1 to 1000.
Benchmarking ............................................ done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(MAPCAR (QUOTE IB:FIZZBUZZ3) FIZZLIST)        1024      1825      1825      1.00
(MAPCAR (QUOTE PH:FB5) FIZZLIST)              1024      1841      1841      1.01
(MAPCAR (QUOTE PH:FB5) FIZZLIST)              1024      1841      1841      1.01
(MAPCAR (QUOTE IB:FIZZBUZZ3) FIZZLIST)        1024      1857      1857      1.02
(MAPCAR (QUOTE IB:FIZZBUZZ1) FIZZLIST)         512      1029      2058      1.13
(MAPCAR (QUOTE IB:FIZZBUZZ1) FIZZLIST)         512      1030      2060      1.13
(MAPCAR (QUOTE PH:FB2) FIZZLIST)               512      1264      2528      1.39
(MAPCAR (QUOTE PH:FB2) FIZZLIST)               512      1264      2528      1.39
(MAPCAR (QUOTE IB:FIZZBUZZ0) FIZZLIST)         512      1466      2932      1.61
(MAPCAR (QUOTE IB:FIZZBUZZ0) FIZZLIST)         512      1467      2934      1.61
(MAPCAR (QUOTE PH:FB4) FIZZLIST)               512      1529      3058      1.68
(MAPCAR (QUOTE KERRY:FIZZBUZZ) FIZZL...)       512      1544      3088      1.69
(MAPCAR (QUOTE KERRY:FIZZBUZZ) FIZZL...)       512      1560      3120      1.71
(MAPCAR (QUOTE PH:FB4) FIZZLIST)               512      1576      3152      1.73
(MAPCAR (QUOTE MAA:FIZZBUZZ_A2) FIZZ...)       512      1638      3276      1.80
(MAPCAR (QUOTE IB:FIZZBUZZ) FIZZLIST)          512      1638      3276      1.80
(MAPCAR (QUOTE MAA:FIZZBUZZ_A2) FIZZ...)       512      1654      3308      1.81
(MAPCAR (QUOTE IB:FIZZBUZZ) FIZZLIST)          512      1669      3338      1.83
(MAPCAR (QUOTE LM:FIZZBUZZ) FIZZLIST)          512      1778      3556      1.95
(MAPCAR (QUOTE LM:FIZZBUZZ) FIZZLIST)          512      1778      3556      1.95
(MAPCAR (QUOTE IB:FIZZBUZZ2) FIZZLIST)         512      1856      3712      2.03
(MAPCAR (QUOTE IB:FIZZBUZZ2) FIZZLIST)         512      1887      3774      2.07
(MAPCAR (QUOTE MAA:FIZZBUZZ_A) FIZZL...)       256      1014      4056      2.22
(MAPCAR (QUOTE MAA:FIZZBUZZ_A) FIZZL...)       512      2044      4088      2.24
(MAPCAR (QUOTE KEITH:FB) FIZZLIST)             256      1045      4180      2.29
(MAPCAR (QUOTE KEITH:FB) FIZZLIST)             256      1045      4180      2.29
(MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST)         256      1123      4492      2.46
(MAPCAR (QUOTE CAB:FIZZBUZZ) FIZZLIST)         256      1154      4616      2.53
(JH:BIZZFIZZ FIZZLIST)                         256      1217      4868      2.67
(JH:BIZZFIZZ FIZZLIST)                         256      1232      4928      2.70
(EEA FIZZLIST)                                 256      1264      5056      2.77
(EEA FIZZLIST)                                 256      1264      5056      2.77
(IB:FIZZBUZZLIST FIZZLIST)                     256      1294      5176      2.84
(IB:FIZZBUZZLIST FIZZLIST)                     256      1310      5240      2.87
(MAPCAR (QUOTE CHRISCARLSON:FIZZFUZZ...)       256      1482      5928      3.25
(MAPCAR (QUOTE CHRISCARLSON:FIZZFUZZ...)       256      1513      6052      3.32
(THARWAT:FIZZBUZZ FIZZLIST)                    256      1685      6740      3.69
(THARWAT:FIZZBUZZ FIZZLIST)                    256      1685      6740      3.69
(MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)          256      1732      6928      3.80
(MAPCAR (QUOTE RONJONP:_FB) FIZZLIST)          256      1747      6988      3.83
(MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST)          64      1622     25952     14.22
(MAPCAR (QUOTE LM:FIZZBUZZ1) FIZZLIST)          64      1653     26448     14.49
(MAPCAR (QUOTE IB:FIZZBUZZ4) FIZZLIST)          32      1138     36416     19.95
(MAPCAR (QUOTE IB:FIZZBUZZ4) FIZZLIST)          32      1139     36448     19.97
--------------------------------------------------------------------------------
Not tested eea1 yet ... will do in due course.

Anyhow, here's my helper functions to perform the test.
Code - Auto/Visual Lisp: [Select]
  1. (defun Make-FizzBuzz-Result-List (size / result)
  2.   (repeat size
  3.     (setq result (cons (cond ((= (rem size 15) 0) "FizzBuzz")
  4.                              ((= (rem size 5) 0) "Buzz")
  5.                              ((= (rem size 3) 0) "Fizz")
  6.                              (size))
  7.                        result)
  8.           size (1- size)))
  9.   result)
  10.  
  11. (defun range (from to / result)
  12.   (while (>= to from)
  13.     (setq result (cons to result)
  14.           to (1- to)))
  15.   result)
  16.  
  17. (defun string-space  (str1 str2 fill len / s)
  18.   (setq s   str1
  19.         len (- len (strlen str2)))
  20.   (while (< (strlen s) len) (setq s (strcat s fill)))
  21.   (strcat s str2))
  22.  
  23. (defun test-fizzbuzz-map-show  (func size)
  24.   (print (vl-symbol-name func))
  25.   (setq func (eval func))
  26.   (vl-every '(lambda (n m) (print (cons (setq n (func n)) m)) (equal n m))
  27.             (range 1 size)
  28.             (Make-FizzBuzz-Result-List size)))
  29.  
  30. (defun test-fizzbuzz-map  (func size)
  31.   (string-space
  32.     (vl-symbol-name func)
  33.     (if (vl-every '(lambda (n m) (equal ((eval func) n) m)) (range 1 size)
  34.                   (Make-FizzBuzz-Result-List size))
  35.       "Pass"
  36.       "Fail")
  37.     "."
  38.     60))
  39.  
  40. (defun test-fizzbuzz-list-show  (func size)
  41.   (print (vl-symbol-name func))
  42.   (setq func (eval func))
  43.   (vl-every '(lambda (n m) (print (cons n m)) (equal n m))
  44.             (Make-FizzBuzz-Result-List size)
  45.             (func (range 1 size))))
  46.  
  47. (defun test-fizzbuzz-list  (func size)
  48.   (string-space
  49.     (vl-symbol-name func)
  50.     (cond ((equal ((eval func) (range 1 size)) (Make-FizzBuzz-Result-List size)) "Pass")
  51.           (t "Fail"))
  52.     "."
  53.     60))
  54.  
  55. (defun test-fizzbuzz  (size)
  56.   (foreach func  '(ChrisCarlson:FizzFuzzBuzz             ronjonp:_fb        CAB:FizzBuzz       ph:FB2
  57.                    ph:FB4             ph:FB5             MAA:FizzBuzz_A     MAA:FizzBuzz_A2    Kerry:FizzBuzz
  58.                    LM:fizzbuzz        LM:fizzbuzz1       Keith:fb           IB:FizzBuzz        IB:FizzBuzz0
  59.                    IB:FizzBuzz1       IB:FizzBuzz2       IB:FizzBuzz3       IB:FizzBuzz4)
  60.     (print (test-fizzbuzz-map func size)))
  61.   (foreach func '(Tharwat:FizzBuzz JH:bizzFizz eea IB:FizzBuzzList IB:FizzBuzzList1)
  62.     (print (test-fizzbuzz-list func size)))
  63.   (princ))
  64.  
  65. (defun bench-fizzbuzz  (size / fizzlist)
  66.   (setq fizzlist (range 1 size))
  67.   (princ "Benchmarking on numbers from 1 to ")
  68.   (princ size)
  69.   (princ ".\n")
  70.   (QBench '((mapcar 'ChrisCarlson:FizzFuzzBuzz fizzlist)
  71.             (mapcar 'ronjonp:_fb fizzlist)
  72.             (mapcar 'CAB:FizzBuzz fizzlist)
  73.             (mapcar 'ph:FB2 fizzlist)
  74.             (mapcar 'ph:FB4 fizzlist)
  75.             (mapcar 'ph:FB5 fizzlist)
  76.             (mapcar 'MAA:FizzBuzz_A fizzlist)
  77.             (mapcar 'MAA:FizzBuzz_A2 fizzlist)
  78.             (mapcar 'Kerry:FizzBuzz fizzlist)
  79.             (mapcar 'LM:fizzbuzz fizzlist)
  80.             (mapcar 'LM:fizzbuzz1 fizzlist)
  81.             (mapcar 'Keith:fb fizzlist)
  82.             (Tharwat:FizzBuzz fizzlist)
  83.             (JH:bizzFizz fizzlist)
  84.             (eea fizzlist)
  85.             (mapcar 'IB:FizzBuzz fizzlist)
  86.             (mapcar 'IB:FizzBuzz0 fizzlist)
  87.             (mapcar 'IB:FizzBuzz1 fizzlist)
  88.             (mapcar 'IB:FizzBuzz2 fizzlist)
  89.             (mapcar 'IB:FizzBuzz3 fizzlist)
  90.             (mapcar 'IB:FizzBuzz4 fizzlist)
  91.             (IB:FizzBuzzList fizzlist)
  92.             (mapcar 'ChrisCarlson:FizzFuzzBuzz fizzlist)
  93.             (mapcar 'ronjonp:_fb fizzlist)
  94.             (mapcar 'CAB:FizzBuzz fizzlist)
  95.             (mapcar 'ph:FB2 fizzlist)
  96.             (mapcar 'ph:FB4 fizzlist)
  97.             (mapcar 'ph:FB5 fizzlist)
  98.             (mapcar 'MAA:FizzBuzz_A fizzlist)
  99.             (mapcar 'MAA:FizzBuzz_A2 fizzlist)
  100.             (mapcar 'Kerry:FizzBuzz fizzlist)
  101.             (mapcar 'LM:fizzbuzz fizzlist)
  102.             (mapcar 'LM:fizzbuzz1 fizzlist)
  103.             (mapcar 'Keith:fb fizzlist)
  104.             (Tharwat:FizzBuzz fizzlist)
  105.             (JH:bizzFizz fizzlist)
  106.             (eea fizzlist)
  107.             (mapcar 'IB:FizzBuzz fizzlist)
  108.             (mapcar 'IB:FizzBuzz0 fizzlist)
  109.             (mapcar 'IB:FizzBuzz1 fizzlist)
  110.             (mapcar 'IB:FizzBuzz2 fizzlist)
  111.             (mapcar 'IB:FizzBuzz3 fizzlist)
  112.             (mapcar 'IB:FizzBuzz4 fizzlist)
  113.             (IB:FizzBuzzList fizzlist)))
  114.   (princ))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ur_naz

  • Newt
  • Posts: 65
  • Made in Ukraine
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #48 on: August 28, 2014, 12:19:28 PM »
The main problem of eea1 function is that it must accept the list with zero in the head. in other case it returns wrong result. but the task says clearly about positive value to accept :-P

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #49 on: August 29, 2014, 05:03:04 AM »
The main problem of eea1 function is that it must accept the list with zero in the head. in other case it returns wrong result. but the task says clearly about positive value to accept :-P

on my computer eea1 work correctly!

Code - Auto/Visual Lisp: [Select]
  1. (eea1 '(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. (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")
Stay home. Stay safe. Save lives.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1542
  • Moscow (Russia)
Re: =={challenge}== Programmer Test - Fizz/Buzz
« Reply #50 on: August 29, 2014, 05:08:29 AM »
Other two variants, using local and global variables.  8-)

Perhaps this trick is useful to you for your code...
 :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun eea2 (l)
  2.             (lambda (n)
  3.               (nth (rem n 15)
  4.                    (list "FizzBuzz" n n "Fizz" n "Buzz" "Fizz" n n "Fizz" "Buzz" n "Fizz" n n)
  5.               )
  6.             )
  7.           )
  8.           l
  9.   )
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (defun eea3 (l)
  2.             (lambda (n)
  3.               (eval
  4.                 (nth (rem n 15) '("FizzBuzz" n n "Fizz" n "Buzz" "Fizz" n n "Fizz" "Buzz" n "Fizz" n n))
  5.               )
  6.             )
  7.           )
  8.           l
  9.   )
  10. )
Stay home. Stay safe. Save lives.

ur_naz

  • Newt
  • Posts: 65
  • Made in Ukraine