Author Topic: Coordinates compertion  (Read 10378 times)

0 Members and 2 Guests are viewing this topic.

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #15 on: July 28, 2015, 08:44:33 AM »
Here's a simple way to approach it:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( lst / key rtn )
  2.     (setq key (caar lst)
  3.           rtn (caadar lst)
  4.     )
  5.     (foreach grp lst
  6.         (foreach pnt (cadr grp)
  7.             (if (< (cadr rtn) (cadr pnt))
  8.                 (setq rtn pnt
  9.                       key (car grp)
  10.                 )
  11.             )
  12.         )
  13.     )
  14.     (list key rtn)
  15. )

Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.    '(
  3.         ("a" ((0 2 4) (0 7 0) (0 2 0)))
  4.         ("b" ((0 2 0) (0 5 0) (0 2 0)))
  5.         ("c" ((3 2 0) (0 2 0) (9 3 0)))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo lst)
  2. ("a" (0 7 0))

Code: [Select]
(defun foo ( lst / key rtn )
    (setq key (caar lst)
          rtn (caadar lst)
    )
    (foreach grp lst
        (foreach pnt (cadr grp)
            (if (< (cadr rtn) (cadr pnt))
                (setq rtn pnt
                      key (car grp)
                )
            )
        )
    )
    (list key rtn)
)

;_rtn is the first point to be compared to
;_compare each delivered point's Y to rtn's Y
;_if the delivered point is bigger
;_replace  rtn with that point
;_replace the key with the delivered key
        ;_
;_

i understand it, but i dont know how i would explain it to someone else....what logical steps i need to plan in order  to the solution?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #16 on: July 28, 2015, 08:48:53 AM »
i need to learn what you did. :-o

thanks for the meantime....

You're welcome.

The task could be accomplished in many ways - here is another:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo2 ( lst )
  2.     (car (vl-sort (apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (list (car x) y)) (cadr x))) lst)) '(lambda ( a b ) (> (cadadr a) (cadadr b)))))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo2 lst)
  2. ("a" (0 7 0))

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #17 on: July 28, 2015, 08:50:47 AM »
Quote
i understand it, but i dont know how i would explain it to someone else....what logical steps i need to plan in order  to the solution?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #18 on: July 28, 2015, 08:54:01 AM »
;_rtn is the first point to be compared to
;_compare each delivered point's Y to rtn's Y
   ;_if the delivered point is bigger
      ;_replace  rtn with that point
      ;_replace the key with the delivered key
        ;_
;_

Your understanding looks to be correct.

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #19 on: July 28, 2015, 08:55:33 AM »
;_rtn is the first point to be compared to
;_compare each delivered point's Y to rtn's Y
   ;_if the delivered point is bigger
      ;_replace  rtn with that point
      ;_replace the key with the delivered key
        ;_
;_

Your understanding looks to be correct.

how do you map this logic to human language?  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #20 on: July 28, 2015, 09:09:11 AM »
how do you map this logic to human language?  :-)

I don't see the point of your question - you have already described each step of the algorithm in 'human language', which shows that you already understand the algorithm; why then would you need this to be written in such a way to explain to someone else? Is this for a homework assignment?

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #21 on: July 28, 2015, 09:22:34 AM »
how do you map this logic to human language?  :-)

I don't see the point of your question - you have already described each step of the algorithm in 'human language', which shows that you already understand the algorithm; why then would you need this to be written in such a way to explain to someone else? Is this for a homework assignment?

i spent half day thinking about this while you just solve it like you are sms "good morning" to your girlfriend  :-D :-D :-D

i just preparing myself to help my daughter with her homework :-D

I was always thinking there was  a way to tell the machine what to do as it was your employee, but I cant really see it possible, (otherwise there wasnt a need for programing language i guess).
I do understand the algorithm, and you understand that I understand it because you are a programmer, but if youwarnt  a programmer you wouldn’t understand.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #22 on: July 28, 2015, 09:27:59 AM »
how do you map this logic to human language?  :)
You mean something like this:
  • Keep track of the first point and its key in some local variables.
  • Look at each key+point pair in turn, comparing its Y value to the one in that local variable.
  • If the current point's Y is larger than the Y in the local variable, replace the contents of the local variables with the current point and its key.
  • Once all points have finished being compared, the local variables should now contain the values of the largest point, so return them as the last statement in the function.
I was always thinking there was  a way to tell the machine what to do as it was your employee, but I cant really see it possible, (otherwise there wasnt a need for programing language i guess).
That's still just a pipe dream ... one that was thought up in the 50s - 60s: What's referred to as a 4th generation programming language, i.e. a natural human language. 3rd being the normal programming languages we use mostly (e.g. Lisp / C / Python / etc.), 2nd being assembly code (i.e. CPU instructions as abreviated words), 1st being the 1s and 0s representing the on/off signals the CPU actually receives and sends.


Unfortunately human languages are very complex with lots of ambiguity, not the best thing for a computer to "understand". For these to actually work, you'd need a computer with "true" artificial intelligence, i.e. it would need to "understand" your meaning instead of just following a set of steps.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #23 on: July 28, 2015, 09:35:15 AM »
how do you map this logic to human language?  :)
You mean something like this:
  • Keep track of the first point and its key in some local variables.
  • Look at each key+point pair in turn, comparing its Y value to the one in that local variable.
  • If the current point's Y is larger than the Y in the local variable, replace the contents of the local variables with the current point and its key.
  • Once all points have finished being compared, the local variables should now contain the values of the largest point, so return them as the last statement in the function.
I was always thinking there was  a way to tell the machine what to do as it was your employee, but I cant really see it possible, (otherwise there wasnt a need for programing language i guess).
That's still just a pipe dream ... one that was thought up in the 50s - 60s: What's referred to as a 4th generation programming language, i.e. a natural human language. 3rd being the normal programming languages we use mostly (e.g. Lisp / C / Python / etc.), 2nd being assembly code (i.e. CPU instructions as abreviated words), 1st being the 1s and 0s representing the on/off signals the CPU actually receives and sends.


Unfortunately human languages are very complex with lots of ambiguity, not the best thing for a computer to "understand". For these to actually work, you'd need a computer with "true" artificial intelligence, i.e. it would need to "understand" your meaning instead of just following a set of steps.

yaa.. that why a robot can never replace a humen... lets cheers for that  :angel:

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #24 on: July 28, 2015, 10:04:38 AM »
Another sort solution:
Code - Auto/Visual Lisp: [Select]
  1. ; (testSort2 lst)
  2. (defun testSort2 (lst / tmp)
  3.   (setq tmp
  4.     (vl-sort
  5.       (mapcar
  6.         '(lambda (sub)
  7.           (list
  8.             (car sub)
  9.             (vl-sort
  10.               (cadr sub)
  11.               '(lambda (a b) (> (cadr a) (cadr b)))
  12.             )
  13.           )
  14.         )
  15.         lst
  16.       )
  17.       '(lambda (a b) (> (cadr (caadr a)) (cadr (caadr b))))
  18.     )
  19.   )
  20.   (list (caar tmp) (caadar tmp))
  21. )

