Development Exercises – L2

Created by Ramses Alexander Coraspe Valdez

Created on February 24, 2020

In [0]:
import csv

10. Create a class called myPowerList, implement methods for

  1. Adding items
  2. Removing the n-th item

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

  1. Lmerge (merge the list as prefix)
  2. Rmerge (merge the list as suffix)

13. Add a method in myPowerList to save the current list as a text file

  1. saveToTextFile(filename)

14. Add a method in myPowerList to read values for a List from a Text file

  1. readFromTextFile(filename)
In [96]:
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")
[1, 3, 6, 2, 4]
[1, 2, 3, 4, 6]
['test1', 'test2', 'test3', 1, 2, 3, 4, 6]
['test1', 'test2', 'test3', 2, 3, 4, 6]
Out[96]:
['test1\n', 'test2\n', 'test3\n', '2\n', '3\n', '4\n', '6\n']

15. Create a class to manage a directory of users containing data

  • Name
  • Address
  • Phone
  • Email

The class must be enable:

  1. Creation of new record
  2. Save all records in a file
  3. Load records from a file
  4. Search and get data from a given record
In [0]:
class directory:
    def __init__(self, n, a, p, e):
        self.Name= n
        self.Address = a
        self.Phone= p
        self.Email = e             
In [98]:
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"))
[['Ramses', 'Jalisco', '555555', 'test@gmail.com'], ['Gerardo', 'Jalisco', '555555', 'gerardo@gmail.com']]
[['Ramses', 'Jalisco', '555555', 'test@gmail.com']]
[['Gerardo', 'Jalisco', '555555', 'gerardo@gmail.com']]