Author Topic: (Challenge) Number of Different Positions  (Read 2887 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
(Challenge) Number of Different Positions
« 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (Challenge) Number of Different Positions
« Reply #1 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: (Challenge) Number of Different Positions
« Reply #2 on: July 16, 2009, 09:50:07 AM »
Coded quick and blind:
...

Maybe it works, maybe it doesn't.
Yep, she works. Good job.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: (Challenge) Number of Different Positions
« Reply #3 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?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: (Challenge) Number of Different Positions
« Reply #4 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!!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: (Challenge) Number of Different Positions
« Reply #5 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))
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (Challenge) Number of Different Positions
« Reply #6 on: July 16, 2009, 09:58:31 AM »
:|

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

busy, headache, apathy, sorry
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: (Challenge) Number of Different Positions
« Reply #7 on: July 16, 2009, 10:00:32 AM »
Whoa?! Good spot Keith.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Number of Different Positions
« Reply #8 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
  )
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Number of Different Positions
« Reply #9 on: July 16, 2009, 11:25:20 AM »
Code: [Select]
(defun foo ( a b )
    (length (vl-remove nil (mapcar '= a b)))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: (Challenge) Number of Different Positions
« Reply #10 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
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Number of Different Positions
« Reply #11 on: July 16, 2009, 11:45:34 AM »
I never was one for reading the instructions.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: (Challenge) Number of Different Positions
« Reply #12 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))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

T.Willey

  • Needs a day job
  • Posts: 5251
Re: (Challenge) Number of Different Positions
« Reply #13 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
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: (Challenge) Number of Different Positions
« Reply #14 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))))))