TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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.
-
Coded quick and blind:
(defun foo ( a b )
(apply '+ (mapcar '(lambda (i j) (if (eq i j) 1 0)) a b))
)
Maybe it works, maybe it doesn't.
-
Coded quick and blind:
...
Maybe it works, maybe it doesn't.
Yep, she works. Good job.
-
Coded quick and blind:
(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?
-
...
:|
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!!
-
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:
(defun foo ( a b )
(apply '+ (mapcar '(lambda (i j) (if (eq i j) 0 1)) a b))
)
-
:|
Do you think you could get it a little more concise?
busy, headache, apathy, sorry
-
Whoa?! Good spot Keith.
-
(defun test (lst1 lst2)
(if (and lst1 lst2)
(+ (if (equal (car lst1) (car lst2))
0
1
)
(test (cdr lst1) (cdr lst2))
)
0
)
)
-
(defun foo ( a b )
(length (vl-remove nil (mapcar '= a b)))
)
-
(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
(defun foo ( a b )
(length (vl-remove nil (mapcar '/= a b)))
)
or
(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
-
I never was one for reading the instructions. 8-)
-
...
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:
( (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))
-
Working on my recursive skills.
(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
-
I'm not too good with recursive stuff, but maybe:
(defun foo (a b)
(apply '+
(if a
(list
(if (eq (car a) (car b)) 0 1)
(foo (cdr a) (cdr b))))))