TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on July 16, 2009, 09:38:18 AM

Title: (Challenge) Number of Different Positions
Post by: JohnK on July 16, 2009, 09:38:18 AM
Define a procedure that takes two lists of symbols, equal in length, and determines the number of positions at which they contain different values in 0(n) [a single pass].

For instance, given the lists (a b c e f g) and (a b d e g h), the procedure should return 3.
Title: Re: (Challenge) Number of Different Positions
Post by: MP on July 16, 2009, 09:47:29 AM
Coded quick and blind:

Code: [Select]
(defun foo ( a b )
    (apply '+ (mapcar '(lambda (i j) (if (eq i j) 1 0)) a b))
)

Maybe it works, maybe it doesn't.
Title: Re: (Challenge) Number of Different Positions
Post by: JohnK on July 16, 2009, 09:50:07 AM
Coded quick and blind:
...

Maybe it works, maybe it doesn't.
Yep, she works. Good job.
Title: Re: (Challenge) Number of Different Positions
Post by: Keith™ on July 16, 2009, 09:52:56 AM
Coded quick and blind:

Code: [Select]
(defun foo ( a b )
    (apply '+ (mapcar '(lambda (i j) (if (eq i j) 1 0)) a b))
)

Maybe it works, maybe it doesn't.

 :|

Do you think you could get it a little more concise?
Title: Re: (Challenge) Number of Different Positions
Post by: JohnK on July 16, 2009, 09:56:15 AM
...

 :|

Do you think you could get it a little more concise?

*lol* yeah i was kinda taken aback as well ...but anyways, thats up to [you]. So get to work!!
Title: Re: (Challenge) Number of Different Positions
Post by: Keith™ on July 16, 2009, 09:57:43 AM
Coded quick and blind:
...

Maybe it works, maybe it doesn't.
Yep, she works. Good job.


Actually, now that I look at it, it doesn't work as requested ... but it does work exactly opposite as requested ;)

You asked for the number of different positions, the code returns the number of equal positions.

But no harm, no foul .. flip the 1 0 bits in the if evaluation like so:

Code: [Select]
(defun foo ( a b )
    (apply '+ (mapcar '(lambda (i j) (if (eq i j) 0 1)) a b))
)
Title: Re: (Challenge) Number of Different Positions
Post by: MP on July 16, 2009, 09:58:31 AM
:|

Do you think you could get it a little more concise?

busy, headache, apathy, sorry
Title: Re: (Challenge) Number of Different Positions
Post by: JohnK on July 16, 2009, 10:00:32 AM
Whoa?! Good spot Keith.
Title: Re: (Challenge) Number of Different Positions
Post by: VovKa on July 16, 2009, 11:18:58 AM
Code: [Select]
(defun test (lst1 lst2)
  (if (and lst1 lst2)
    (+ (if (equal (car lst1) (car lst2))
0
1
       )
       (test (cdr lst1) (cdr lst2))
    )
    0
  )
)
Title: Re: (Challenge) Number of Different Positions
Post by: CAB on July 16, 2009, 11:25:20 AM
Code: [Select]
(defun foo ( a b )
    (length (vl-remove nil (mapcar '= a b)))
)
Title: Re: (Challenge) Number of Different Positions
Post by: Keith™ on July 16, 2009, 11:37:32 AM
Code: [Select]
(defun foo ( a b )
    (length (vl-remove nil (mapcar '= a b)))
)

interesting solution CAB, but it also is opposite of the challenge .. to return the number that is different

Either of the next edits would meet the requirement

Code: [Select]
(defun foo ( a b )
    (length (vl-remove nil (mapcar '/= a b)))
)

or

Code: [Select]
(defun foo ( a b )
    (length (vl-remove T (mapcar '= a b)))
)

incidently, I am not providing a solution because quite honestly I can't best what is already provided as viable solutions
Title: Re: (Challenge) Number of Different Positions
Post by: CAB on July 16, 2009, 11:45:34 AM
I never was one for reading the instructions.  8-)
Title: Re: (Challenge) Number of Different Positions
Post by: JohnK on July 16, 2009, 11:46:08 AM
...
incidently, I am not providing a solution because quite honestly I can't best what is already provided as viable solutions
Ah, so what?! Mine was about as straight forward as you can get. Its just for fun anyways. Everyone's initial reaction will have a different spin and each offers a new learning experience. Post what you got.

Mine:
Code: [Select]
( (lambda ( a e / i )
    (setq i 0)
    (mapcar
      '(lambda ( o u )
         (if (not (eq o u))
           (setq i (1+ i))))
      a e)
    i)
 '(a b c e f g)
 '(a b d e g h))
Title: Re: (Challenge) Number of Different Positions
Post by: T.Willey on July 16, 2009, 12:03:59 PM
Working on my recursive skills.

Code: [Select]
(defun foo ( a b c )
    
    (if (car a)
        (if (equal (car a) (car b))
            (foo (cdr a) (cdr b) c)
            (foo (cdr a) (cdr b) (1+ c))
        )
        c
    )
)

Command: (foo '(1 2 3 4 5 6) '(1 2 5 6 3 6) 0)
3
Title: Re: (Challenge) Number of Different Positions
Post by: Lee Mac on July 16, 2009, 01:44:14 PM
I'm not too good with recursive stuff, but maybe:

Code: [Select]
(defun foo (a b)
  (apply '+
    (if a
      (list
        (if (eq (car a) (car b)) 0 1)
          (foo (cdr a) (cdr b))))))