Code Red > AutoLISP (Vanilla / Visual)

Find the nest level of a list

(1/8) > >>

Grrr1337:
Hey guys,
Anyone wanna play?  :idea:

The task is to find the "deepest" nesting level of a list:


--- Code - Auto/Visual Lisp: ---_$ (NestedListLevel "string") -> nil_$ (NestedListLevel nil ) -> nil_$ (NestedListLevel '("A" "B" "C") ) -> 0_$ (NestedListLevel '(("A")("B")("C")) ) -> 1_$ (NestedListLevel '(("A")(("B"))("C")) ) -> 2_$ (NestedListLevel '(("A")((((("B")))))("C")) ) -> 5_$ (NestedListLevel '(((("A")))((((((("B")))))))("C")) ) -> 7_$ (NestedListLevel '(((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C")) ) -> 24
I hope the above example is clear enough.  :rolleyes2:

Heres my attempt with this:

--- Code - Auto/Visual Lisp: ---; _$ (NestedListLevel "string") -> nil; _$ (NestedListLevel nil ) -> nil; _$ (NestedListLevel '("A" "B" "C") ) -> 0; _$ (NestedListLevel '(("A")("B")("C")) ) -> 1; _$ (NestedListLevel '(("A")(("B"))("C")) ) -> 2; _$ (NestedListLevel '(("A")((((("B")))))("C")) ) -> 5; _$ (NestedListLevel '(((("A")))((((((("B")))))))("C")) ) -> 7; _$ (NestedListLevel '(((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C")) ) -> 24(defun NestedListLevel ( L / rec )  (defun rec ( L i )    (or i (setq i 0))    (cond       ( (not L) (list i))      ( (and (listp L) (listp (car L))) (append (rec (car L) (1+ i)) (rec (cdr L) i)) )      ( (listp L) (rec (cdr L) i) )    )  )  (if (and L (listp L)) (apply 'max (rec L nil)) )); defun NestedListLevelIMO my code is a bit sloppy (sorry about that  - haven't done much recursions recently).

Lee Mac:
Quick one:
--- Code - Auto/Visual Lisp: ---(defun foo ( lst )    (apply 'max (mapcar '(lambda ( x ) (if (listp x) (if (< 0 (vl-list-length x)) (+ 1 (foo x)) 1) 0)) lst)))

ronjonp:
Here's mine:

--- Code - Auto/Visual Lisp: --- (defun nestedlistlevel_rjp (l)  (if (listp l)    (apply 'max           (mapcar (function (lambda (x)                               (if (listp x)                                 (length (vl-remove-if-not                                           (function (lambda (y) (= 40 y)))                                           (vl-string->list (vl-princ-to-string x))                                         )                                 )                                 0                               )                             )                   )                   l           )    )  ))

Lee Mac:

--- Code - Auto/Visual Lisp: ---(nestedlistlevel_rjp '(("(1") "2" "3"))

--- Code - Auto/Visual Lisp: ---(nestedlistlevel_rjp '(((1) (2) (3))))
:wink:

Grrr1337:
Wow Lee, you always keep up with this super-advanced lisp level!
Guess I still have to figure out the mapcar with a recursive function combo.

Ron, interesting technique, but here are some bad news:


--- Code - Auto/Visual Lisp: ---_$ (NestedListLevel '(((((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C")))) )26_$ (NESTEDLISTLEVEL_RJP '(((((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C")))) )30_$ (foo '(((((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C")))) )26

Navigation

[0] Message Index

[#] Next page

Go to full version