Help with right angled triangle function

This is the place for queries that don't fit in any of the other categories.

Help with right angled triangle function

Postby guscer » Sat Nov 16, 2013 10:12 am

Hi guys, i'm currently learning with interactivepython: How to Think Like a Computer Scientist, and i'm stuck with an exercise on chapter Decisions and Selection.
1) Write a function is_rightangled which, given the length of three sides of a triangle, will determine whether the triangle is right-angled. Assume that the third argument to the function is always the longest side. It will return True if the triangle is right-angled, or False otherwise.
2) Extend the above program so that the sides can be given to the function in any order.
The 1st question is really easy and i could finish it but i can't do the 2nd question. Although there is answer to this, i still don't understand.
Code: Select all
def is_rightangled(a,b,c):
    is_rightangled = False

    largest = a
    if b > largest:
        # largest = b
        if abs((a**2) + (c**2) - (b**2)) < 0.001:
            is_rightangled = True
    if c > largest:
        # largest = c
        if abs((a**2) + (b**2) - (c**2)) < 0.001:
            is_rightangled = True
    else:
        # largest = a
        if abs((c**2) + (b**2) - (a**2)) < 0.001:
            is_rightangled = True

    return is_rightangled

I wonder if there is any other simpler solution to this question and i hope for full explanation about the above answer. Thank you !
Last edited by Yoriz on Sat Nov 16, 2013 10:41 am, edited 2 times in total.
Reason: First post lock, Changed the title
guscer
 
Posts: 4
Joined: Sat Nov 16, 2013 10:03 am

Re: Decisions and Selection excercise

Postby Yoriz » Sat Nov 16, 2013 10:35 am

Hi, welcome to the forum.
That question has been asked before on this forum, you can view it by the following link.
[ActiveCode] Help with right angled triangle function
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function

Postby guscer » Sat Nov 16, 2013 12:00 pm

already seen it but i can't understand how the sorted function work.
guscer
 
Posts: 4
Joined: Sat Nov 16, 2013 10:03 am

Re: Help with right angled triangle function

Postby stranac » Sat Nov 16, 2013 12:18 pm

guscer wrote:already seen it but i can't understand how the sorted function work.

What about it can't you understand?
It takes an iterable(e.g. list, tuple, string...), and returns a sorted list of its items.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1117
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with right angled triangle function

Postby Yoriz » Sat Nov 16, 2013 4:09 pm

guscer wrote:already seen it but i can't understand how the sorted function work.


Why ask for a solution when you have already seen it, dont you think you should of asked about what you didnt understand of the solution instead.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests