-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses methods.py
More file actions
55 lines (46 loc) · 1.6 KB
/
classes methods.py
File metadata and controls
55 lines (46 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 11:21:58 2017
@author: thelma
"""
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 another set of integers. Compares
self and other and returns all elements that appear in both sets
as new set"""
overlap = intSet()
for element in self.vals:
if element in other.vals and element not in overlap.vals:
overlap.vals.append(element)
return overlap
def __len__(self):
count = 0
for element in self.vals:
count += 1
return count
def __str__(self):
"""Returns a string representation of self"""
self.vals.sort()
return '{' + ','.join([str(e) for e in self.vals]) + '}'