Author Topic: -={ Challenge }=- Group List by Number  (Read 19063 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: -={ Challenge }=- Group List by Number
« Reply #60 on: March 05, 2014, 09:39:24 AM »
Personally , I would like use
Code: [Select]
(vl-list* (cadddr l) (caddr l) (cadr l) (car l) a)to replace
Code: [Select]
(cons (cadddr l) (cons (caddr l) (cons (cadr l) (cons (car l) a))))

I think your code will run slower.
But I did not check...
 :-)
Stay home. Stay safe. Save lives.

LE3

  • Guest
Re: -={ Challenge }=- Group List by Number
« Reply #61 on: March 05, 2014, 10:33:35 AM »
I targeted all industries involving CAD design, but approached as a programmer (as I have very little experience in any particular field with my age and studies) - many said they were not interested, and others said they could not afford to take on anyone with the current financial climate... It would be a great job indeed to get paid to do what you love - you must be pretty happy Michael.

First time I read this old thread --- and for curiosity purposes (if you could answer it), do you work now as programmer for a company now?

kudos.

ur_naz

  • Newt
  • Posts: 66
  • Made in Ukraine
Re: -={ Challenge }=- Group List by Number
« Reply #62 on: March 08, 2014, 05:23:31 PM »
Very, very lazy GroupByNum...
Code - Auto/Visual Lisp: [Select]
  1. (defun firstn (n lst)
  2.   (if (< 0 n)
  3.     (cons (car lst) (firstn (1- n) (cdr lst)))))
  4.  
  5. (defun nthcdr (n lst)
  6.   (if (< n 0) lst (nthcdr (1- n) (cdr lst))))
  7.  
  8. (defun GroupByNum (n lst)
  9.   (if lst (cons (firstn n lst) (GroupByNum n (nthcdr n lst)))))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: -={ Challenge }=- Group List by Number
« Reply #63 on: March 10, 2014, 07:12:33 AM »
Be careful using recursion on long lists. You could run into stack overflows, especially since AutoLisp does not do tail-call otimization. Other than that good functional code, though firstn & GroupByNum are not tail recursive - so even if ALisp was optimizing it wouldn't work on these.

BTW, "Lazy" has slightly different connotation in some programming circles. Rather than being a way to describe slow running programs, it actually means the opposite: A lazy algorithm only calculates the results as and when needed, such that it uses the least amount of resources and takes only as little time as needed - then builds on its results afterwards without re-calculating for each new query.

Unfortunately a true lazy algorithm isn't possible in AutoLisp. We don't have access to reference calling on variables, not to mention an easy OOP system. But you can do something similar using a cached store of results - though I'm not sure it would be faster in all cases. Especially this one where each call could be on a totally different list and number combination.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

reltro

  • Guest
Re: -={ Challenge }=- Group List by Number
« Reply #64 on: March 10, 2014, 04:55:20 PM »
Hey...
maybe like this?

Code: [Select]
(defun-q GroupByNum (n L / F R)
    nil
    (if (not (setq F (cadr (assoc n (eval (nth 1 GroupByNum))))))
        (setq GroupByNum
            (subst
                (list 'quote
                    (cons
                        (list
                            n
                            (setq F
                                (vl-list*
                                    'list
                                    (quote (car L))
                                    (repeat (- n 1)
                                        (setq F
                                            (cons
                                                (quote (car (setq L (cdr L))))
                                                F
                                            )
                                        )
                                    )
                                )
                            )
                        )
                        (eval (nth 1 GroupByNum))
                    )
                )
                (nth 1 GroupByNum)
                GroupByNum
            )
        )
    )
   
    (while L
        (setq R (cons (eval F) R)
              L (cdr L)
        )
    )
   
    (reverse R)
)
« Last Edit: March 10, 2014, 05:02:23 PM by reltro »

chlh_jd

  • Guest
Re: -={ Challenge }=- Group List by Number
« Reply #65 on: March 12, 2014, 08:53:53 AM »
Hey...
maybe like this?

Code: [Select]
(defun-q GroupByNum (n L / F R)
    nil
    (if (not (setq F (cadr (assoc n (eval (nth 1 GroupByNum))))))
        (setq GroupByNum
            (subst
                (list 'quote
                    (cons
                        (list
                            n
                            (setq F
                                (vl-list*
                                    'list
                                    (quote (car L))
                                    (repeat (- n 1)
                                        (setq F
                                            (cons
                                                (quote (car (setq L (cdr L))))
                                                F
                                            )
                                        )
                                    )
                                )
                            )
                        )
                        (eval (nth 1 GroupByNum))
                    )
                )
                (nth 1 GroupByNum)
                GroupByNum
            )
        )
    )
   
    (while L
        (setq R (cons (eval F) R)
              L (cdr L)
        )
    )
   
    (reverse R)
)
Oh , another beautiful codes which is difficult to understand .
It also open my eyes , thank you , although it is slower .
« Last Edit: March 12, 2014, 09:35:44 AM by chlh_jd »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1199
  • Marco
Re: -={ Challenge }=- Group List by Number
« Reply #66 on: March 12, 2014, 04:24:24 PM »
another attempt
Code: [Select]
(defun ALE_GroupByNum3 (l n / a b x)
  (while l
    (repeat (/ n 10)
      (setq
        a (append a (list
            (car l) (cadr l) (caddr l) (cadddr l) (cadddr (cdr l)) (cadddr (cddr l))
            (cadddr (cdddr l)) (cadddr (cddddr l)) (cadddr (cddddr (cdr l)))
            (cadddr (cddddr (cddr l)))
          )         )
        l (cddddr(cddddr(cddr l)))
      )
    )
    (if (zerop (setq x (rem n 10)))
      (setq b (cons a b)  a nil)
      (setq b (cons (append a (First09 x l)) b)   l (Cdr09 x l)  a nil)
    )
  )
  (reverse b)
)
(defun First09 (n l)   
  (cond
    ( (eq 1 n) (list (car l))                                                )
    ( (eq 2 n) (list (car l) (cadr l))                                       )
    ( (eq 3 n) (list (car l) (cadr l) (caddr l))                             )
    ( (eq 4 n) (list (car l) (cadr l) (caddr l) (cadddr l))                  )
    ( (eq 5 n) (list (car l) (cadr l) (caddr l) (cadddr l) (cadddr (cdr l))) )
    ( (eq 6 n)
      (list (car l) (cadr l) (caddr l) (cadddr l) (cadddr (cdr l)) (cadddr (cddr l)))
    )
    ( (eq 7 n)
      (list
        (car l) (cadr l) (caddr l) (cadddr l) (cadddr (cdr l))
        (cadddr (cddr l)) (cadddr (cdddr l))
      )
    )
    ( (eq 8 n)
      (list
        (car l) (cadr l) (caddr l) (cadddr l) (cadddr (cdr l))
        (cadddr (cddr l)) (cadddr (cdddr l)) (cadddr (cddddr l))
      )
    )
    ( (eq 9 n)
      (list
        (car l) (cadr l) (caddr l) (cadddr l) (cadddr (cdr l)) (cadddr (cddr l))
        (cadddr (cdddr l)) (cadddr (cddddr l)) (cadddr (cddddr (cdr l)))
      )
    )
  )
)
(defun Cdr09 (n l)   
  (cond
    ( (eq 1 n) (cdr l) )
    ( (eq 2 n) (cddr l) )
    ( (eq 3 n) (cdddr l) )
    ( (eq 4 n) (cddddr l) )
    ( (eq 5 n) (cddddr(cdr l)) )
    ( (eq 6 n) (cddddr(cddr l)) )
    ( (eq 7 n) (cddddr(cdddr l)) )
    ( (eq 8 n) (cddddr(cddddr l)) )
    ( (eq 9 n) (cddddr(cddddr(cdr l))) )
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1199
  • Marco
Re: -={ Challenge }=- Group List by Number
« Reply #67 on: March 13, 2014, 01:50:58 PM »
Again, not faster!  :realmad:
Code: [Select]
(defun ALE_GroupByNum_L (l n / b f1 f2)
  (setq f1 (strcat "first" (itoa n))  f2 (strcat "cdr" (itoa n)))
  (or
    (eval (read f1))
    (progn
      ( (lambda (/ TmpCod OutLst NumElm)
          (setq NumElm n  OutLst nil)
          (repeat NumElm
            (setq TmpCod 'l)
            (repeat (/ (1- NumElm) 4) (setq TmpCod (cons 'cddddr (list TmpCod))))
            (repeat (rem (1- NumElm) 4) (setq TmpCod (cons 'cdr (list TmpCod))))
            (setq TmpCod (cons 'car (list TmpCod)) NumElm (1- NumElm)  OutLst  (cons TmpCod OutLst))
          )
          (eval
            (cons 'defun (cons (read f1) (cons '(l) (list (cons 'list OutLst)))))
          )
        )
      ) 
      ( (lambda (/ TmpCod)
          (setq TmpCod 'l)
          (repeat (/ n 4) (setq TmpCod (cons 'cddddr (list TmpCod))))
          (repeat (rem n 4) (setq TmpCod (cons 'cdr (list TmpCod))))
          (eval
            (cons 'defun (cons (read f2) (cons '(l) (list TmpCod))))
          )
        )
      )
    )
  )
  (while l
    (setq b (cons ((eval (read f1)) l) b)  l ((eval (read f2)) l))
  )
  (reverse b)
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1199
  • Marco
Re: -={ Challenge }=- Group List by Number
« Reply #68 on: March 13, 2014, 04:10:48 PM »
better... :)
Code: [Select]
(defun ALE_GroupByNum_L5 (l n / a f1 f2)
  (or
    (setq f1 (eval (read (strcat "First" (itoa n)))) f2 (eval (read (strcat "cdr" (itoa n)))))
    (progn
      (setq f1 (strcat "First" (itoa n))  f2 (strcat "cdr" (itoa n)))
      ( (lambda (/ TmpCod OutLst NumElm TmpNum)
          (setq NumElm n  OutLst nil)
          (repeat NumElm
            (setq TmpCod 'l  NumElm (1- NumElm)  TmpNum NumElm)
            (repeat (/ TmpNum 4)                       (setq TmpCod (cons 'cddddr (list TmpCod))))
            (repeat (/ (setq TmpNum (rem TmpNum 4)) 3) (setq TmpCod (cons 'cdddr  (list TmpCod))))
            (repeat (/ (setq TmpNum (rem TmpNum 3)) 2) (setq TmpCod (cons 'cddr   (list TmpCod))))
            (repeat (rem TmpNum 2) (setq TmpCod (cons 'cdr (list TmpCod))))
            (setq TmpCod (cons 'car (list TmpCod))   OutLst  (cons TmpCod OutLst))
          )
          (setq f1 (eval (eval (cons 'defun (cons (read f1) (cons '(l) (list (cons 'list OutLst))))))))
        )
      ) 
      ( (lambda (/ TmpCod)
          (setq TmpCod 'l)
          (repeat (/ n 4)                 (setq TmpCod (cons 'cddddr (list TmpCod))))
          (repeat (/ (setq n (rem n 4)) 3)(setq TmpCod (cons 'cdddr  (list TmpCod))))
          (repeat (/ (setq n (rem n 3)) 2)(setq TmpCod (cons 'cddr   (list TmpCod))))
          (repeat (rem n 2)               (setq TmpCod (cons 'cdr    (list TmpCod))))
          (setq f2 (eval (eval (cons 'defun (cons (read f2) (cons '(l) (list TmpCod)))))))
        )
      )
    )
  )
  (while (setq a (cons (f1 l) a)  l (f2 l)))
  (reverse a)
)