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:

  1. Define an intersect method that returns a new intSet containing elements that appear in both sets. In other words,

    s1.intersect(s2)

    would return a new intSet of integers that appear in both s1 and s2. Think carefully - what should happen if s1 and s2 have no elements in common?

  2. Add the appropriate method(s) so that len(s) returns the number of elements in s.

    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的更多相关文章

  1. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  2. JavaScript: Constructor and Object Oriented Programming

    Constructor :  Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...

  3. 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性

    一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...

  4. Python 面向導向語言 Object Oriented Programming Language

    Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...

  5. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  6. opp(Object Oriented Programming)

    嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...

  7. oop(Object Oriented Programming)

    嗯,昨天忙了一天没来及发,过年啊,打扫啊,什么搽窗户啊,拖地啊,整理柜子啊,什么乱七八糟的都有,就是一个字,忙. 好了,废话也不多说,把自己学到的放上来吧.嗯,说什么好呢,就说原型链啊 原型对象 每个 ...

  8. python, 面向对象编程Object Oriented Programming(OOP)

    把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数 ...

  9. JS面向对象程序设计(OOP:Object Oriented Programming)

    你是如何理解编程语言中的面向对象的? 我们研究JS和使用JS编程本身就是基于面向对象的思想来开发的,JS中的一切内容都可以统称为要研究的“对象”,我们按照功能特点把所有内容划分成“几个大类,还可以基于 ...

随机推荐

  1. 使用RandomAccessFile读写数据

    ------------siwuxie095 工程名:TestRandomAccessFile 包名:com.siwuxie095.file 类名:MultiWriteFile.java(主类).Wr ...

  2. shell cut 用法

    cut -f   提取第几列 -d  按指定的分隔符割列 cut -f 1 xxx.txt   提取第1列 cut -f 1,3 xxx.txt   提取第1,3列 cut -d ":&qu ...

  3. spring aop自动代理注解配置之二

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 面试题:SSH项目总结 !=!=未看 没用

    阿玻罗软件(上海)有限公司已经两年了.中国银行营销系统,到民生银行小微信贷工厂建设.再到交通银行ioffice移动平台项目.以前所学的SSH好多都用不上 公司的框架.都是负责项目的贷款查找模块开发和测 ...

  5. linux-常用命令备注

    //杀掉某个进程-xargs应用 ps aux | grep "udplog.js" | cut -c 9-15 | xargs kill -9 //远程拷贝文件或文件夹 sudo ...

  6. 现代C++学习笔记之二入门篇1

    现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...

  7. 打造一套UI与后台并重.net通用权限管理系统

    一.前言 从进行到软件开发这个行业现在已经有几年了,在整理出这个套开发框架之前自己做了不少重复造轮子的事.每次有新的项目总是要耗费不少时间在UI.权限和系统通用模块上面,自己累得要死,老板还骂没效率. ...

  8. C#静态类 静态方法与非静态方法比较

    静态类 在类(class)上加入static修饰,表示该类无法被实例化,并将该类中,无法实例化变量或函数 静态类的主要特性 仅包含静态成员 无法实例化 静态类的本质,时一个抽象的密封类,所以不能被继承 ...

  9. .Net Core 自动化部署:使用docker版jenkins部署dotnetcore应用

    安装docker版jenkins 因为jenkins的docker版本本身没有 dotnetcore的环境,所以我们需要先自己动手制作下包含dotnet环境的jenkins Docker Contai ...

  10. 搭建基于MinGW平台的《OpenGL蓝皮书(OpenGL SuperBibe 5th)》示例代码编译环境

    副标题:搭建基于MinGW平台的<OpenGL超级宝典>(OpenGL蓝皮书第5版)GLTools 编译环境.示例代码:Triangle.cpp @ SB5.zip 以下内容以及方法均参考 ...