top of page
Search

Module_6

  • Writer: Angela Porcelli
    Angela Porcelli
  • Feb 19, 2021
  • 1 min read

Original data: Test your functions with the following code:

r1 = create_rectangle(10, 20, 30, 40) print str_rectangle(r1) shift_rectangle(r1, -10, -20) print str_rectangle(r1) r2 = offset_rectangle(r1, 100, 100) print str_rectangle(r1) # should be same as previous print str_rectangle(r2)


The code to run a rectangle is 'Rectangle(Point(0, 0), w, h)'. The first step is to define the class 'Point' so that a working template is created. The prefix for an F-string needs to be included in the return code since the assignment request rectangles r1 and r2 be returned as strings.


class Point: def __init__(self,x,y): self.x=x self.y=y def __str__(self): return f'({self.x}, {self.y})'


The assignment is requesting rectangles to be returned, therefore it is necessary to define the class 'Rectangle' in order to create a working template for rectangles (rect). Again, an F-string prefix must be included in the return code since a string output for the rectangles r1 and r2 will be requested.


class Rectangle: def __init__(self,posn,w,h): self.corner = posn self.width=w self.height=h def __str__(self): return f'({self.corner}, {self.width}, {self.height})' Now, Python must be told how to construct a rectangle


def create_rectangle(x,y,width,height): return Rectangle(Point(x,y),width, height)


The assignment also request that the rectangle be returned as a string, which can be coded as


def str_rectangle(rect): return str(rect)


In order to shift the rectangle to a new location the x and y coordinates must be altered.


def shift_rectangle(rect,dx,dy): ix=rect.corner.x iy=rect.corner.y rect.corner.ix= ix +dx rect.corner.iy = iy+dy


A new rectangle instance offset from the original x and y coordinates by dx and dy can be created by defining an offset_rectangle code.


def offset_rectangle(rect,dx,dy): ix=rect.corner.x iy=rect.corner.y width=rect.width height=rect.height return create_rectangle(ix+dx,iy+dy, width, height)




Follow this assignment on GitHub

 
 
 

Recent Posts

See All
Final_Project

Problem description: The goal of graduating with a college degree is to have the ability to choose an employer that will provide an...

 
 
 

Comments


Post: Blog2_Post
  • Facebook
  • Twitter
  • LinkedIn

©2021 by LIS 5937 - Python for Data Science Professions. Proudly created with Wix.com

bottom of page