TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on March 31, 2006, 11:29:56 AM

Title: Challenge ( cylinder volume )
Post by: Mark on March 31, 2006, 11:29:56 AM
Calculate the volume of a cylinder given diameter and height. However, to make it a challenge you can not use any variables. You must prompt the user for for both diameter and height, include error checking for negative numbers, zero and <enter>. Local functions are allowed.

have fun!
Title: Re: Challenge ( cylinder volume )
Post by: MP on March 31, 2006, 11:36:37 AM
<sitting on hands for now>

 :evil:
Title: Re: Challenge ( cylinder volume )
Post by: ElpanovEvgeniy on March 31, 2006, 12:09:38 PM
you can not use any variables.
Whether truly I have understood, what it is impossible to use SETQ?
Title: Re: Challenge ( cylinder volume )
Post by: MP on March 31, 2006, 12:12:04 PM
If I understand Mark's intent, and I believe I do, he meant you cannot use set or setq.
Title: Re: Challenge ( cylinder volume )
Post by: MP on March 31, 2006, 12:19:55 PM
For fun I offer --

Code: [Select]
(defun c:foo ( )
    (princ
        (strcat "Volume of cylinder = "
            (rtos
                (   (lambda ( foo )
                        (*  pi
                            (expt (foo "\nEnter radius: ") 2)
                            (foo "\nEnter height: ")
                        )
                    )
                    (lambda ( pmt ) (initget 7) (getdist pmt))
                )
            )
        )
    )
    (princ)
)
Title: Re: Challenge ( cylinder volume )
Post by: MP on March 31, 2006, 12:24:43 PM
Another fun variant --

Code: [Select]
(defun c:foo ( / foo )
    (defun foo ( pmt ) (initget 7) (getdist pmt))
    (princ
        (strcat "Volume of cylinder = "
            (rtos
                (   (lambda ( radius height )
                        (*  pi
                            (expt radius 2)
                            height
                        )
                    )
                    (foo "\nEnter radius: ")
                    (foo "\nEnter height: ")
                )
            )
        )   
    )
    (princ)
)
Title: Re: Challenge ( cylinder volume )
Post by: MP on March 31, 2006, 12:32:54 PM
And for a daffy combination of the two previous variants --

Code: [Select]
(defun c:foo ( )
    (princ
        (strcat "Volume of cylinder = "           
            (rtos
                (   (lambda ( foo )
                        (   (lambda ( radius height )
                                (*  pi
                                    (expt radius 2)
                                    height
                                )
                            )
                            (foo "\nEnter radius: ")
                            (foo "\nEnter height: ")
                        )
                    )
                    (lambda ( pmt ) (initget 7) (getdist pmt))
                )   
            )
        )   
    )
    (princ)
)

:P
Title: Re: Challenge ( cylinder volume )
Post by: whdjr on March 31, 2006, 12:35:41 PM
Here's mine:

Code: [Select]
;Calculate the volume of a cylinder given diameter and height. However,
 ;to make it a challenge you can not use any variables. You must prompt
 ;the user for for both diameter and height, include error checking for
 ;negative numbers, zero and <enter>. Local functions are allowed.
 ;
 ;
 ;
 ;Cylinder pi * radius2 * height
 ;
 ;
 ;
(defun c:cylinder (/ ask sq)
 (defun ask (msg)
  (initget 7)
  (getreal msg)
 )
 (defun sq (num)
  (* num num)
 )
 (princ
  (strcat "\nCylinder Volume:    "
  (rtos (* pi
   (sq (ask "\nEnter Diameter for Cylinder:  "))
   (ask "\nEnter Height for Cylinder:  ")
)
2
  )
  )
 )
 (princ)
)
Title: Re: Challenge ( cylinder volume )
Post by: Mark on March 31, 2006, 01:22:34 PM
Will I'd say you copied my idea but you posted first. :-)

Code: [Select]
(defun c:cubevol ( / ask)

  (defun ask (msg)
    (initget 7)
    (getreal msg)
    )

  (princ
    (strcat "Volume = "
            (rtos
              (*
                (* pi
                   (expt (/ (ask "\nDiameter: ") 2) 2)
                   )
                (ask "\nHeight: ")
                )
              )
            )
    )
  (princ)
  )
Title: Re: Challenge ( cylinder volume )
Post by: Kerry on March 31, 2006, 05:29:59 PM
Nice challenge Mark .. :-)
Title: Re: Challenge ( cylinder volume )
Post by: RbtDanforth on March 31, 2006, 05:39:34 PM
There was once a tale told of WW2 in the Pacific, The Japanese were on one side of the island and the Allies were on the other with a big jungle in the middle. The question was how to go after the Japanese.

The Aussie said "just go through the jungle and get the Japanese"

The Brit said "No, we need to go around the jungle"

The American said " Naw you are all wrong! We just remove the jungle and go get them Japs"

The Aussie lisp would be just to paste this in the command line
Code: [Select]
(* pi (getreal "raduis")(getreal "radius again")(getreal "height"))

Has the additional advantage of working with truncated cones as well :-D
Title: Re: Challenge ( cylinder volume )
Post by: RbtDanforth on March 31, 2006, 06:18:08 PM
On deeper thought it does has to be a cylinder or very nearly one. but there is also
Code: [Select]
'cal (sqr(radius))*pi* (height) but you have to type the radius and height in, and technically it is not lisp but it is short and sweet
Title: Re: Challenge ( cylinder volume )
Post by: LE on March 31, 2006, 06:32:41 PM
Code: [Select]
(apply '*
       (list (expt (cond (1 (initget 6) (getdist "\nRadius: "))) 2)
     (cond (1 (initget 6) (getdist "\nHeight: ")))))
Title: Re: Challenge ( cylinder volume )
Post by: LE on March 31, 2006, 06:40:44 PM
Yes I know I need glasses....  :lol:

Code: [Select]
(apply
  '*
  (list (expt (cond (1 (initget 7) (/ (getdist "\nDiameter: ") 2)))
      2)
(cond (1 (initget 7) (getdist "\nHeight: ")))))
Title: Re: Challenge ( cylinder volume )
Post by: Kerry on March 31, 2006, 06:44:18 PM
hehehehe .. I was just posting, you spoil my fun   :cry:
Title: Re: Challenge ( cylinder volume )
Post by: CAB on March 31, 2006, 07:04:04 PM
Late to the party as usual, but here is my late entry.
Code: [Select]
(* (/ pi 4)
   (expt (progn (initget 7) (getdist "\nDiameter: ")) 2)
   (progn (initget 7) (getdist "\nHeight: ")))
Title: Re: Challenge ( cylinder volume )
Post by: LE on March 31, 2006, 07:13:42 PM
I must admit too.... I forgot about the usage of progn  :oops:
Title: Re: Challenge ( cylinder volume )
Post by: RbtDanforth on March 31, 2006, 08:26:45 PM
Expt! that was what I was missing! :cry:

Code: [Select]
(* (expt (getdist "radius?")2) pi (Getdist "Height"))

(*) doesn't need a progn, and if someone types in "George" it is their problem.

Now the tricky bit would be to draw the cylinder at the same time! :-P
Title: Re: Challenge ( cylinder volume )
Post by: ElpanovEvgeniy on April 01, 2006, 04:57:50 AM
(apply '(lambda (a b) (* pi a a b 0.25)) (mapcar '(lambda (a) (initget 7) (getdist a)) '("Diameter?" "Height?")))
Title: Re: Challenge ( cylinder volume )
Post by: Mark on April 01, 2006, 08:46:57 AM
Good stuff! That poor cat got skinned a bunch'a ways didn't it.  :-)
Title: Re: Challenge ( cylinder volume )
Post by: CAB on April 01, 2006, 10:28:26 AM
I don't know how you come up with these challenges Mark. Very creative.
Title: Re: Challenge ( cylinder volume )
Post by: Mark on April 01, 2006, 10:37:12 AM
I don't know how you come up with these challenges Mark. Very creative.

It ain't easy! Wish I was talented enough to challenge the big guns.  :-)

And for all those reading this, don't be shy, write your own challenges and post them.