Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

Python List Handson 1

The document describes implementing an IncreasingList class in Python. The class acts as an increasing list that must implement append(), pop(), and len() methods. Append adds a value after removing greater elements, pop removes the last element, and len returns the length. The class is tested against input files containing append, pop, and size operations to print the list length.

Uploaded by

sanskriti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Python List Handson 1

The document describes implementing an IncreasingList class in Python. The class acts as an increasing list that must implement append(), pop(), and len() methods. Append adds a value after removing greater elements, pop removes the last element, and len returns the length. The class is tested against input files containing append, pop, and size operations to print the list length.

Uploaded by

sanskriti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Implement a variation of a list type in Python, specifically a type

IncreasingList that acts as an increasing list of elements. It must implement


the following 3 methods:

-append(self, val): First, it removes all elements from the list that have
greater values than val, starting from the last one. Once there are no greater
elements in the list, it appends val to the end of the list.
-pop(self): It removes the last element from the list if the list is not empty;
otherwise, it does nothing.
-len__(self): returns the number of elements in the list.Additional methods may
be implemented as necessary. Notice that at any moment, by the definition of
append and pop operations, the elements in the list are guaranteed to be sorted
in non-decreasing order. The implementations of the 3 required methods will be
tested by a provided code stub on several input files. Each input file contains
several operations, each one of the types below. Values returned by size
operations are printed to the standard output by the provided code stub.

-append val: calls append(val) on the Increasing List instance pop:


-calls pop() on the Increasing List instance
-size: calls len(obj), where obj is an instance of Increasing List, and prints
the returned value to the standard outputWe need to perform task for Python
Increasing List,

m
er as
#!/bin/python3

co
eH w
import math
import os

o.
import random rs e
import re
ou urc
import sys
o

class IncreasingList:
aC s
v i y re

def __init__(self):
self.incrsng = []

def append(self, val):


"""
ed d

first, it removes all elements from the list that have greater values
ar stu

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
"""
j = len(self)
if j == 0:
sh is

(self.incrsng).extend([val])
Th

else:
while (j != 0) and (self.incrsng[j-1] > val):
del self.incrsng[j-1]
j = j - 1

j = len(self)
(self.incrsng).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
"""
j = len(self)
if j == 0:
pass

This study source was downloaded by 100000828451155 from CourseHero.com on 08-25-2021 12:29:16 GMT -05:00

https://www.coursehero.com/file/85449836/Python-list-handson-1txt/
else:
del self.incrsng[j-1]

def __len__(self):
"""
returns the number of elements in the list
"""
count = 0
for i in self.incrsng:
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])

m
lst.append(val)

er as
elif op_name == "pop":

co
lst.pop()

eH w
elif op_name == "size":
fptr.write("%d\n" % len(lst))

o.
else: rs e
raise ValueError("invalid operation")
ou urc
fptr.close()
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000828451155 from CourseHero.com on 08-25-2021 12:29:16 GMT -05:00

https://www.coursehero.com/file/85449836/Python-list-handson-1txt/
Powered by TCPDF (www.tcpdf.org)

You might also like