Try to solve the Python class inheritance problem

Continuation of Scientific Computing with Python

I'm studying Python steadily at freeCodeCamp. In Previous article, this time we will challenge * Polygon Area Calculator *.

4th question: Polygon Area Calculator

What is required is as follows

--Creating a Rectangle class --Creating a Square class

rect = shape_calculator.Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())

sq = shape_calculator.Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())

rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))
50
26
Rectangle(width=10, height=3)
**********
**********
**********

81
5.656854249492381
Square(side=4)
****
****
****
****

8

Personal point: Nothing in particular ...

Rectangle implementation

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

    def set_width(self, width):
        self.width = width

    def set_height(self, height):
        self.height = height

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

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

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

    def get_picture(self):
        if(self.width >= 50 or self.height >= 50):
            return "Too big for picture."

        row = "*" * self.width
        return "\n".join([row for idx in range(self.height)]) + "\n"

    def get_amount_inside(self, shape):
        return int(self.get_area() / shape.get_area())

    def __str__(self):
        return f"{self.__class__.__name__}(width={self.width}, height={self.height})"

Finally

The only thing I looked up was how about getting the class name? It was only the part. Are you growing?

The next issue is * Probability Calculator *.

Recommended Posts

Try to solve the Python class inheritance problem
Try to solve the internship assignment problem with Python
Try to solve the fizzbuzz problem with Keras
Try to solve the man-machine chart with Python
Try to solve the programming challenge book with python3
[Python] Try to read the cool answer to the FizzBuzz problem
I tried to solve the problem with Python Vol.1
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Stack problem: Try to solve "20. Valid Parentheses"
How to solve the bin packing problem
Python amateurs try to summarize the list ①
Solve the maximum subarray problem in Python
Try to solve the N Queens problem with SA of PyQUBO
I wanted to solve the ABC164 A ~ D problem with Python
Try to solve the shortest path with Python + NetworkX + social data
Try to solve the function minimization problem using particle swarm optimization
Try to calculate a statistical problem in Python
[Python] Class inheritance (super)
The 16th offline real-time how to write reference problem to solve with Python
[Python] Class inheritance, override
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
The 19th offline real-time how to write reference problem to solve with Python
Try to solve a set problem of high school math with Python
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)
The first algorithm to learn with Python: FizzBuzz problem
I tried to solve the soma cube with python
Solve the Monty Hall problem
Try to understand Python self
The 15th offline real-time I tried to solve the problem of how to write with python
Try to get the function list of Python> os package
I wanted to solve the Panasonic Programming Contest 2020 with Python
How to use the __call__ method in a Python class
Try to solve the problems / problems of "Matrix Programmer" (Chapter 0 Functions)
Try to automate the operation of network devices with Python
Solve the Japanese problem when using the CSV module in Python.
The problem becomes easier to solve depending on the formulation method
Try to decipher the garbled attachment file name with Python
How to write offline real time I tried to solve the problem of F02 with Python
How to write a Python class
Leave the troublesome processing to Python
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Try the Python LINE Pay SDK
Try to operate Facebook with Python
"The easiest Python introductory class" modified
In the python command python points to python3.8
How to get the Python version
Try to introduce the theme to Pelican
[Python] Road to snake charmer (3) Python class
Try to calculate Trace in Python
[Python] How to import the library
Try converting cloudmonkey CLI to python3 -1
Examine the object's class in python
Try using the Python Cmd module
Cython to try in the shortest
The fastest way to try EfficientNet
The easiest way to try PyQtGraph
[Python] Change the alphabet to numbers
I tried to solve the ant book beginner's edition with python
[Python] Solving the import problem due to the difference in entry points
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)