Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ShapeSet:
- def __init__(self):
- """
- Initialize any needed variables
- """
- self.shapes = []
- self.place = None
- self.tristring = ""
- self.cirstring = ""
- self.squstring = ""
- self.shapesstring = ""
- def addShape(self, sh):
- """
- Add shape sh to the set; no two shapes in the set may be
- identical
- sh: shape to be added
- """
- if sh in self.shapes:
- raise ValueError('duplicate shape')
- else: self.shapes.append(sh)
- def __iter__(self):
- """
- Return an iterator that allows you to iterate over the set of
- shapes, one shape at a time
- """
- self.place = 0
- return self
- def next(self):
- if self.place >= len(self.shapes):
- raise StopIteration
- self.place += 1
- return self.shapes[self.place - 1]
- def __str__(self):
- """
- Return the string representation for a set, which consists of
- the string representation of each shape, categorized by type
- (circles, then squares, then triangles)
- """
- ## sorted()
- shapeslist = sorted(self.shapes)
- for shape in shapeslist:
- self.shapesstring = self.shapesstring +shape.__str__()+'\n'
- return self.shapesstring
- ## catagorised by type
- #for shape in self.shapes:
- #if type(shape)==Triangle:
- #self.tristring = self.tristring + shape.__str__() + "\n"
- #if type(shape)==Circle:
- #self.cirstring = self.cirstring + shape.__str__() + "\n"
- #if type(shape)==Square:
- #self.squstring = self.squstring + shape.__str__() + "\n"
- #return self.cirstring + self.squstring + self.tristring
- ## results with sorted()
- ##Circle with radius 2.0
- ##Circle with radius 4.3
- ##Circle with radius 3.3
- ##Circle with radius 4.5
- ##Circle with radius 6.0
- ##Circle with radius 19.4
- ## results with catagories
- ##Circle with radius 4.3
- ##Circle with radius 2.0
- ##Circle with radius 4.5
- ##Circle with radius 3.3
- ##Circle with radius 6.0
- ##Circle with radius 19.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement