Home>

I need to create a class that creates a fixed-length list and if I want to add more than the length, it overwrites the list. For example,

cycled_list= CycledList (5)
cycled_list.append (1)
cycled_list.append (2)
cycled_list.append (3)
cycled_list.append (4)
cycled_list.append (5)
cycled_list.append (6)
    ``,
    Expected Output:
    ``,
    [6, 2, 3, 4, 5]
    `` ''

I wrote a separate algorithm for the rewriting list

lst1= [1,2,3,4,5,6,7,8,9]
size= 5
lst2= lst1 [size:]
for i in range (0, len (lst2)):
    lst1 [i]= lst2 [i]
    lst1= lst1.copy ()
del lst1 [-len (lst2):]
print (lst1)

Now the same thing needs to be applied to the class

class CycledList:
def __init __ (self, size: int):
        self._data= []
    def append (self, item):
        self._data.append (item)
        print (self._data)

I don't understand how to output the last result of adding to the list, it outputs everything step by step. If I shift print to the left, it will give an error that self is not defined.

  • Answer # 1

    Steps:

    • Added internal indexself._inner_idxwhich increases atappend
    • While the number of elements is less than specified insize, elements are added
    • When the size is reached, new elements replace existing ones. For the cyclicality of the index, we use modulo division by a given sizeself.size

    Example:

    class CycledList:
        def __init __ (self, size: int):
            self._inner_idx= 0
            self._data= []
            self.size= size
        def append (self, item):
            if len (self._data) <
     self.size:
                self._data.append (item)
            else:
                self._inner_idx%= self.size
                self._data [self._inner_idx]= item
            self._inner_idx += 1
    cycled_list= CycledList (5)
    cycled_list.append (1)
    cycled_list.append (2)
    cycled_list.append (3)
    cycled_list.append (4)
    cycled_list.append (5)
    cycled_list.append (6)
    print (cycled_list._data)
    # [6, 2, 3, 4, 5]
    
  • Answer # 2

    Steps:

    • Added internal indexself._inner_idxwhich increases atappend
    • While the number of elements is less than specified insize, elements are added
    • When the size is reached, new elements replace existing ones. For the cyclicality of the index, we use modulo division by a given sizeself.size

    Example:

    class CycledList:
        def __init __ (self, size: int):
            self._inner_idx= 0
            self._data= []
            self.size= size
        def append (self, item):
            if len (self._data) <
     self.size:
                self._data.append (item)
            else:
                self._inner_idx%= self.size
                self._data [self._inner_idx]= item
            self._inner_idx += 1
    cycled_list= CycledList (5)
    cycled_list.append (1)
    cycled_list.append (2)
    cycled_list.append (3)
    cycled_list.append (4)
    cycled_list.append (5)
    cycled_list.append (6)
    print (cycled_list._data)
    # [6, 2, 3, 4, 5]