Challenge: Shapes with Classes

Lab Python Intermediate 20 points

Create a class Rectangle with width and height, a method area(), a method perimeter() and a __str__ that returns Rectangle WxH. Then create a class Square(Rectangle) that takes only one size, and whose __str__ returns Square S.

Input

A number n, then n lines: rect W H or square S.

Output

For each shape: <shape>: area <area>, perimeter <perimeter>.

Example

Input

3
rect 4 5
square 3
rect 10 1

Output

Rectangle 4x5: area 20, perimeter 18
Square 3: area 9, perimeter 12
Rectangle 10x1: area 10, perimeter 22
Sample input available Sample output available
Editor
Python

Hints

Square's __init__ can call super().__init__(size, size).

Square gets area() and perimeter() from Rectangle for free.

f"{shape}" uses __str__.

Solution explanation

A square is just a rectangle with equal sides, so Square inherits everything and only changes how it's created and printed.

Show solution
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * (self.width + self.height)

    def __str__(self):
        return f"Rectangle {self.width}x{self.height}"


class Square(Rectangle):
    def __init__(self, size):
        super().__init__(size, size)

    def __str__(self):
        return f"Square {self.width}"


for _ in range(int(input())):
    parts = input().split()
    if parts[0] == "rect":
        shape = Rectangle(int(parts[1]), int(parts[2]))
    else:
        shape = Square(int(parts[1]))
    print(f"{shape}: area {shape.area()}, perimeter {shape.perimeter()}")
Output
Answer
HOME LEARN COMMUNITY DASHBOARD