Python List Handson 1
Python List Handson 1
-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.
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 = []
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)