1

This is the error I receive for line 15: TypeError: unsupported operand type(s) for *: 'Interval' and 'Interval'

Trying to create an API for a data type to represent a rectangle in 1D Intervals, for the x and y axis, and to perform certain operations regarding the dimensions

Here is my code:

import stdio
import sys
from interval import Interval


# A data type to represent a rectangle as two (x and y) intervals.
class Rectangle:
    # Constructs a new rectangle given the x and y intervals.
    def __init__(self, xint, yint):
        self._xint = xint
        self._yint = yint

    # Returns the area of this rectangle.
    def area(self):
        return self._xint * self._yint

    # Returns the perimeter of this rectangle.
    def perimeter(self):
        return (self._xint + self._yint) * 2

    # Returns True if this rectangle contains the point (x, y), and False otherwise.
    def contains(self, x, y):
        return (x <= self._xint) and (y <= self._yint)

    # Returns True if this rectangle intersects other, and False otherwise.
    def intersects(self, other):
        return (self._xint < other._xint and self._yint > other._yint) or (self._xint > other._xint and self._yint <
                                                                           other._yint)

    # Returns a string representation of this rectangle.
    def __str__(self):
       return 'Rectangle: %d X %d' % (self._xint, self._yint)


# Unit tests the data type (DO NOT EDIT).
def _main():
    x = float(sys.argv[1])
    y = float(sys.argv[2])
    rectangles = []
    while not stdio.isEmpty():
        lbound1 = stdio.readFloat()
        rbound1 = stdio.readFloat()
        lbound2 = stdio.readFloat()
        rbound2 = stdio.readFloat()
        rectangles += [Rectangle(Interval(lbound1, rbound1), Interval(lbound2, rbound2))]
    for i in range(len(rectangles)):
        stdio.writef('Area(%s) = %f\n', rectangles[i], rectangles[i].area())
        stdio.writef('Perimeter(%s) = %f\n', rectangles[i], rectangles[i].perimeter())
        if rectangles[i].contains(x, y):
            stdio.writef('%s contains (%f, %f)\n', rectangles[i], x, y)
    for i in range(len(rectangles)):
        for j in range(i + 1, len(rectangles)):
            if rectangles[i].intersects(rectangles[j]):
                stdio.writef('%s intersects %s\n', rectangles[i], rectangles[j])


if __name__ == '__main__':
    _main()

Here is the Interval library as well:

import stdio
import sys


# A data type to represent a 1-dimensional interval [lbound, ubound].
class Interval:
    # Construct a new interval given its lower and upper bounds.
    def __init__(self, lbound, ubound):
        self._lbound = lbound
        self._ubound = ubound

    # Returns the lower bound of this interval.
    def lower(self):
        return self._lbound

    # Returns the upper bound of this interval.
    def upper(self):
        return self._ubound

    # Returns True if this interval contains the point x, and False otherwise.
    def contains(self, x):
        if self._lbound <= x <= self._ubound:
            return True
        return False

    # Returns True if this interval intersects other, and False otherwise.
    def intersects(self, other):
        if (self._lbound <= other._lbound <= self._ubound) or (self._lbound <= other._ubound <= self._ubound):
            return True
        return False

    # Returns a string representation of this interval.
    def __str__(self):
        return "[{0}, {1}]".format(self._lbound, self._ubound)


# Unit tests the data type (DO NOT EDIT).
def _main():
    x = float(sys.argv[1])
    intervals = []
    while not stdio.isEmpty():
        lbound = stdio.readFloat()
        rbound = stdio.readFloat()
        intervals += [Interval(lbound, rbound)]
    for i in range(len(intervals)):
        if intervals[i].contains(x):
            stdio.writef('%s contains %f\n', intervals[i], x)
    for i in range(len(intervals)):
        for j in range(i + 1, len(intervals)):
            if intervals[i].intersects(intervals[j]):
                stdio.writef('%s intersects %s\n', intervals[i], intervals[j])


if __name__ == '__main__':
    _main()

0 Answers0