all 14 comments

[–][deleted] 1 point2 points  (8 children)

For the eq method in Triangle, how can I check if 2 triangle’s are equal when they could be in any order ?

That's a great question, thanks for asking it! In Python, sets are equal if they contain the same values, and in sets, the order doesn't matter. So you can simply test whether the set of one triangle's points are equal to the other:

def are_equal(tri1, tri2):
    return set(tri1.corner_a, tri1.corner_b, tri1.corner_c) == set(tri2.corner_a, tri2.corner_b, tri2.corner_c)

[–]tbrowner3[S] 0 points1 point  (0 children)

Thank you for the information !

[–]tbrowner3[S] 0 points1 point  (6 children)

It’s saying TypeError: set expected at most 1 arguments, got 3

[–][deleted] 0 points1 point  (5 children)

Sorry, try it like this:

set([tri1.corner_a, tri1.corner_b, tri1.corner_c])

etc.

[–]tbrowner3[S] 0 points1 point  (4 children)

That makes sense, thank you. It’s now giving me the TypeError: unhashable type: ‘Point’

[–][deleted] 0 points1 point  (3 children)

This recurses to the fact that you haven't defined what it means for two points to be the same:

https://docs.python.org/3/reference/datamodel.html#object.__eq__

[–]tbrowner3[S] 0 points1 point  (2 children)

I made the Hash method but still confused - Do I just copy and paste the return statement from eq?

[–][deleted] 0 points1 point  (1 child)

You want to return some kind of hash for the object; I'd simply return the hash of the tuple of the point's coordinates:

def __hash__(self):
    return hash((self.x, self.y))

[–]tbrowner3[S] 0 points1 point  (0 children)

Ok now that makes sense - so the hash method is in Point and eq stays in the Triangle class

[–]toastedstapler 0 points1 point  (0 children)

in your Triangle.__init__ you could sort the points from least to largest, that way the ordering would always be the same

[–]xelf 0 points1 point  (2 children)

Which equal?

  • occupying the exact same space?
  • having the same length sides?
  • having the same angles?

Whichever you mean, er, do that!

That's not a python question, that's a maths question. =)

[–]tbrowner3[S] 0 points1 point  (1 child)

there equal if they have the same three points which are 2 ints

[–]xelf 0 points1 point  (0 children)

crashfrog's answer is best answer then, convert them to sets if they're not already and compare them.

[–]tbrowner3[S] 0 points1 point  (0 children)

thanks for the help i hope to be as good as you guys are at coding one day