Sparta Camp Python 2019 Day2 Challenge

I didn't know the answer to the problem on the video, so I tried to solve it myself. I thought it would be helpful for those who are learning from the live video of Sparta Camp.

Task

This is a beginner's problem in object-oriented programming. Rather than writing good code in object-oriented programming, the goal is to be able to write in the correct grammar.

Implement the Circle class so that the following code is correct. area means area, and perimeter means perimeter (perimeter).

circle.py


#Circle with radius 1
circle1 = Circle(radius=1)
print(circle1.area()) #3.14
print(circle1.perimeter()) #6.28

#Circle with radius 3
circle3 = Circle(radius=3)
print(circle3.area()) #28.26 ← Probably 28 in the video.27 is wrong
print(circle3.perimeter()) # 18.84 ← Probably 18 in the video.85 is wrong


#Implement the Rectangle class so that the following code works correctly
#diagonal means (the length of) the diagonal.


rectangle1 = Rectangle(height=5, width=6)
rectangle1.area() #30.00
rectangle1.diagonal() #7.81

rectangle2 = Rectangle(height=3, width=3)
rectangle2.area() #9.00
rectangle2.diagonal() #4.24

answer

It's a rough code, but I'll put the answer I came up with for reference.

class Circle:
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return (self.radius ** 2) * 3.14

    def perimeter(self):
        return self.radius * 2 * 3.14

class Rectangle:

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

    def area(self):
        area = self.height * self.width
        print(f'{area:.2f}')

    def diagonal(self):
        line = pow(self.height, 2) + pow(self.width, 2)
        line = line ** (1/2)
        print(f'{line:.2f}')

Correct answer if the execution result is as follows!

3.14
6.28
28.26
18.84
30.00
7.81
9.00
4.24

Recommended Posts

Sparta Camp Python 2019 Day2 Challenge
Python day 1
[Python] Challenge 100 knocks! (015 ~ 019)
[Python] Challenge 100 knocks! (030-034)
Python learning day 4
[Python] Challenge 100 knocks! (010-014)
Rabbit Challenge 4Day
[Python] Challenge 100 knocks! (025-029)
python challenge diary ①
Rabbit Challenge 3DAY
[Python] Challenge 100 knocks! (020-024)
Python study day 1
[1day1lang AdventCalender] day4 Python
Effective Python Learning Memorandum Day 15 [15/100]
Umemura style Python expedition Day 0
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Rabbit Challenge Deep Learning 1Day
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Rabbit Challenge Deep Learning 2Day
Effective Python Learning Memorandum Day 13 [13/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Effective Python Learning Memorandum Day 4 [4/100]
Umemura style Python expedition day 1
Effective Python Learning Memorandum Day 7 [7/100]
Challenge Python3 and Selenium Webdriver
Effective Python Learning Memorandum Day 2 [2/100]
[Introduction to Python3 Day 1] Programming and Python
Challenge LOTO 6 with Python without discipline
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
# 2 Python beginners challenge AtCoder! ABC085C --Otoshidama
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)