Answer to Question #285200 in Python for basavaraj

Question #285200

implement a variations of a list type in a python, Specifically a type increasing list


1
Expert's answer
2022-01-07T01:50:14-0500
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS