Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
2,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. class SpecialList:
  2. """A list that can hold a limited number of items."""
  3.  
  4. lst = []
  5. size = 0
  6.  
  7. def init(self, size):
  8. """ (SpecialList, int)
  9.  
  10. >>> L = SpecialList(10)
  11. >>> L.size
  12. 10
  13. >>> L.value_list
  14. []
  15. """
  16. self.size = 10
  17.  
  18.  
  19.  
  20. def push_value(self, new_value):
  21. """ (SpecialList, object) -> NoneType
  22.  
  23. Append new_value to this list, if there is enough space in the list according to its maximum size.
  24. If there is insufficient space, new_value should not be added to the list.
  25.  
  26. >>> L = SpecialList(10)
  27. >>> L.push_value(3)
  28. >>> L.value_list
  29. [3]
  30. """
  31. if len(list) < 10:
  32. self.lst.append(new_value)
  33.  
  34.  
  35.  
  36. def pop_most_recent_value(self):
  37. """ (SpecialList) -> object
  38.  
  39. Precondition: len(self.value_list) != 0
  40.  
  41. Return the value added most recently to value_list and remove it from the list.
  42.  
  43. >>> L = SpecialList(10)
  44. >>> L.push_value(3)
  45. >>> L.push_value(4)
  46. >>> L.value_list
  47. [3, 4]
  48. >>> L.pop_most_recent_value()
  49. 4
  50. """
  51. return self.lst.pop(-1)
  52.  
  53.  
  54.  
  55. def compare(self, other):
  56. """ (SpecialList, SpecialList) -> int
  57.  
  58. Return 0 if both SpecialList objects have lists that contain the same number of items.
  59. Return 1 if self's list contains more items than other's list.
  60. Return -1 if self's list contains fewer items than other's list.
  61. """
  62. # complete this code
  63. if len(self.lst) == len(other.lst):
  64. return 0
  65. if len(self.lst) > len(other.lst):
  66. return 1
  67. if len(self.lst) < len(other.lst):
  68. return -1
  69.  
  70.  
  71. class Rectangle:
  72. """ A rectangle with a width and height. """
  73.  
  74. def init(self, w, h):
  75. """ (Rectangle, number, number)
  76.  
  77. Create a new rectangle of width w and height h.
  78.  
  79. >>> r = Rectangle(1, 2)
  80. >>> r.width
  81. 1
  82. >>> r.height
  83. 2
  84. """
  85.  
  86. self.width = w
  87. self.height = h
  88.  
  89. def get_area(self):
  90. """ (Rectangle) -> number
  91.  
  92. Return the area of this rectangle.
  93.  
  94. >>> r = Rectangle(10, 20)
  95. >>> r.get_area()
  96. 200
  97. """
  98.  
  99. return self.width * self.height
  100.  
  101.  
  102. class RectangleCollection:
  103.  
  104. def init(self):
  105. """ (RectangleCollection) -> NoneType
  106.  
  107. >>> rc = RectangleCollection()
  108. >>> rc.rectangles
  109. []
  110. """
  111. self.rectangle_list = []
  112.  
  113. def add_rectangle(self, rectangle):
  114. self.rectangle_list.append(rectangle)
  115.  
  116. def get_same_area_rects(self, area):
  117. temp_list = []
  118. for rectangle in self.rectangle_list:
  119. if rectangle.get_area == area:
  120. temp_list.append(rectangle)
  121. return temp_list
  122.  
  123. class User:
  124.  
  125. def init(self, username, password, account_info):
  126. """ (User, str, str, str) -> NoneType
  127.  
  128. Initialize the user with username, password, and account_info.
  129.  
  130. >>> new_user = User('xyz', 'password1', "Bob's Online Banking")
  131. >>> new_user.username
  132. 'xyz'
  133. >>> new_user.password
  134. 'password1'
  135. >>> new_user.account_info
  136. "Bob's Online Banking"
  137. """
  138. self.username = username
  139. self.password = password
  140. self.account_info = account_info
  141.  
  142. def login(self, entered_password):
  143. """ (User, str) -> bool
  144.  
  145. Return True iff the user's password matches entered_password.
  146.  
  147. >>> new_user = User('xyz', 'password1', "Bob's Online Banking")
  148. >>> new_user.login('password1')
  149. True
  150. >>> new_user.login('1234')
  151. False
  152. """
  153. return self.password == entered_password
  154.  
  155. def update_account(self, entered_password, new_info):
  156. """ (User, str, str) -> NoneType
  157.  
  158. Modify the user's account_info to be new_info if the user's password
  159. matches entered_password.
  160.  
  161. >>> new_user = User('xyz', 'password1', "Bob's Online Banking")
  162. >>> new_user.update_account('1234', 'B.O.B.')
  163. >>> new_user.account_info
  164. "Bob's Online Banking"
  165. >>> new_user.update_account('password1', 'B.O.B.')
  166. >>> new_user.account_info
  167. 'B.O.B.'
  168. """
  169.  
  170. if self.password == entered_password:
  171. self.account_info = new_info
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement