I am currently working through a python course about objected oriented programming. I was given this first assignment to work on:
Create a simple class, MaxSizeList, that acts a little bit like a list, with a pre-configured limit on its size.
Here some test calling code that imports the completed MaxSizeList and then uses it to create two new MaxSizeList objects.
from assignments import MaxSizeList # assumes "class MaxSizeList" # is in a script called "assignments.py"
Below are the test cases:
a = MaxSizeList(3) b = MaxSizeList(1) a.push("hey") a.push("hi") a.push("let's") a.push("go") b.push("hey") b.push("hi") b.push("let's") b.push("go") print(a.get_list()) print(b.get_list())
# ['hi', "let's", 'go'] # ['go']
I came up with this code which outputs the correct result:
class MaxSizeList(object): ls =  def __init__(self,mx): self.val = mx def push(self,st): self.ls.append(st) def get_list(self): while len(self.ls) != self.val: self.ls.pop(0) return self.ls
Do you have any input on how this can be done more efficiently? Did I do it correctly or is there anything to note from the code I came up with?