Recursive solution:
Code - Auto/Visual Lisp: [Select]
  1. ; (TestRecursive lst nil nil)
  2. (defun TestRecursive (lst key pt)
  3.   (cond
  4.     ((not lst)
  5.       (list key pt)
  6.     )
  7.     ;; No more points in sub:
  8.     ((not (cadar lst))
  9.       (TestRecursive (cdr lst) key pt)
  10.     )
  11.     ;; Check if current point has higher Y:
  12.     (
  13.       (>
  14.         (cadr (caadar lst))
  15.         (cadr pt)
  16.       )
  17.       (TestRecursive
  18.         (cons
  19.           (list (caar lst) (cdadar lst)) ; Remove point from current sub.
  20.           (cdr lst)
  21.         )
  22.         (caar lst)
  23.         (caadar lst)
  24.       )
  25.     )
  26.     (T
  27.       (TestRecursive
  28.         (cons
  29.           (list (caar lst) (cdadar lst))
  30.           (cdr lst)
  31.         )
  32.         key
  33.         pt
  34.       )
  35.     )
  36.   )
  37. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #25 on: July 28, 2015, 11:51:48 AM »
Actually I would tend to go about it in a functional way - i.e. I'd want a reduce function (but AutoLisp doesn't have such built in). Thus I'd make one for myself. Here's a simplistic (imperative) variant which works in pretty much the same way as Lee's original code:
Code - Auto/Visual Lisp: [Select]
  1. (defun reduce (operation lst / accumulator)
  2.   (setq accumulator (car lst) operation (eval operation))
  3.   (foreach item (cdr lst)
  4.     (setq accumulator (operation accumulator item)))
  5.   accumulator)
Then I could use that by applying a custom operation onto it - the operation only ever needs to compare two inputs and return whichever it deems the "correct" or some modified value (e.g. when summing a list). In this example case you're looking for the highest Y value inside a list of points. So:
Code - Auto/Visual Lisp: [Select]
  1. (defun PointMaxY (pt1 pt2)
  2.   (if (> (cadr pt2) (cadr pt1)) pt2 pt1))
  3.  
  4. ; Test code
  5. _$ (reduce 'PointMaxY '((1 2 3) (4 3 2) (2 0 6)))
  6. (4 3 2)

Of course, that's just half the issue in your OP. And that's where the map+reduce gives the power behind all this. E.g. extracting the "highest" point from each item in the keyed list:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '(
  2.         ("a" ((0 2 4) (0 7 0) (0 2 0)))
  3.         ("b" ((0 2 0) (0 5 0) (0 2 0)))
  4.         ("c" ((3 2 0) (0 2 0) (9 3 0)))))
  5. (("a" ((0 2 4) (0 7 0) (0 2 0))) ("b" ((0 2 0) (0 5 0) (0 2 0))) ("c" ((3 2 0) (0 2 0) (9 3 0))))
  6. _$ (mapcar '(lambda (item) (list (car item) (reduce 'PointMaxY (cadr item)))) lst)
  7. (("a" (0 7 0)) ("b" (0 5 0)) ("c" (9 3 0)))

And then to extract the highest of each, another reduce on top of that:
Code - Auto/Visual Lisp: [Select]
  1. (reduce '(lambda (item1 item2)
  2.            (if (> (cadadr item2) (cadadr item1)) item2 item1))
  3.         (mapcar '(lambda (item)
  4.                    (list (car item) (reduce 'PointMaxY (cadr item))))
  5.                 lst))
  6. ("a" (0 7 0))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #26 on: July 28, 2015, 01:37:30 PM »
Good generalisation Irné  :-)

For fun, here's another recursive solution, hampered slightly by the data format:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( lst )
  2.     (   (lambda ( bar ) (bar (apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (list (car x) y)) (cadr x))) lst))))
  3.         (lambda ( lst )
  4.             (if (cdr lst)
  5.                 (if (< (car (cdadar lst)) (cadr (cadadr lst)))
  6.                     (bar (cdr lst))
  7.                     (bar (cons (car lst) (cddr lst)))
  8.                 )
  9.                 (car lst)
  10.             )
  11.         )
  12.     )
  13. )

(EDIT: Updated as per Roy's suggestion below)
« Last Edit: July 29, 2015, 05:15:33 AM by Lee Mac »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #27 on: July 28, 2015, 03:43:11 PM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #28 on: July 28, 2015, 03:47:20 PM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Sorry roy, I don't follow?  :?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #29 on: July 29, 2015, 02:56:12 AM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Sorry roy, I don't follow?  :?
Code: [Select]
(setq lst '(0 1 2 3 4))
(cons (cadr lst) (cddr lst)) => (1 2 3 4)
(cdr lst) => (1 2 3 4)