0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonのクラス継承の問題を解いてみる

Posted at

Scientific Computing with Pythonの続き

freeCodeCampでコツコツPythonを勉強しています。
前回の記事では、今回はPolygon Area Calculatorに挑戦します。

  • Python for Everybody
  • Scientific Computing with Python Projects
    • Arithmetic Formatter
    • Time Calculator
    • Budget App
    • Polygon Area Calculator(今回はここ)
    • Probability Calculator

4問目:Polygon Area Calculator

求められてることは以下の通り

  • Rectangleクラスの作成
  • Squareクラスの作成
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

個人的ポイント: 特になし…

Rectangleの実装

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})"

最後に

唯一調べたのは、クラス名を取得するのってどうだっけ?という部分だけでした。
成長しているのか?

次の問題はProbability Calculatorです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?