Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SpecialList:
- """A list that can hold a limited number of items."""
- lst = []
- size = 0
- def init(self, size):
- """ (SpecialList, int)
- >>> L = SpecialList(10)
- >>> L.size
- 10
- >>> L.value_list
- []
- """
- self.size = 10
- def push_value(self, new_value):
- """ (SpecialList, object) -> NoneType
- Append new_value to this list, if there is enough space in the list according to its maximum size.
- If there is insufficient space, new_value should not be added to the list.
- >>> L = SpecialList(10)
- >>> L.push_value(3)
- >>> L.value_list
- [3]
- """
- if len(list) < 10:
- self.lst.append(new_value)
- def pop_most_recent_value(self):
- """ (SpecialList) -> object
- Precondition: len(self.value_list) != 0
- Return the value added most recently to value_list and remove it from the list.
- >>> L = SpecialList(10)
- >>> L.push_value(3)
- >>> L.push_value(4)
- >>> L.value_list
- [3, 4]
- >>> L.pop_most_recent_value()
- 4
- """
- return self.lst.pop(-1)
- def compare(self, other):
- """ (SpecialList, SpecialList) -> int
- Return 0 if both SpecialList objects have lists that contain the same number of items.
- Return 1 if self's list contains more items than other's list.
- Return -1 if self's list contains fewer items than other's list.
- """
- # complete this code
- if len(self.lst) == len(other.lst):
- return 0
- if len(self.lst) > len(other.lst):
- return 1
- if len(self.lst) < len(other.lst):
- return -1
- class Rectangle:
- """ A rectangle with a width and height. """
- def init(self, w, h):
- """ (Rectangle, number, number)
- Create a new rectangle of width w and height h.
- >>> r = Rectangle(1, 2)
- >>> r.width
- 1
- >>> r.height
- 2
- """
- self.width = w
- self.height = h
- def get_area(self):
- """ (Rectangle) -> number
- Return the area of this rectangle.
- >>> r = Rectangle(10, 20)
- >>> r.get_area()
- 200
- """
- return self.width * self.height
- class RectangleCollection:
- def init(self):
- """ (RectangleCollection) -> NoneType
- >>> rc = RectangleCollection()
- >>> rc.rectangles
- []
- """
- self.rectangle_list = []
- def add_rectangle(self, rectangle):
- self.rectangle_list.append(rectangle)
- def get_same_area_rects(self, area):
- temp_list = []
- for rectangle in self.rectangle_list:
- if rectangle.get_area == area:
- temp_list.append(rectangle)
- return temp_list
- class User:
- def init(self, username, password, account_info):
- """ (User, str, str, str) -> NoneType
- Initialize the user with username, password, and account_info.
- >>> new_user = User('xyz', 'password1', "Bob's Online Banking")
- >>> new_user.username
- 'xyz'
- >>> new_user.password
- 'password1'
- >>> new_user.account_info
- "Bob's Online Banking"
- """
- self.username = username
- self.password = password
- self.account_info = account_info
- def login(self, entered_password):
- """ (User, str) -> bool
- Return True iff the user's password matches entered_password.
- >>> new_user = User('xyz', 'password1', "Bob's Online Banking")
- >>> new_user.login('password1')
- True
- >>> new_user.login('1234')
- False
- """
- return self.password == entered_password
- def update_account(self, entered_password, new_info):
- """ (User, str, str) -> NoneType
- Modify the user's account_info to be new_info if the user's password
- matches entered_password.
- >>> new_user = User('xyz', 'password1', "Bob's Online Banking")
- >>> new_user.update_account('1234', 'B.O.B.')
- >>> new_user.account_info
- "Bob's Online Banking"
- >>> new_user.update_account('password1', 'B.O.B.')
- >>> new_user.account_info
- 'B.O.B.'
- """
- if self.password == entered_password:
- self.account_info = new_info
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement