#!/usr/bin/env python # coding: utf-8 # # Dynamic Array Exercise # ____ # # In this exercise we will create our own Dynamic Array class! # # We'll be using a built in library called [ctypes](https://docs.python.org/2/library/ctypes.html). Check out the documentation for more info, but its basically going to be used here as a raw array from the ctypes module. # If you find yourself very interested in it, check out: [Ctypes Tutorial](http://starship.python.net/crew/theller/ctypes/tutorial.html) # # Also... # **A quick note on public vs private methods, we can use an underscore _ before the method name to keep it non-public. For example:** # In[14]: class M(object): def public(self): print 'Use Tab to see me!' def _private(self): print "You won't be able to Tab to see me!" # In[15]: m = M() # In[17]: m.public() # In[19]: m._private() # Check out PEP 8 and the Python docs for more info on this! # _____ # # ### Dynamic Array Implementation # In[43]: import ctypes class DynamicArray(object): ''' DYNAMIC ARRAY CLASS (Similar to Python List) ''' def __init__(self): self.n = 0 # Count actual elements (Default is 0) self.capacity = 1 # Default Capacity self.A = self.make_array(self.capacity) def __len__(self): """ Return number of elements sorted in array """ return self.n def __getitem__(self,k): """ Return element at index k """ if not 0 <= k