Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

[py challenge] challenge #1

hey all, this is to test your python skills. (i hope you like them, if i get good feedback i will post more)

questions:

1. Write a function called hypotenuse that returns the length of the hypotenuse of a right triangle given the lengths of the two legs as arguments.

2. Write a function called circle_area that takes in two points (each with an x and a y value) which are the center of a circle and a point on the circumference of the circle and then calculates the area of that circle. An example of the function call would be:
Ex. circle_area(4,1,6,12)

post the source code in Pastebin or if you must you can use the "
Code
and
" tags (code tags)
I would hope that you're not using this for homework under the ruse of a challenge...
Spoiler (Click to View)
(Dec 23, 2013 01:38 PM)ImportUsername Wrote:  I would hope that you're not using this for homework under the ruse of a challenge...
Spoiler (Click to View)

the second question you answered looks correct but the anser for the first one should look more like this
Code
import math
def hypotenuse(a,b): #pythagorgas's theorem
    c=a**2+b**2
    hypo=math.sqrt(c) #square roots
    print(hypo)

#hypotenuse(3,4)

also, no its not for homework LOL, im not taking my python course this year.
(Dec 23, 2013 01:58 PM)SupreemJuggalo Wrote:  the second question you answered looks correct but the anser for the first one should look more like this
Code
import math
def hypotenuse(a,b): #pythagorgas's theorem
    c=a**2+b**2
    hypo=math.sqrt(c) #square roots
    print(hypo)

#hypotenuse(3,4)

also, no its not for homework LOL, im not taking my python course this year.

Why would I calculate the hypotenuse by squaring each and then finding the square root of the sum when there is already a function to find the hypotenuse?

from the python docs (http://docs.python.org/2/library/math.html):
"math.hypot(x, y)
Return the Euclidean norm, sqrt(x*x + y*y)"

It's a waste of lines of code

Also the answer you gave is wrong according to your question. You say to make a function that "returns the length of the hypotenuse", but the one you gave as a correct answer only prints it
Spoiler (Click to View)
Haha, I guess he is not very experienced with python...
Here's mine:
PHP Code

<?php 
import math 
#lel kinda cheating but not really
hypotenuse = lambda x, y:math.sqrt((x)**2 + (y)**2)
circle_area = lambda a, b, c, d:(((c-a) ** 2)+((d-b)**2)) * math.pi 

Did I do good?
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
Here's mine
PHP Code

<?php 
#Hypotenuse
sqrt = lambda a:a**0.5
hypotenuse 
= lambda c,d:sqrt(((c**2)+(d**2))) 

PHP Code

<?php 
#Circle
from math import pi,sqrt
circle_area 
= lambda e,f,g,h: pi*(sqrt((g-e)**2+(h-f)**2))**2 


(Dec 24, 2013 03:44 AM)Goodies Wrote:  Here's mine:
PHP Code

<?php 
import math 
#lel kinda cheating but not really
hypotenuse = lambda x, y:((x * x) + (y*y)) ** 2
circle_area 
= lambda a, b, c, d:(((c-a) ** 2)+((d-b)**2)) * math.pi 

Did I do good?

Isn't square root the 1/2 power of a number?
(Dec 24, 2013 04:00 AM)DpzFAsq4UEK8 Wrote:  Here's mine
PHP Code

<?php 
#Hypotenuse
sqrt = lambda a:a**0.5
hypotenuse 
= lambda c,d:sqrt(((c**2)+(d**2))) 

PHP Code

<?php 
#Circle
from math import pi,sqrt
circle_area 
= lambda e,f,g,h: pi*(sqrt((g-e)**2+(h-f)**2))**2 



Isn't square root the 1/2 power of a number?

Yea Goodies is a bit off on his hypotenuse. For example with the one he gave, hypotenuse(3,4) returns 625 instead of 5. I guess that's what happens when you cheat cough cough

Also korn why do you make this:
sqrt = lambda a:a**0.5
when you could import sqrt from math?
(Dec 24, 2013 08:18 AM)ImportUsername Wrote:  Yea Goodies is a bit off on his hypotenuse. For example with the one he gave, hypotenuse(3,4) returns 625 instead of 5. I guess that's what happens when you cheat cough cough

Also korn why do you make this:
sqrt = lambda a:a**0.5
when you could import sqrt from math?

I didn't want to use the math module on that one (and I know that sqrt is in math module...).
And on that second one I used math because I thought it would give me more precision for the pi number than assigning the pi variable manually.
(Dec 24, 2013 08:18 AM)ImportUsername Wrote:  Yea Goodies is a bit off on his hypotenuse. For example with the one he gave, hypotenuse(3,4) returns 625 instead of 5. I guess that's what happens when you cheat cough cough

Also korn why do you make this:
sqrt = lambda a:a**0.5
when you could import sqrt from math?

I didn't cheat lol I BORROWED. LOL
I'm not good with logic O.o

That's not the part of python I work with.


Eh... updated. Pretty sure it works now.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by -MaxFeb 11, 2013 01:17 PM 13 664 Feb 11, 2013 01:17 PM
Last Post: frenchtoast