Author Topic: Challenge ( cylinder volume )  (Read 5907 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Challenge ( cylinder volume )
« 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!
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Challenge ( cylinder volume )
« Reply #1 on: March 31, 2006, 11:36:37 AM »
<sitting on hands for now>

 :evil:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Challenge ( cylinder volume )
« Reply #2 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?

MP

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

whdjr

  • Guest
Re: Challenge ( cylinder volume )
« Reply #7 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)
)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Challenge ( cylinder volume )
« Reply #8 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)
  )
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Challenge ( cylinder volume )
« Reply #9 on: March 31, 2006, 05:29:59 PM »
Nice challenge Mark .. :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

RbtDanforth

  • Guest
Re: Challenge ( cylinder volume )
« Reply #10 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

RbtDanforth

  • Guest
Re: Challenge ( cylinder volume )
« Reply #11 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

LE

  • Guest
Re: Challenge ( cylinder volume )
« Reply #12 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: ")))))

LE

  • Guest
Re: Challenge ( cylinder volume )
« Reply #13 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: ")))))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Challenge ( cylinder volume )
« Reply #14 on: March 31, 2006, 06:44:18 PM »
hehehehe .. I was just posting, you spoil my fun   :cry:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge ( cylinder volume )
« Reply #15 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: ")))
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.

LE

  • Guest
Re: Challenge ( cylinder volume )
« Reply #16 on: March 31, 2006, 07:13:42 PM »
I must admit too.... I forgot about the usage of progn  :oops:

RbtDanforth

  • Guest
Re: Challenge ( cylinder volume )
« Reply #17 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

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Challenge ( cylinder volume )
« Reply #18 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?")))

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Challenge ( cylinder volume )
« Reply #19 on: April 01, 2006, 08:46:57 AM »
Good stuff! That poor cat got skinned a bunch'a ways didn't it.  :-)
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Challenge ( cylinder volume )
« Reply #20 on: April 01, 2006, 10:28:26 AM »
I don't know how you come up with these challenges Mark. Very creative.
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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Challenge ( cylinder volume )
« Reply #21 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.
TheSwamp.org  (serving the CAD community since 2003)