implement a variations of a list type in a python, Specifically a type increasing list
import math
import os
import random
import re
import sys
class IncreasingList(list):
def append(self, val):
"""
first, it removes all elements from the list that have greater values than val, starting from the last one, and once there are no greater element in the list, it appends val to the end of the list
"""
if self == []:
self.extend([val])
else:
while self[-1] > val:
self.pop()
self.extend([val])
def pop(self):
"""
removes the last element from the list if the list is not empty, otherwise, if the list is empty, it does nothing
"""
del self[-1]
def __len__(self):
"""
returns the number of elements in the list
"""
count = 0
for i in self:
count = count + 1
return count
if __name__ == '__main__':
fptr = open(os.environ[''OUTPUT_PATH'], 'w')
lst = IncreasingList()
q = int(input())
for _ in range(q):
op = input().split()
op_name = op[0]
if op_name == "append":
val = int(op[1])
lst.append(val)
elif op_name == "pop":
lst.pop()
elif op_name == "size":
fptr.write("%dn" % len(lst))
else:
raise ValueError("invalid operation")
fptr.close()
Comments
Leave a comment