Author Topic: LISP Challenge: Red Sector A  (Read 5100 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
LISP Challenge: Red Sector A
« on: December 10, 2008, 09:38:05 PM »
Write a function that takes 2 arguments:

        Arg 1: Radians (a value between 0 and 2*PI)
        Arg 2: Sectors (an integer between 2 and 32)

Signature:

        (defun GetSector ( radians sectors )
            ...
        )

Objective:

        Find where the value radians falls in a circle divided into sectors.

        If the radians value corresponds exactly to a sector boundary return the whole
        number that is equal to the lower sector it is adjacent to.

        For any radian value that does not correspond to a sector boundary, return
        the whole number that is equal to the lower sector it is adjacent to plus 0.5.

        Sector numbers start at 1.



For example, given radian values of:

        pi/8
        pi/6
        pi/4
        pi/2
        pi

And a sectors value of 4 ( boundaries on multiples of pi/2, or 90° ) the return values should be:

        0.5
        0.5
        0.5
        1.0
        2.0

Using the same radian values but a sectors value of 5 ( boundaries on multiples of 2pi/5 or 72° ) the return values should be:

        0.5
        0.5
        0.5
        1.5
        2.5

Using the same radian values but a sectors value of 6 ( boundaries on multiples of pi/3 or 60° ) the return values should be:

        0.5
        0.5
        0.5
        1.5
        3.0



Perhaps an illustration of the first example will make it easier to understand than my pigeon engrish:

       

        A radians value of precisely 0 should return 0.

        A radians value of precisely pi/2 should return 1.

        Radians values between 0 and pi/2 should return 0.5.

        Clear as mud? :D



Even someone with say a grossly verbose vertical coding style should be able to do it in a half dozen lines of code. :whistle:

Note: Assume the function will be passed valid data, i.e. you do not have to qualify the arguments -- just do the math.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2157
  • class keyThumper<T>:ILazy<T>
Re: LISP Challenge: Red Sector A
« Reply #1 on: December 10, 2008, 10:21:29 PM »
Quote
....  half dozen lines of code

... depends on the formatting ... :)
but something like

Code: [Select]
(defun GetSector (radians sectors / segment)
    (setq segment (/ radians (/ (* pi 2) sectors)))
    (+ (fix segment)
       (if (zerop (- (fix segment) segment)) 0.0  0.5 )
    )
)

didn't have time to fully test or optimise ... lunch is over ...  :|


edit: fixed the local variables ( / sloppy) ...
« Last Edit: December 11, 2008, 05:01:22 PM by kdub@wdt »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2157
  • class keyThumper<T>:ILazy<T>
Re: LISP Challenge: Red Sector A
« Reply #2 on: December 10, 2008, 10:23:00 PM »
(GetSector  (/ PI 8) 4 ) ;==>
(GetSector  (/ PI 6) 4 ) ;==>
(GetSector  (/ PI 4) 4 ) ;==>
(GetSector  (/ PI 2) 4 ) ;==>
(GetSector  (/ PI 1) 4 ) ;==>

(GetSector  (/ PI 8) 5 ) ;==>
(GetSector  (/ PI 6) 5 ) ;==>
(GetSector  (/ PI 4) 5 ) ;==>
(GetSector  (/ PI 2) 5 ) ;==>
(GetSector  (/ PI 1) 5 ) ;==>

(GetSector  (/ PI 8) 6 ) ;==>
(GetSector  (/ PI 6) 6 ) ;==>
(GetSector  (/ PI 4) 6 ) ;==>
(GetSector  (/ PI 2) 6 ) ;==>
(GetSector  (/ PI 1) 6 ) ;==>


0.5
0.5
0.5
1.0
2.0

0.5
0.5
0.5
1.5
2.5

0.5
0.5
0.5
1.5
3.0
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

pkohut

  • Guest
Re: LISP Challenge: Red Sector A
« Reply #3 on: December 10, 2008, 10:24:24 PM »
Code: [Select]
(defun GetSector (rad sec /  v)
  (setq v (/ rad (/ (* PI 2.0) sec))))
  (if (zerop (- (fix v) v))
    (fix v)
    (+ (fix v) 0.5))
)

Paul

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP Challenge: Red Sector A
« Reply #4 on: December 10, 2008, 10:40:27 PM »
Very cool you guys, similar to mine, if not more succinct:

When I first coded mine up it looked like this:

Code: [Select]
(defun _GetSector ( radians sectors )
    (   (lambda ( divisor )
            (+  (if (zerop (rem radians divisor)) 0.0 0.5)   
                (fix (/ radians divisor))
            )
        )
        (/ pi sectors 0.5)
    )   
)

But I found some boundary conditions didn't satisfy zerop properly, so I ended up with:

Code: [Select]
(defun _GetSector ( radians sectors )
    (   (lambda ( divisor )
            (+  (if (equal 0.0 (rem radians divisor) 1e-8) 0.0 0.5)   
                (fix (/ radians divisor))
            )
        )
        (/ pi sectors 0.5)
    )   
)

Funny thing is it took about 10 times longer to write the initial post than the function itself.

I always find it interesting how similar techniques are used but in different implementations, thanks for playing folks. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

pkohut

  • Guest
Re: LISP Challenge: Red Sector A
« Reply #5 on: December 10, 2008, 11:23:31 PM »
My eye sight goes fuzzy when I see lambda in lisp, along with the vla- stuff, but that
is a pretty cool use of rem.

Somehow I had an extra paren between copying and pasting my original, so here it is again, fixed,
and rearranged to get rid of a division.

Code: [Select]
(defun GetSector (rad sec /  v)
  (setq v (* rad (/ sec (* PI PI))))
  (if (zerop (- (fix v) v))
    (fix v)
    (+ (fix v) 0.5))
)

pkohut

  • Guest
Re: LISP Challenge: Red Sector A
« Reply #6 on: December 10, 2008, 11:35:56 PM »
What a minute MP, can you explain how the lambda is working in your
code?  I'm not seeing it   :ugly: :ugly:

Edit:  I see it now, duh!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP Challenge: Red Sector A
« Reply #7 on: December 10, 2008, 11:36:53 PM »
My eye sight goes fuzzy when I see lambda in lisp, along with the vla- stuff, but that
is a pretty cool use of rem.

Somehow I had an extra paren between copying and pasting my original, so here it is again, fixed,
and rearranged to get rid of a division.

Code: [Select]
(defun GetSector (rad sec /  v)
  (setq v (* rad (/ sec (* PI PI))))
  (if (zerop (- (fix v) v))
    (fix v)
    (+ (fix v) 0.5))
)

Sorry for the use of lamba Paul, it just is cleaner to me than assigning then using, though admittedly more aesthetic than anything else.

Your function is close but not quite there ... (getsector pi 4) returns 1.5 whereas the correct answer is 2. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP Challenge: Red Sector A
« Reply #8 on: December 10, 2008, 11:38:56 PM »
What a minute MP, can you explain how the lambda is working in your
code?  I'm not seeing it   :ugly: :ugly:

Edit:  I see it now, duh!

I was just about to respond! Glad you got it. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

pkohut

  • Guest
Re: LISP Challenge: Red Sector A
« Reply #9 on: December 10, 2008, 11:44:41 PM »
NP.  I wish I had learned lisp better way back when, but I just couldn't get into it at all.

As for the second function...it works on my system:
Command: (getsector (/ pi 1) 4)
2

Edit:
Command: (getsector pi 4)
2

Paul

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: LISP Challenge: Red Sector A
« Reply #10 on: December 10, 2008, 11:54:51 PM »
Too late to the party so nothing much I can contribute.
I can rearrange the parts though.
Code: [Select]
(defun GetSector (rad sec /  v)
  (cond
    ((equal 0.0 (- (fix (setq v (/ rad (/ (* PI 2) sec)))) v) 1e-8)
      (fix v))
    ((+ (fix v) 0.5))
  )
)
Off to bed...Zzzzzzz 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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP Challenge: Red Sector A
« Reply #11 on: December 11, 2008, 12:11:20 AM »
NP.  I wish I had learned lisp better way back when, but I just couldn't get into it at all.

As for the second function...it works on my system:
Command: (getsector (/ pi 1) 4)
2

Edit:
Command: (getsector pi 4)
2

Paul

re: "Way back when ...". I remember when I had to pick up lisp way back, and did it under duress -- I thought it was butt ugly code, but my boss at the time pretty much forced me to pick it up. :D.

Are you sure you're testing the ones you posted? Both of them are returning 1.5 from (getsector pi 4), and I've reloaded them twice. :shrug:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

pkohut

  • Guest
Re: LISP Challenge: Red Sector A
« Reply #12 on: December 11, 2008, 01:21:01 AM »
Are you sure you're testing the ones you posted? Both of them are returning 1.5 from (getsector pi 4), and I've reloaded them twice. :shrug:

That second version is a bust, v is computed to the wrong value.  The first version works if you remove the extra paren from
the second line so it reads (setq v (/ rad (/ (* PI 2.0) sec)))

re: "Way back when ...". I remember when I had to pick up lisp way back, and did it under duress -- I thought it was butt ugly code, but my boss at the time pretty much forced me to pick it up. :D.

R10 days, I think around 1990.  I thought lisp was OK, but I had about 11 years of assembly language under my belt and was
determined to learn C and later ADS/ARX.  So any lisp I learned was just enough to get a job done.

But, that being said, after all these years there are 2 books that I still use today
whenever I need help with lisp code.  #1 "Maximizing AutoCAD: Inside Autolisp
Volume 2" and #2 (sorry Tony  :kewl:) "Maximizing AutoCAD R13".  Granted I haven't looked
at many custominzing books in a long time, but nothing beats those 2, IMO.


gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: LISP Challenge: Red Sector A
« Reply #13 on: December 11, 2008, 03:40:56 AM »
Hi,

My 2 cents, quite late but I was sleeping while you where playing.

Code: [Select]
(defun getsector (radians sectors / r f)
  (if (equal (setq r (/ (* radians sectors 0.5) pi))
     (setq f (fix r))
     1e-8
      )
    f
    (+ f 0.5)
  )
)
Speaking English as a French Frog

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: LISP Challenge: Red Sector A
« Reply #14 on: December 11, 2008, 10:51:58 AM »
That second version is a bust, v is computed to the wrong value.  The first version works if you remove the extra paren from
the second line so it reads (setq v (/ rad (/ (* PI 2.0) sec)))

got ya

R10 days, I think around 1990.  I thought lisp was OK, but I had about 11 years of assembly language under my belt and was
determined to learn C and later ADS/ARX.  So any lisp I learned was just enough to get a job done.

But, that being said, after all these years there are 2 books that I still use today
whenever I need help with lisp code.  #1 "Maximizing AutoCAD: Inside Autolisp
Volume 2" and #2 (sorry Tony  :kewl:) "Maximizing AutoCAD R13".  Granted I haven't looked
at many customizing books in a long time, but nothing beats those 2, IMO.

I hear ya. I first toyed with LISP around '86, wrote some tiny utilities but I was heavily embroiled in BASIC PDS and Turbo C at the time and could be bothered to really get into LISP. It was 1990 when my boss "encouraged" me to learn LISP (we had a GIS project that was failing because we were continually shipped LISP from another party, LISP that was crucial to project execution, that wouldn't work). To learn LISP I pretty much typed the entire (Autodesk supplied) LISP manual into a help file linked into my text editor so I had access to it as I coded.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst