当前位置:网站首页>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
边栏推荐
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- Nifi fast installation and file synchronization
- 英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
- JMeter installation tutorial and solutions to the problems I encountered
- Detailed explanation of UWA pipeline function | visual configuration automatic test
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- Regular filtering of Intranet addresses and segments
- Go language, array, string, slice
- VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
- Use itextpdf to intercept the page to page of PDF document and divide it into pieces
猜你喜欢
Detailed explanation of Milvus 2.0 quality assurance system
Idea of batch manufacturing test data, with source code
1959年高考数学真题
Milvus 2.0 质量保障系统详解
Milvus 2.0 質量保障系統詳解
Loggie source code analysis source file module backbone analysis
DanceNN:字节自研千亿级规模文件元数据存储系统概述
oracle 中快速获取表的列名列表
How much do you know about the process of the interview
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
随机推荐
MySQL master-slave replication
杂文 谈谈古典的《拆掉思维里的墙》
Copy constructor shallow copy and deep copy
PostgreSQL列存与行存
First knowledge of go language
◰ GL shader handler encapsulation
Construction of promtail + Loki + grafana log monitoring system
1959年高考数学真题
PHP efficiently reads large files and processes data
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
loggie 源码分析 source file 模块主干分析
Dlib of face recognition framework
Server log analysis tool (identify, extract, merge, and count exception information)
Kingdee Cloud Star API calling practice
◰GL-着色器处理程序封装
VsCode-Go
JSON deserialize anonymous array / object
Document operation II (5000 word summary)
About stream flow, write it down briefly------
DDT + Excel for interface test