联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2022-04-24 10:55

ITI1120 D – Review Session

Monday, April 4, 2022


Part One: Object-Oriented Programming:


1) Write a class Rectangle that represents a rectangular two-dimensional region. Your

Rectangle objects should have the following methods:


Method Description

def init (self,x,y,w,h)

Initializes a new Rectangle whose top-left corner is

specified by the given (x, y) coordinates and with the

given width and height, w by h.


Raise a ValueError on a negative width or height.

def height(self) A property representing the rectangle’s height.

def width(self) A property representing the rectangle’s width.

def x(self) A property representing the rectangle’s x-coordinate.

def y(self) A property representing the rectangle’s y-coordinate.

def str (self)

Returns a string representation of this Rectangle,

such as "Rectangle[x=1,y=2,width=3,height=4]".


2) Add the following accessor method to your Rectangle class: def contains (self, x, y). Your

method should return True if the given coordinates lie inside the bounds of this

Rectangle.


3) Add following method to Rectangle class: union(self,rect). Your method should accept

another Rectangle as a parameter and return a new Rectangle that represents the area

occupied by the tightest bounding box that contains both the current Rectangle (self) and

the given other Rectangle.


4) Add the following method to Rectangle class: intersection(self,rect). Your method should

accept another Rectangle as a parameter and return a new Rectangle that represents the

largest rectangular region completely contained within both the current Rectangle (self)

and the given other Rectangle. If the rectangles don’t intersect at all, returns a Rectangle

with its width and height both equal to 0.


5) Add the following method to your Rectangle class: def__eq__(self,rect). Your method

should accept another Rectangle as a parameter and return True if the two rectangles

have exactly the same state, including their x, y, width, and height values.


Part Two: Dictionaries and Sets:


Write a function called overlap that takes a set of integers and a list of integers as parameters

and that returns a new set containing values that appear in both structures. For example, given

set and list:

set1: {0, 19, 8, 9, 12, 13, 14, 15}

list1: [0, 19, 2, 4, 5, 9, 10, 11]

If the following call is made:

overlap(set1, list1)

the function would return:

{0, 19, 9}


You are not allowed to construct any structures, besides the set you will return, to solve this

problem. You may not alter the passed in list or set. You may not convert the list to a set.


Part Three: Recursion:


Write a function called reverse that accepts a dictionary from strings to strings as a parameter

and returns a new dictionary that is the reverse of the original. The reverse of a dictionary is a

new dictionary that uses the values from the original as its keys and the keys from the original

as its values. Since a dictionary’s values need not be unique but its keys must be, you should

have each value map to a set of keys. For example, if passed the following dictionary:

{42: "Marty", 81: "Sue", 17: "Ed", 31: "Dave", 56: "Ed", 3: "Marty", 29: "Ed"}


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp