Author Topic: [challenge] A12 : Replicate the elements of a list a given number of times  (Read 513 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12605
  • London, England
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #15 on: January 12, 2022, 06:12:01 PM »
Hello,
quickly my proposal
Code: [Select]
(defun repli_vdh (l n / m)
  (apply 'append
(apply 'mapcar
(cons 'list
      (repeat n
(setq m (cons l m))
      )
)
)
  )
)

Excellent variation Bruno, I like it.

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #16 on: January 13, 2022, 01:48:39 AM »
Hello,
quickly my proposal
Code: [Select]
(defun repli_vdh (l n / m)
  (apply 'append
(apply 'mapcar
(cons 'list
      (repeat n
(setq m (cons l m))
      )
)
)
  )
)

Excellent variation Bruno, I like it.

F# equivalent should be:
Code - F#: [Select]
  1. let dupli' n l =
  2.    l
  3.    |> List.replicate n
  4.    |> List.transpose
  5.    |> List.concat
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1566
  • Moscow (Russia)
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #17 on: January 13, 2022, 08:40:00 AM »
version - recursion using mapcar...
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l i / f)
  2.   (defun f (l i)
  3.     (cond ((> i 1) (mapcar 'cons l (f l (1- i))))
  4.           ((= i 1) (mapcar 'list l))
  5.           (t nil)
  6.     )
  7.   )
  8.   (apply 'append (f l i))
  9. )


dexus

  • Newt
  • Posts: 27
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #18 on: January 13, 2022, 11:44:28 AM »
Here we go for some benchmarks:
Code - Auto/Visual Lisp: [Select]
  1. ; dexus
  2. (defun repli-dexus2 (l x / r i)
  3.   (while l
  4.     (setq i (car l) l (cdr l))
  5.     (repeat x (setq r (cons i r)))
  6.   )
  7.   (reverse r)
  8. )
  9.  
  10. ;gile
  11. (defun repli-gile (l n / f)
  12.   (defun f (l i)
  13.     (cond
  14.       ((null l) l)
  15.       ((<= i 0) (f (cdr l) n))
  16.       (T (cons (car l) (f l (1- i))))
  17.     )
  18.   )
  19.   (f l n)
  20. )
  21.  
  22. ;bruno_vdh
  23. (defun repli_vdh (l n / m)
  24.   (if (> n 0)
  25.     (apply 'append
  26.      (apply 'mapcar
  27.       (cons 'list
  28.       (repeat n
  29.         (setq m (cons l m))
  30.       )
  31.       )
  32.      )
  33.     )
  34.   )
  35. )
  36.  
  37. ;Tharwat
  38. (defun Repli-Tharwat ( lst num / new rtn )
  39.   (or (zerop num)
  40.       (foreach itm lst
  41.         (while (not (= (length new) num))
  42.           (setq new (cons itm new))
  43.           )
  44.         (setq rtn (cons new rtn)
  45.               new nil)
  46.         )
  47.       )
  48.   (if rtn (reverse (apply 'append rtn)) lst)
  49.   )
  50.  
  51. ;kirby
  52. (defun Replicate-kirby (MyList NumRep / OutList CNT MyItem1)
  53.   ; Challenge A12 : Replicate the elements of a list a specified number of times
  54.   ; e.g. (Replicate '(a b c) 3)
  55.   ; output (a a a b b b c c c)
  56.   (if (or (eq NumRep nil) (< NumRep 0))
  57.           (setq NumRep 0)         ; correct nil or negative reps
  58.   )      
  59.    
  60.   (setq OutList nil)
  61.    
  62.   (if (and MyList (eq (type MyList) 'LIST))
  63.     (progn
  64.           (setq CNT 0)
  65.           (repeat (length MyList)
  66.                   (setq MyItem (nth CNT MyList))
  67.                  
  68.                   (repeat NumRep
  69.                           (setq OutList (cons MyItem OutList))
  70.                   ) ; close repeat
  71.          
  72.                   (setq CNT (1+ CNT))
  73.           ) ; close repeat
  74.    
  75.           (setq OutList (reverse OutList))
  76.     )
  77.   ) ; close if
  78.   OutList
  79. )
  80.      
  81. ;bruno_vdh
  82. (defun repli_vdh_2 (l n)
  83.   (if l
  84.     ((lambda (x l)
  85.       (repeat n
  86.         (setq l (cons x l))
  87.       )
  88.      )
  89.      (car l)
  90.      (repli_vdh_2 (cdr l) n)
  91.     )
  92.   )
  93. )
  94.  
  95. ;Stefan
  96. (defun repli-stef (l n / r)
  97.   (foreach x (reverse l)
  98.     (repeat n
  99.       (setq r (cons x r))
  100.     )
  101.   )
  102. )
  103.  
  104. ;JohnK
  105. (defun repli-se7en ( aList quanity / naList)
  106.   (if (not (zerop quanity))
  107.     (progn
  108.       (foreach x aList
  109.                (repeat quanity
  110.                        (set 'naList (cons x naList))))
  111.       (reverse naList))) )
  112.  
  113. ; ElpanovEvgeniy
  114. (defun f-ElpanovEvgeniy (l i / f)
  115.   (defun f (l i)
  116.     (cond ((> i 1) (mapcar 'cons l (f l (1- i))))
  117.           ((= i 1) (mapcar 'list l))
  118.           (t nil)
  119.     )
  120.   )
  121.   (apply 'append (f l i))
  122. )
  123.  
  124. (setq arrayToUse '(a b c))
  125. (setq numberToUse 10)
  126.  
  127. (benchmark '(
  128.         (repli-dexus2 arrayToUse numberToUse)
  129.         (repli-gile arrayToUse numberToUse)
  130.         (repli-se7en arrayToUse numberToUse)
  131.         (repli_vdh arrayToUse numberToUse)
  132.         (Repli-Tharwat arrayToUse numberToUse)
  133.         (Replicate-kirby arrayToUse numberToUse)
  134.         (repli_vdh_2 arrayToUse numberToUse)
  135.         (repli-stef arrayToUse numberToUse)
  136.         (repli-se7en arrayToUse numberToUse)
  137.         (f-ElpanovEvgeniy arrayToUse numberToUse)
  138. ))

Finally one where mine is high on the list  :yay!:
Code: [Select]
(REPLI-DEXUS2 ARRAYTOUSE NUMBERTOUSE).........1765 / 9.29 <fastest>
(REPLI_VDH_2 ARRAYTOUSE NUMBERTOUSE)..........1766 / 9.28
(REPLI-STEF ARRAYTOUSE NUMBERTOUSE)...........1766 / 9.28
(REPLI-SE7EN ARRAYTOUSE NUMBERTOUSE)..........1797 / 9.12
(REPLI-SE7EN ARRAYTOUSE NUMBERTOUSE)..........1797 / 9.12
(REPLICATE-KIRBY ARRAYTOUSE NUMBERTO...)......1812 / 9.05
(REPLI-GILE ARRAYTOUSE NUMBERTOUSE)...........1921 / 8.53
(REPLI-THARWAT ARRAYTOUSE NUMBERTOUSE)........3218 / 5.09
(REPLI_VDH ARRAYTOUSE NUMBERTOUSE)............5734 / 2.86
(F-ELPANOVEVGENIY ARRAYTOUSE NUMBERT...).....16390 / 1 <slowest>

bruno_vdh

  • Newt
  • Posts: 31
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #19 on: January 13, 2022, 03:53:59 PM »
version - recursion using mapcar...
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l i / f)
  2.   (defun f (l i)
  3.     (cond ((> i 1) (mapcar 'cons l (f l (1- i))))
  4.           ((= i 1) (mapcar 'list l))
  5.           (t nil)
  6.     )
  7.   )
  8.   (apply 'append (f l i))
  9. )

Hello,
I really like your proposal.
I was inspired by it to vary the formatting according to the sign of the increment

Code: [Select]
_$(f '(a b c) 3)
(A A A B B B C C C)
_$(f '(a b c) 0)
nil
_$(f '(a b c) -3)
((A A A) (B B B) (C C C))

Code: [Select]
(defun f (l i)
  (cond ((< i -1) (mapcar 'cons l (f l (1+ i))))
((= i -1) (mapcar 'list l))
((> i 0) (apply 'append (f l (- i))))
  )
)



ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1566
  • Moscow (Russia)
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #20 on: January 15, 2022, 11:43:38 AM »
(F-ELPANOVEVGENIY ARRAYTOUSE NUMBERT...).....16390 / 1 <slowest>

This is the best that I came up with without repeating the above written codes.  :cry:

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #21 on: January 15, 2022, 11:57:40 AM »
(F-ELPANOVEVGENIY ARRAYTOUSE NUMBERT...).....16390 / 1 <slowest>

This is the best that I came up with without repeating the above written codes.  :cry:
You are still number one in my eyes. :)
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Stefan

  • Bull Frog
  • Posts: 288
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #22 on: January 15, 2022, 01:55:46 PM »
(F-ELPANOVEVGENIY ARRAYTOUSE NUMBERT...).....16390 / 1 <slowest>

This is the best that I came up with without repeating the above written codes.  :cry:

Everything is relative.

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq arrayToUse '(a b c d e f g h i j k l m n o p q r s t u v w x y z))
  2.    (setq numberToUse 10)
  3. (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
  4. 5
  5. _$ (benchmark '(
  6.             (repli-dexus2 arrayToUse numberToUse)
  7.             (repli-gile arrayToUse numberToUse)
  8.             (repli-se7en arrayToUse numberToUse)
  9.             (repli_vdh arrayToUse numberToUse)
  10.             (Repli-Tharwat arrayToUse numberToUse)
  11.             (Replicate-kirby arrayToUse numberToUse)
  12.             (repli_vdh_2 arrayToUse numberToUse)
  13.             (repli-stef arrayToUse numberToUse)
  14.             (f-ElpanovEvgeniy arrayToUse numberToUse)
  15.     ))
  16. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  17.  
  18.     (REPLI_VDH ARRAYTOUSE NUMBERTOUSE)...........1171 / 3.06 <fastest>
  19.     (F-ELPANOVEVGENIY ARRAYTOUSE NUMBERT...).....1687 / 2.12
  20.     (REPLI-STEF ARRAYTOUSE NUMBERTOUSE)..........2734 / 1.31
  21.     (REPLI_VDH_2 ARRAYTOUSE NUMBERTOUSE).........2782 / 1.29
  22.     (REPLI-DEXUS2 ARRAYTOUSE NUMBERTOUSE)........2797 / 1.28
  23.     (REPLICATE-KIRBY ARRAYTOUSE NUMBERTO...).....3219 / 1.11
  24.     (REPLI-SE7EN ARRAYTOUSE NUMBERTOUSE).........3266 / 1.10
  25.     (REPLI-THARWAT ARRAYTOUSE NUMBERTOUSE).......3297 / 1.09
  26.     (REPLI-GILE ARRAYTOUSE NUMBERTOUSE)..........3578 / 1.00 <slowest>
  27.  

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1566
  • Moscow (Russia)
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #23 on: January 15, 2022, 02:08:17 PM »
Everything is relative.
You are still number one in my eyes. :)
:-o
Thank you !

bruno_vdh

  • Newt
  • Posts: 31
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #24 on: January 16, 2022, 11:24:27 AM »
Another, which memorizes the number of repetitions in the function definition.
Code: [Select]
(defun repli_vdh3 (l n / f)
  (defun f (n)
    (if (> n 0)
      (cons 'cons (cons 'x (list (f (1- n)))))
      (list 'f (list 'car 'l) (list 'cdr 'l))
    )
  )
  (eval (cons 'defun (cons 'f (list (list 'x 'l) (list 'if 'l (f n))))))
  (f (car l) (cdr l))
)
« Last Edit: January 16, 2022, 11:39:34 AM by bruno_vdh »