..MindWrite..

Archive for the ‘uncategorized’ Category

Python Dynamic Array Implementation

Posted by guptaradhesh on July 30, 2017


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 <self.n:
            return IndexError('K is out of bounds!') # Check it k index is in bounds of array
        
        return self.A[k] #Retrieve from array at index k
        
    def append(self, ele):
        """
        Add element to end of the array
        """
        if self.n == self.capacity:
            self._resize(2*self.capacity) #Double capacity if not enough room
        
        self.A[self.n] = ele #Set self.n index to element
        self.n += 1
        
    def _resize(self,new_cap):
        """
        Resize internal array to capacity new_cap
        """
        
        B = self.make_array(new_cap) # New bigger array
        
        for k in range(self.n): # Reference all existing values
            B[k] = self.A[k]
            
        self.A = B # Call A the new bigger array
        self.capacity = new_cap # Reset the capacity
        
    def make_array(self,new_cap):
        """
        Returns a new array with new_cap capacity
        """
        return (new_cap * ctypes.py_object)()

Advertisements

Posted in uncategorized | Leave a Comment »

Remove first word using sed

Posted by guptaradhesh on April 2, 2014

cat filename | sed -e 's/^ *[^ ]* //'

Rating: 4.5

Posted in uncategorized | Tagged: , | Leave a Comment »

Maximum subarray in an array

Posted by guptaradhesh on March 26, 2014

         /*
	 * Returns a maximum sum value in an array of integers
	 */
	public int maxScan(int[] array)
	{
		if(array.length == 0) return -1;
		else if(array.length == 1) 
			return array[0];
		else
		{	
			int maxSum = array[0];
			int current_max = array[0];
			for(int i = 1; i < array.length; i++)
			{
				current_max = Math.max(array[i], current_max + array[i]); 
				maxSum = Math.max(maxSum, current_max);
			}
			return maxSum;
		}
	}

Posted in uncategorized | Tagged: , | Leave a Comment »

Which linux distribution version and distribution are you using?

Posted by guptaradhesh on February 3, 2014

To find which linux version and distribution you are using:

# uname -a

# cat /proc/version

Posted in uncategorized | Tagged: , , , | Leave a Comment »

Paul Simon’s Tune

Posted by guptaradhesh on October 5, 2010

Paul Simon’s Tune

One of the questions I learned in my algorithms assignment talked about Paul Simon’s tune; as in, given a mathematical equation, which Paul Simon’s tune does it represent. Surely an intersting problem. One might think of going through all songs of nowhere heard Paul Simon.

Lately realised, this tune esp. the lyrics are simply awesome. The meaning is so philosophical, practial and yet so mathematical. It says: “One man’s ceiling is another man’s floor

Mathematically written as,  ∀x∃y⌈x⌉ = ⌊y⌋

Posted in puzzles/ algorithms, uncategorized | Tagged: , | Leave a Comment »