#๐Ÿ”’ defining intervals

56 messages ยท Page 1 of 1 (latest)

fast helm
#

not sure how to go about defining equalities, intersections, and differences. I keep getting assertion or type errors.

note: class Interval cannot be edited

dense dustBOT
#

@fast helm

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fast helm
obtuse surge
#

Do one comparison individually

#

Interval(3,10) == Interval(6,10)

#

But I don't think you're using either functions properly

fast helm
#

how would i use the functions properly?

obtuse surge
#

You call them

#

oh wait you're overloading the methods

#

then it should be fine

#

Do individual comparisons

sand gull
#

You need to parenthesize since == has higher precedence than 'and'

#

assert (Interval1 and Interval2) == Interval3

obtuse surge
#

like this

assert Interval(3,10) == Interval(6,10)
sand gull
#

Do you also have a custom equal comparator?

obtuse surge
#

If you want to compare both at the same time, you can add and after that

#
assert Interval(3,10) == Interval(6,10) and Interval(6,20) == Interval(6,10)
sand gull
#

(i.e. Did you override __eq__)

obtuse surge
sand gull
blazing dagger
#

You return before it can reach Interval.__eq__ = func

sand gull
#

Your interval equality check seems strange. It only checks if the other interval is an Interval.

#

You should be checking if the endpoints are equal too?

sand gull
fast helm
#

the parts that say test were written by the prof to grade the def, not sure if i am allowed to edit those or why they are the way they are

fast helm
blazing dagger
sand gull
blazing dagger
sand gull
# fast helm

No need to parenthesize now, but change the asserts back to use bitwise and (&)

sand gull
#

Dunder __and__ overrides bitwise and

sand gull
# fast helm

Now the problem is that your equality check doesn't work

#

As I said, you need to do more than just test for 'other' being an Interval.

#

Two intervals are equal if and only if their corresponding endpoints are equal.

#

Also, make sure you have overridden dunder __ne__

#

(as a shortcut, you can define __ne__ using __eq__)

blazing dagger
sand gull
#

Oh my bad

blazing dagger
#

But you don't have to, it uses the inverted result of eq by default

sand gull
#

It does?!

#

That's news to me, thank you

blazing dagger
#

Anyway,
now it's correctly setting the funcs, now change the defs so the tests pass

sand gull
#

If so, what's the 'minimum' that I need to override?

#

Or any docs I can read about this?

fast helm
#

ok tysm i appreciate y'all so much

#

!close

dense dustBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.