Week 5: Object Oriented Programming 9. Classes and Inheritance Exercise: int set
class intSet(object):
"""An intSet is a set of integers
The value is represented by a list of ints, self.vals.
Each int in the set occurs in self.vals exactly once.""" def __init__(self):
"""Create an empty set of integers"""
self.vals = [] def insert(self, e):
"""Assumes e is an integer and inserts e into self"""
if not e in self.vals:
self.vals.append(e) def member(self, e):
"""Assumes e is an integer
Returns True if e is in self, and False otherwise"""
return e in self.vals def remove(self, e):
"""Assumes e is an integer and removes e from self
Raises ValueError if e is not in self"""
try:
self.vals.remove(e)
except:
raise ValueError(str(e) + ' not found') def __str__(self):
"""Returns a string representation of self"""
self.vals.sort()
return '{' + ','.join([str(e) for e in self.vals]) + '}'
Your task is to define the following two methods for the intSet class:
Define an
intersectmethod that returns a newintSetcontaining elements that appear in both sets. In other words,s1.intersect(s2)
would return a new
intSetof integers that appear in boths1ands2. Think carefully - what should happen ifs1ands2have no elements in common?Add the appropriate method(s) so that
len(s)returns the number of elements ins.Hint: look through the Python docs to figure out what you'll need to solve this problem.
class intSet(object):
"""An intSet is a set of integers
The value is represented by a list of ints, self.vals.
Each int in the set occurs in self.vals exactly once."""
def __init__(self):
"""Create an empty set of integers"""
self.vals = []
def insert(self, e):
"""Assumes e is an integer and inserts e into self"""
if not e in self.vals:
self.vals.append(e)
def member(self, e):
"""Assumes e is an integer
Returns True if e is in self, and False otherwise"""
return e in self.vals
def remove(self, e):
"""Assumes e is an integer and removes e from self
Raises ValueError if e is not in self"""
try:
self.vals.remove(e)
except:
raise ValueError(str(e) + ' not found')
def intersect(self, other):
"""Assumes other is an intSet
Returns a new intSet containing elements that appear in both sets."""
# Initialize a new intSet
commonValueSet = intSet()
# Go through the values in this set
for val in self.vals:
# Check if each value is a member of the other set
if other.member(val):
commonValueSet.insert(val)
return commonValueSet
def __str__(self):
"""Returns a string representation of self"""
self.vals.sort()
return '{' + ','.join([str(e) for e in self.vals]) + '}'
def __len__(self):
"""Returns the length of the set.
This method is called by the `len` built-in function."""
return len(self.vals)
Week 5: Object Oriented Programming 9. Classes and Inheritance Exercise: int set的更多相关文章
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
- 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性
一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...
- Python 面向導向語言 Object Oriented Programming Language
Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- opp(Object Oriented Programming)
嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...
- oop(Object Oriented Programming)
嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...
- python, 面向对象编程Object Oriented Programming(OOP)
把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数 ...
- JS面向对象程序设计(OOP:Object Oriented Programming)
你是如何理解编程语言中的面向对象的? 我们研究JS和使用JS编程本身就是基于面向对象的思想来开发的,JS中的一切内容都可以统称为要研究的“对象”,我们按照功能特点把所有内容划分成“几个大类,还可以基于 ...
随机推荐
- - description 方法作用
自定义一个Person类 @interface Person : NSObject { int _age; double _height; double _weight; NSString *_nam ...
- Docker学习笔记_网上资源参考
Docker学习,网上资源参考 1.菜鸟教程: http://www.runoob.com ...
- 安装 SQL Server 2014 Express
安装 SQL Server 2014 Express 我的电脑系统: Windows 10 64位 一 . 下载 安装Microsoft SQL Server 2014 Express 软甲下载地址: ...
- 23.NULL 函数
SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 "Products" 表: P_Id ProductName UnitPrice ...
- redirect_uri域名与后台配置不一致,错误码:10003
登录公众平台,重新配置下网页授权域名就可以了 参考https://blog.csdn.net/haoxuexiaolang/article/details/79432073
- sublime text 3安装 vue插件
1.上一个章节讲到Vue.js的环境安装,这一章节主要是针对ST3 如何安装vue插件,来快速的进行vue组件代码的编写. (内容转载自:https://www.cnblogs.com/bluedoc ...
- Mbatis——动态SQL
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- canvas时钟demo
显示效果如下 源码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- ASP.NET框架获取数据字典数据做成树的格式
private List<TreeEntity> treeList = new List<TreeEntity>();//创建一个树的List集合 public ActionR ...
- ios PNG Crush error (PNG图片错误)
我是这么解决的: I had the same problem. How to fix : Open up image with Preview -> File > Export > ...