TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: well20152016 on January 04, 2022, 02:43:23 AM

Title: [challenge] subtract rle2
Post by: well20152016 on January 04, 2022, 02:43:23 AM
;;;(rle1 '(a a a a b c c a a d e e e e))
;;;((A 6) (B 1) (C 2) (D 1) (E 4))


Code - XML: [Select]
  1. (rle2 (setq l1 (list '(A 6) '(B 2) '(C 2) '(D 1) '(E 4))) (setq l2 (list '(A 15) '(B 11) '(C 12) '(D 11) '(E 41) '(F 31) '(G 44))))
  2. (
  3. (    ((A 6) (B 2) (C 2) (D 1) (E 4))     2)             Simultaneous existence 2 l1            non-existent   return nil
  4.  ((A 3) (B 7) (C 8) (D 9) (E 33) (F 31) (G 44))      surplus
  5. )


Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun rle2 (l1 l2)
  3. (setq l4 l2)
  4. (if (apply 'and (setq l3 (mapcar'(lambda (x) (car(vl-remove-if-not '(lambda (y)(= (car y) (car x)) )l2))) l1)))
  5.   (progn
  6.     (setq ll (apply 'min (mapcar'(lambda(x y) (/(cadr y)(cadr x)))l1 l3)))
  7.     (if (> ll 0)(progn
  8.                   (list (list l1 ll) (mapcar'(lambda(x)(if (setq l4 (vl-remove-if-not'(lambda (y)(= (car y) (car x)))l1)) (list (car x) (- (cadr x)(* ll(cadar l4)))) x))l2))            
  9.      )
  10.       (list nil l2))
  11.   )
  12.  )  
  13. )
  14.  


Title: Re: [challenge] subtract rle2
Post by: JohnK on January 04, 2022, 10:59:36 AM
I'm not sure I followed the spirit of the challenge (I don't think I understood your intent) but here's mine.

(setq lst1 '((A 6) (B 1) (C 2) (D 1) (E 4)))
(setq lst2 '((A 3) (B 1) (C 1) (D 1) (E 3)))
> ((A 3) nil c nil E)

Code - Auto/Visual Lisp: [Select]
  1. (defun subt-rle-list (lst1 lst2)
  2.   (mapcar
  3.     (function
  4.       (lambda ( x y )
  5.         (if (< (cadr y) (cadr x))
  6.           (if (= 1 (- (cadr x) (cadr y)))
  7.             (car x)
  8.             (list (car x) (- (cadr x) (cadr y)))))))
  9.     lst1 lst2))
Title: Re: [challenge] subtract rle2
Post by: ronjonp on January 04, 2022, 01:51:26 PM
Code - Auto/Visual Lisp: [Select]
  1. (defun _foo-rjp (l / a r)
  2.   (while (> (setq a (length l)) 0)
  3.     (setq r (cons (list (car l) (- a (length (setq l (vl-remove (car l) l))))) r))
  4.   )
  5.   (reverse r)
  6. )
  7. (_foo-rjp (setq l '(a a a a b c c a a d e e e e)))
  8. ;; ((A 6) (B 1) (C 2) (D 1) (E 4))
Title: Re: [challenge] subtract rle2
Post by: JohnK on January 04, 2022, 02:23:37 PM
(a a a a b c c a a d e e e e)
>((A 6) (B 1) (C 2) (D 1) (E 4))
Okay, that is confusing. How do you decompress that output?


EDIT: Spelling error
Title: Re: [challenge] subtract rle2
Post by: well20152016 on January 05, 2022, 09:31:32 AM


(setq lst1 '((A 6) (B 1) (C 2) (D 1) (E 4)))
(setq lst2 '((A 3) (B 1) (C 1) (D 1) (E 3)))
> ((A 3) nil c nil E)

(setq lst1 '((A 6) (B 1) (C 2) (D 2) (E 4)))
(setq lst2 '((A 9) (B 2) (C 2) (D 1) (E 5))) >  ((A 3) (B 1) (C 0) nil (E 1))
(nil  '((A 9) (B 2) (C 2) (D 1) (E 5)))

(setq lst1 '((A 6) (B 1) (C 2) (D 2) (E 4)))
(setq lst2 '((A 15) (B 3) (C 4) (D 5) (E 9))) >  ((A 3) (B 1) (C 0) (D 1) (E 1))
(('((A 6) (B 1) (C 2) (D 2) (E 4)) 2)  ' ((A 3) (B 1) (C 0) (D 1) (E 1)))

Simultaneous existence,No nil




Title: Re: [challenge] subtract rle2
Post by: JohnK on January 05, 2022, 10:10:26 AM
(setq lst1 '((A 6) (B 1) (C 2) (D 2) (E 4)))
(setq lst2 '((A 9) (B 2) (C 2) (D 1) (E 5)))
> ((A 3) (B 1) (C 0) nil (E 1))
In this this instance why does c = 0 and d = nil?

(setq lst1 '((A 6) (B 1) (C 2) (D 2) (E 4)))
(setq lst2 '((A 15) (B 3) (C 4) (D 5) (E 9)))
> ((A 3) (B 1) (C 0) (D 1) (E 1))
How does
        A: 6 - 15 = 3?
        B: 1 - 3 = 1?
        C: 2 - 4 = 0?
        ...

This is not run-Length encoding! I am sorry, but I am confused about so many aspects of this thread.
Title: Re: [challenge] subtract rle2
Post by: ssdd on January 05, 2022, 10:46:06 AM
 A 15-2*6=3
 B 3-2*1=1
C 4-2*2=0
D 5-2*2=1
E 9-2*4=1
Title: Re: [challenge] subtract rle2
Post by: JohnK on January 05, 2022, 12:28:16 PM
A 15-2*6=3
 B 3-2*1=1
C 4-2*2=0
D 5-2*2=1
E 9-2*4=1


I think I see now but how do you then decode the result? And what does Run Length Encoding have to do with this operation?
How does: ((A 3) (B 1) (C 0) (D 1) (E 1))
get back to: ((A 6)  (B 1) (C 2) (D 2) (E 4))
or: ((A 15) (B 3) (C 4) (D 5) (E 9))
Title: Re: [challenge] subtract rle2
Post by: well20152016 on January 05, 2022, 10:41:25 PM
A 15-2*6=3
 B 3-2*1=1
C 4-2*2=0
D 5-2*2=1
E 9-2*4=1


I think I see now but how do you then decode the result? And what does Run Length Encoding have to do with this operation?
How does: ((A 3) (B 1) (C 0) (D 1) (E 1))
get back to: ((A 6)  (B 1) (C 2) (D 2) (E 4))
or: ((A 15) (B 3) (C 4) (D 5) (E 9))

get back   (list       
        ((A 3) (B 1) (C 0) (D 1) (E 1))   ------surplus       
       (    ((A 6)  (B 1) (C 2) (D 2) (E 4))  2 )  ------ ((A 6)  (B 1) (C 2) (D 2) (E 4))      There are two