Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. def triangle(a, b, c):
  2.    
  3.     if a < b + c and b < a + c and c < a + b and 0 not in [a, b, c]:
  4.         if a == b and b == c:
  5.             return 'equilateral'
  6.         elif a == b or b == c or a == c:
  7.             return 'isosceles'
  8.         else:
  9.             return 'scalene'
  10.     else:
  11.         raise TriangleError()
  12.  
  13. def score(dice):
  14.     # You need to write this method
  15.    
  16.     dice_map = {}
  17.    
  18.     for p in xrange(1, 7):
  19.         dice_map[p] = dice.count(p)
  20.    
  21.     total_points = 0
  22.  
  23.     def mult_helper(index, mult):
  24.         mult_ = dice_map[index] / 3
  25.         dice_map[index] -= 3 * mult_
  26.         return mult * mult_
  27.    
  28.     # * A set of three ones is 1000 points
  29.     total_points += mult_helper(1, 1000)
  30.    
  31.     # * A set of three numbers (other than ones) is worth 100 times the
  32.     #   number. (e.g. three fives is 500 points).    
  33.     for key in dice_map:
  34.         total_points += mult_helper(key, key * 100)
  35.    
  36.     # * A one (that is not part of a set of three) is worth 100 points.    
  37.     total_points += dice_map[1] * 100
  38.    
  39.     # * A five (that is not part of a set of three) is worth 50 points.
  40.     total_points += dice_map[5] * 50
  41.    
  42.     return total_points
  43.  
  44. class Proxy(object):
  45.    
  46.     __internals__ = "_messages", "_obj"
  47.    
  48.     def __init__(self, target_object):
  49.         # WRITE CODE HERE
  50.        
  51.         #initialize '_obj' attribute last. Trust me on this!
  52.         self._obj = target_object
  53.        
  54.         # to save messages
  55.         self._messages = []
  56.  
  57.     def __setattr__(self, attr, value):
  58.        
  59.         if attr not in self.__internals__:
  60.             self._messages.append(attr + "=")
  61.             return setattr(self._obj, attr, value)
  62.         else:
  63.             super(Proxy, self).__setattr__(attr, value)
  64.  
  65.     def __getattr__(self, attr):
  66.        
  67.         if attr not in self.__internals__:
  68.            
  69.             self._messages.append(attr)
  70.        
  71.             if hasattr(self._obj, attr):
  72.                 return getattr(self._obj, attr)
  73.             else:
  74.                 raise AttributeError(attr)
  75.         else:
  76.             return super(Proxy, self).__getattr__(attr)
  77.    
  78.     def messages(self):
  79.         return self._messages
  80.  
  81.     def was_called(self, name):
  82.         return name in self._messages
  83.    
  84.     def number_of_times_called(self, name):
  85.         return self._messages.count(name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement