import csv
10. Create a class called myPowerList, implement methods for
11. Add a method in myPowerList to return the sorted list Do not use list.sort, implement the algorithm iterating in the list
12. Add a method in myPowerList to merge another list with the current list
13. Add a method in myPowerList to save the current list as a text file
14. Add a method in myPowerList to read values for a List from a Text file
class myPowerList:
def __init__(self):
self.myList = [];
def add(self, e):
self.myList.append(e);
def remove(self, i):
self.myList.pop(i);
def partition(self,arr,low,high):
i = ( low-1 )
pivot = arr[high]
for j in range(low , high):
if arr[j] < pivot:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
def quickSort(self, arr,low,high):
if low < high:
pi = self.partition(arr,low,high)
self.quickSort(arr, low, pi-1)
self.quickSort(arr, pi+1, high)
def get_sorted(self):
self.quickSort(self.myList,0,len(self.myList)-1);
def merge(self, l, d):
if(d=='p'):
self.myList= self.myList + l;
else:
self.myList= l + self.myList;
def getList(self):
return self.myList;
def saveToTextFile(self, filename):
with open(filename, "w", newline='') as outfile:
for r in self.myList:
outfile.write(str(r) + "\n")
def saveToCSVFile(self, filename):
with open(filename, 'w',newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Name', 'Address', 'Phone', 'Email'])
for r in self.myList:
writer.writerow([r.Name, r.Address, r.Phone, r.Email])
def readFromCSVFile (self, filename):
with open(filename) as f:
reader = csv.reader(f)
data = [r for r in reader]
data.pop(0);
self.myList = data;
def readFromTextFile(self, filename):
return open(filename).readlines()
def searchInList(self, t):
match=[];
for r in self.myList:
m = [s for s in r if (t in s)];
if(len(m)>0):
match.append(r);
return match;
li= myPowerList();
li.add(1);
li.add(3);
li.add(6);
li.add(2);
li.add(4);
print(li.getList());
li.get_sorted()
print(li.getList());
newList= ["test1", "test2", "test3"]
li.merge(newList,'s')
print(li.getList());
li.remove(3)
print(li.getList());
li.saveToTextFile("file.csv")
li.readFromTextFile("file.csv")
15. Create a class to manage a directory of users containing data
The class must be enable:
class directory:
def __init__(self, n, a, p, e):
self.Name= n
self.Address = a
self.Phone= p
self.Email = e
li= myPowerList();
li.add(directory("Ramses", "Jalisco", "555555", "test@gmail.com"));
li.add(directory("Gerardo", "Jalisco", "555555", "gerardo@gmail.com"));
li.saveToCSVFile("file.csv")
li.readFromCSVFile("file.csv");
print(li.getList());
print(li.searchInList("Ram"))
print(li.searchInList("Ger"))