-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f944c1
commit 88941e4
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Exercise: int set | ||
|
||
# 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. | ||
|
||
# 2. Add the appropriate method(s) so that len(s) returns the number of elements in s. | ||
|
||
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]) + '}' | ||
|
||
def intersect(self, other): | ||
"""Returns intersection of both sets""" | ||
commonList = intSet() | ||
for element in self.vals: | ||
if element in other.vals: | ||
commonList.insert(element) | ||
return commonList | ||
|
||
def __len__(self): | ||
return len(self.vals) | ||
|