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

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Using only AutoLisp, replicate the elements of a list a given number of times.

Example:
(repli '(a b c) 3)
> (A A A B B B C C C)
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
Code - Auto/Visual Lisp: [Select]
  1. (defun repli (l n / f)
  2.   (defun f (l i)
  3.     (cond
  4.       ((null l) l)
  5.       ((<= i 0) (f (cdr l) n))
  6.       (T (cons (car l) (f l (1- i))))
  7.     )
  8.   )
  9.   (f l n)
  10. )

Code: [Select]
_$ (repli '(a b c) 3)
(A A A B B B C C C)
_$ (repli '(a b c) 1)
(A B C)
_$ (repli '(a b c) 0)
nil
_$ (repli '(a b c) -1)
nil
« Last Edit: January 12, 2022, 09:56:15 AM by gile »
Speaking English as a French Frog

bruno_vdh

  • Newt
  • Posts: 31
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))
      )
)
)
  )
)

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Code - Auto/Visual Lisp: [Select]
  1. (defun repli-se7en ( aList quanity / naList)
  2.   (if (zerop quanity)
  3.     aList
  4.     (progn
  5.       (foreach x aList
  6.                (repeat quanity
  7.                        (set 'naList (cons x naList))))
  8.       (reverse naList))) )
  9.  

Code: [Select]
(repli-se7en '(a b c) 3)
> (A A A B B B C C C)

(repli-se7en '(a b c) nil)
> ; error: bad argument type: numberp: nil

(repli-se7en '(a b c) -1)
> nil

(repli-se7en '(a b c) 0)
> (A B C)

Good call on the -1 gile.

mine: if quantity >= 0 return the list. If quantity !number || < 0 return error.
« Last Edit: January 12, 2022, 10:06:54 AM by JohnK »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

bruno_vdh

  • Newt
  • Posts: 31
_$ (repli '(a b c) 0)
nil
_$ (repli '(a b c) -1)
nil[/code]

Ok, corrected version
Code: [Select]
(defun repli_vdh (l n / m)
  (if (> n 0)
    (apply 'append
   (apply 'mapcar
  (cons 'list
(repeat n
  (setq m (cons l m))
)
  )
   )
    )
  )
)

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
mine: if quantity >= 0 return the list

IMO, it should return nil when n == 0 (n * 0 = 0)
Code: [Select]
_$ (repli '(a b c) 3)
(A A A B B B C C C)
_$ (repli '(a b c) 2)
(A A B B C C)
_$ (repli '(a b c) 1)
(A B C)
_$ (repli '(a b c) 0)
nil
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 645
  • Hypersensitive
For the sake of unposted way.  :laugh:
Code - Auto/Visual Lisp: [Select]
  1. (defun Repli-Tharwat ( lst num / new rtn )
  2.   (or (zerop num)
  3.       (foreach itm lst
  4.         (while (not (= (length new) num))
  5.           (setq new (cons itm new))
  6.           )
  7.         (setq rtn (cons new rtn)
  8.               new nil)
  9.         )
  10.       )
  11.   (if rtn (reverse (apply 'append rtn)) lst)
  12.   )

kirby

  • Newt
  • Posts: 57
One more.... with benefit of coming late to the party and catching the problem of nil or negative repetitions

Code - Auto/Visual Lisp: [Select]
  1. (defun Replicate (MyList NumRep / OutList CNT MyItem1)
  2. ; Challenge A12 : Replicate the elements of a list a specified number of times
  3. ; e.g. (Replicate '(a b c) 3)
  4. ; output (a a a b b b c c c)
  5. (if (or (eq NumRep nil) (< NumRep 0))
  6.         (setq NumRep 0)         ; correct nil or negative reps
  7. )      
  8.  
  9. (setq OutList nil)
  10.  
  11. (if (and MyList (eq (type MyList) 'LIST))
  12.   (progn
  13.         (setq CNT 0)
  14.         (repeat (length MyList)
  15.                 (setq MyItem (nth CNT MyList))
  16.                
  17.                 (repeat NumRep
  18.                         (setq OutList (cons MyItem OutList))
  19.                 ) ; close repeat
  20.        
  21.                 (setq CNT (1+ CNT))
  22.         ) ; close repeat
  23.  
  24.         (setq OutList (reverse OutList))
  25.   )
  26. ) ; close if
  27. OutList
  28. )
  29.  

dexus

  • Newt
  • Posts: 27
Here is my version based on the previous challenge.
Code - Auto/Visual Lisp: [Select]
  1. (defun repli-dexus2 (l x / r i)
  2.   (while l
  3.     (setq i (car l) l (cdr l))
  4.     (repeat x (setq r (cons i r)))
  5.   )
  6.   (reverse r)
  7. )

Code: [Select]
-1: nil
 0: nil
 1: (A B C)
 2: (A A B B C C)
« Last Edit: January 12, 2022, 11:28:44 AM by dexus »

bruno_vdh

  • Newt
  • Posts: 31
Another...

Code: [Select]
(defun repli_vdh_2 (l n)
  (if l
    ((lambda (x l)
       (repeat n
(setq l (cons x l))
       )
     )
      (car l)
      (repli_vdh_2 (cdr l) n)
    )
  )
)

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #10 on: January 12, 2022, 11:33:35 AM »
Another...

Code: [Select]
(defun repli_vdh_2 (l n)
  (if l
    ((lambda (x l)
       (repeat n
(setq l (cons x l))
       )
     )
      (car l)
      (repli_vdh_2 (cdr l) n)
    )
  )
)
Pretty one, Bruno !
Speaking English as a French Frog

Stefan

  • Bull Frog
  • Posts: 288
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #11 on: January 12, 2022, 11:53:09 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun repli-stef (l n / r)
  2.   (foreach x (reverse l)
  3.     (repeat n
  4.       (setq r (cons x r))
  5.     )
  6.   )
  7. )

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #12 on: January 12, 2022, 12:32:09 PM »
I cannot resist. F# version:
Code - F#: [Select]
  1. let dupli n l =
  2.     [ for x in l do
  3.           for _ in 1 .. n -> x ]
« Last Edit: January 12, 2022, 12:43:16 PM by gile »
Speaking English as a French Frog

bruno_vdh

  • Newt
  • Posts: 31
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #13 on: January 12, 2022, 12:33:05 PM »
Pretty one, Bruno !
Thanks (gile), I had a good master :wink:

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Re: [challenge] A12 : Replicate the elements of a list a given number of times
« Reply #14 on: January 12, 2022, 02:10:01 PM »
Fixed (I can see that argument):
Code - Auto/Visual Lisp: [Select]
  1. (defun repli-se7en ( aList quanity / naList)
  2.   (if (not (zerop quanity))
  3.     (progn
  4.       (foreach x aList
  5.                (repeat quanity
  6.                        (set 'naList (cons x naList))))
  7.       (reverse naList))) )

mine: if quantity >= 0 return the list

IMO, it should return nil when n == 0 (n * 0 = 0)
Code: [Select]
_$ (repli '(a b c) 3)
(A A A B B B C C C)
_$ (repli '(a b c) 2)
(A A B B C C)
_$ (repli '(a b c) 1)
(A B C)
_$ (repli '(a b c) 0)
nil
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org