当前位置:网站首页>freeCodeCamp----shape_ Calculator exercise
freeCodeCamp----shape_ Calculator exercise
2022-04-23 16:58:00 【Lily's autumn】
Catalog
1 Subject requirements
Request to create a Rectangle And a class Square class , The latter is a subclass of the former , Inherit the methods and properties of the former .
The implementation of class is also very clear , Basically translate the requirements into python Just OK 了 , As follows :
1.1 Rectangle class
establish Rectangle Class time , The following methods need to be included :
1.set_width
2.set_height
3.get_area: Returns area (width * height)
4.get_perimeter: Returns perimeter (2 * width + 2 * height)
5.get_diagonal: Returns diagonal ((width ** 2 + height ** 2) ** .5)
# get_picture Function when the width is less than 50 when , Use the area of the rectangle as * Print out the number
# exceed 50 To print an error message
# Remember to add a newline character to each line
6.get_picture():
# get_amount_inside function , Receive a square parameter , Returns the number of squares that can pass through a rectangle
# give an example : One 4*8 A rectangle can contain two sides with a length of 4*4 The square of
7.get_amount_inside();
When Rectangle When the call is a string , The given form is :Rectangle(width=5, height=10)
1.2 Square class
establish Square Class time , There are the following requirements :
1.Square yes Rectangle Subclasses of ;
2. establish Square Class time , Pass in a side length parameter ;
3.__init__(); Methods should be stored from Rectangle Inherited from width and height attribute ;
4.Square Class can call Rectangle Method , And includes a set_side() Method ;
5. When Square When the call is a string , The given form is :Square(side=9)
6.set_width and set_height Method definition width and height;
2 Test input
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))
Expected output
50
26
Rectangle(width=10, height=3)
**********
**********
**********
81
5.656854249492381
Square(side=4)
****
****
****
****
8
3 Source code analysis
There's nothing to say , All definitions , It is not difficult to have only one print function , Try a few more times and you'll come out .
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."
else:
a = ''
for i in range(self.height):
# for j in range(self.width):
# print('*')
a += '*' * self.width + '\n'
# print('\n')
return a
def __str__(self):
return 'Rectangle(width={}, height={})'.format(self.width, self.height)
def get_amount_inside(self, another_shape):
return (self.width // another_shape.width) * (self.height // another_shape.height)
class Square(Rectangle):
def __init__(self, side):
self.width = side
self.height = side
def set_side(self, side):
self.width = side
self.height = side
def __str__(self):
return 'Square(side={})'.format(self.width)
def set_width(self, side):
self.width = side
self.height = side
def set_height(self, side):
self.width = side
self.height = side
版权声明
本文为[Lily's autumn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230555126952.html
边栏推荐
- Handwritten event publish subscribe framework
- Node access to Alipay open platform sandbox to achieve payment function
- Collect blog posts
- Paging SQL
- Zhongang Mining: Fluorite Flotation Process
- Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
- Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
- Grpc gateway based on Ocelot
- [problem solving] [show2012] random tree
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
猜你喜欢

Mock test

How vscode compares the similarities and differences between two files

feign报400处理

Do you really understand the principle of code scanning login?

昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案

Installation and management procedures

Quick install mongodb

Feign report 400 processing

PyTorch:train模式与eval模式的那些坑

Use case labeling mechanism of robot framework
随机推荐
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
JSON deserialize anonymous array / object
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Sub database and sub table & shardingsphere
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
关于局域网如何组建介绍
Bytevcharts visual chart library, I have everything you want
Detailed explanation of UWA pipeline function | visual configuration automatic test
Server log analysis tool (identify, extract, merge, and count exception information)
Smart doc + Torna generate interface document
MySql主从复制
杂文 谈谈古典的《拆掉思维里的墙》
MySQL personal learning summary
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Nodejs installation and environment configuration
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
如何用Redis实现分布式锁?
PostgreSQL列存与行存