Jump to content

Recommended Posts

Posted
BTW: Both my last suggestion and Lee's Chain do not work on:

(setq ls3 '((B1 B2 B3 B4 B5) (B3 B4) (B2 B3) (B1 B2)))

: (chain ls3)
((B4 B3 B2 B1 B2 B3 B4 B5))
: (List_To_Chains ls3)
((B1 B2 B3 B4 B5) (B2 B3 B4))

 

each item in my list is repeated at max 2 times ... so both of you and lee 's routines are working fine now ...

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

  • handasa

    12

  • Roy_043

    6

  • Lee Mac

    4

  • Tharwat

    1

Posted
(setq ls0 '((B8 B9) (B1 B2) (B6 B5) (B2 B1) (B3 B4) (B2 B3) (B6 B7) (B10 B11)))
(setq ls1 '((B1 B2) (B3 B4) (B2 B3) (B1 B2)))
(setq ls2 '((B1 B2) (B1 B2) (B3 B4) (B2 B3)))
(setq ls3 '((B1 B2 B3 B4 B5) (B3 B4) (B2 B3) (B1 B2)))
(setq ls4 '((B1 B2) (B3 B2) (B1 B2) (B3 B2) (B1 B2) (B3 B2) (B1 B2) (B3 B2)))

; (SubLists_To_Chains ls0) => ((B7 B6 B5) (B1 B2 B3 B4) (B8 B9) (B10 B11))
(defun SubLists_To_Chains (lst)
 (while
   (vl-some
     '(lambda (sub1)
       (vl-some
         '(lambda (sub2 / new)
           (if (setq new (List_Chain sub1 sub2))
             (setq lst
               (cons
                 new
                 (vl-remove sub2 (vl-remove sub1 lst))
               )
             )
           )
         )
         (vl-remove sub1 lst)
       )
     )
     lst
   )
 )
 lst
)

; (List_Chain '(B1 B2 B3 B4 B5) '(B1 B2))
; (List_Chain '(B5 B4 B3 B2) '(B1 B2))
; (List_Chain '(B5 B4 B3 B2) '(B3 B4))
; (List_Chain '(B1 B2) '(B3 B2))
(defun List_Chain (lst1 lst2 / tmp pos)
 (if (> (length lst1) (length lst2))
   (mapcar 'set '(lst1 lst2) (list lst2 lst1))
 )
 (if (not (vl-position (car lst1) lst2)) (setq lst1 (reverse lst1)))
 (while (and lst1 (setq tmp (vl-position (car lst1) lst2)))
   (setq pos tmp)
   (setq lst1 (cdr lst1))
 )
 (cond
   ((not pos)
     nil
   )
   ((not lst1)
     lst2
   )
   ((zerop pos)
     (append (reverse lst1) lst2)
   )
   (T
     (append lst2 lst1)
   )
 )
)

Posted

this one is excellent , Roy ... thanks man

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...