Advertisement
dbwonders

ps9

Jun 1st, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. class ShapeSet:
  2.     def __init__(self):
  3.         """
  4.        Initialize any needed variables
  5.        """
  6.         self.shapes = []
  7.         self.place = None
  8.         self.tristring = ""
  9.         self.cirstring = ""
  10.         self.squstring = ""
  11.         self.shapesstring = ""
  12.     def addShape(self, sh):
  13.         """
  14.        Add shape sh to the set; no two shapes in the set may be
  15.        identical
  16.        sh: shape to be added
  17.        """        
  18.         if sh in self.shapes:
  19.             raise ValueError('duplicate shape')
  20.         else: self.shapes.append(sh)
  21.        
  22.     def __iter__(self):
  23.         """
  24.        Return an iterator that allows you to iterate over the set of
  25.        shapes, one shape at a time
  26.        """
  27.         self.place = 0
  28.         return self
  29.     def next(self):
  30.         if self.place >= len(self.shapes):
  31.             raise StopIteration
  32.         self.place += 1
  33.         return self.shapes[self.place - 1]  
  34.     def __str__(self):
  35.         """
  36.        Return the string representation for a set, which consists of
  37.        the string representation of each shape, categorized by type
  38.        (circles, then squares, then triangles)
  39.        """
  40.        ## sorted()
  41.         shapeslist = sorted(self.shapes)
  42.         for shape in shapeslist:
  43.             self.shapesstring = self.shapesstring +shape.__str__()+'\n'                    
  44.         return self.shapesstring
  45.        
  46.     ## catagorised by type
  47.         #for shape in self.shapes:
  48.             #if type(shape)==Triangle:
  49.                 #self.tristring = self.tristring + shape.__str__() + "\n"
  50.             #if type(shape)==Circle:
  51.                 #self.cirstring = self.cirstring + shape.__str__() + "\n"
  52.             #if type(shape)==Square:
  53.                 #self.squstring = self.squstring + shape.__str__() + "\n"
  54.         #return self.cirstring + self.squstring + self.tristring  
  55.  
  56. ## results with sorted()
  57. ##Circle with radius 2.0
  58. ##Circle with radius 4.3
  59. ##Circle with radius 3.3
  60. ##Circle with radius 4.5
  61. ##Circle with radius 6.0
  62. ##Circle with radius 19.4
  63.  
  64. ## results with catagories
  65. ##Circle with radius 4.3
  66. ##Circle with radius 2.0
  67. ##Circle with radius 4.5
  68. ##Circle with radius 3.3
  69. ##Circle with radius 6.0
  70. ##Circle with radius 19.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement