You are on page 1of 2

import math

import os
import random
import re
import sys

class IncreasingList:
sample=[]

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 len(self.sample)==0:
self.sample.append(val)

else:
if val< self.sample[-1] and len(self.sample)!=0:
print(self.sample[-1])
self.sample.pop()
print("LEngth ",len(self.sample))
self.append(val)
else:
self.sample.append(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
"""
if (len(self.sample)) !=0:
del self.sample[-1]
else:
pass

def __len__(self):
"""
returns the number of elements in the list
"""
return len(self.sample)
if __name__ == '__main__':
fptr = open("C:\\Users\\Kini\\Desktop\\testcase1.txt", 'r')
lst = IncreasingList()
Lines = fptr.readlines()
for line in Lines:
print("-----------------------")
op = line.split()
print(op)
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":
print("%d" % len(lst))
else:
raise ValueError("invalid operation")
print(lst.sample)
fptr.close()

You might also like