Pages

Thursday, December 30, 2010

Grouping Algorithm Using Javascript

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 29,2010
 * @version : 1.0
 *
 * This function will provide distinct selection algorithm.
 */

/**
 * Constructor service
 * @param data array of data
 */
function GroupingAlgorithm(data){
    this.data = data;
}

/**
 * a mehtod that provide base service to slice the data.
 * var data = [1,2,2,3,1,2]
 *          = [2,2,3,2]
 *          = [3]
 *          = []
 * @param varData array of data
 * @return sliced array
 */
GroupingAlgorithm.prototype.first = function(varData){
    var value = varData[0];
    var newData = new Array();
    var k = 0;
    for(i=1;i<varData.length;i++){
        if(varData[i]!=value){
            newData[k]=varData[i];
            k++;
        }
    }
    return newData;
};


/**
 * This function will do like what happend at SELECT DISTINCT 
 * in MySQL.
 * @return array unsorted distinct value;
 */
GroupingAlgorithm.prototype.getDistinctValue = function(){
    var rest = true;
    var distinctValue = new Array();
    var d = 0;
    var copyData = this.data;
    while(rest){
        distinctValue[d]=copyData[0];
        copyData = this.first(copyData);
        d++;
        if(copyData.length==0){
            rest = false;
        }

    }
    return distinctValue;
};

/**
 * A method to know how many occurence for specified value
 *
 * @return array of data
 */
GroupingAlgorithm.prototype.getTotalNumber = function(){
    var distinctData = this.getDistinctValue();
    var nTimes = new Array();
  
    for(i=0;i<distinctData.length;i++){
        var n = 0;
        for(j=0;j<this.data.length;j++){
            if(distinctData[i]==this.data[j]){
                n++;
            }
        }
        nTimes[i]=n;
    }
    return nTimes;
};
        

Selection Algorithm Using Java

/**
 * Selection Algorithm
 * This function provide service for selection algorithm.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 29, 2010
 * @version : 1.0
 */

/**
 * Constructor service
 * @param data array of data
 */
function DataAlgorithm(data){
    this.data = data;
}

/**
 * function to get min value from the data
 * @return min value of the data
 */
DataAlgorithm.prototype.getMinValue = function(){
    return this.data[this.getMinValueIndex()];
};

/**
 * function to get max value from the data
 * @return max value of the data
 */
DataAlgorithm.prototype.getMaxValue = function(){
    return this.data[this.getMaxValueIndex()];
};

/**
 * function to get index of the min value
 * @return number index of the min value
 */
DataAlgorithm.prototype.getMinValueIndex = function(){
    var minIndex = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]<this.data[minIndex]){
            minIndex = i+1;
        }
    }
    return minIndex;
};

/**
 * method to get index of the max value
 * @return number index of the max value
 */
DataAlgorithm.prototype.getMaxValueIndex = function(){
    var maxIndex = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]>this.data[maxIndex]){
            maxIndex = i+1;
        }
    }
    return maxIndex;
};
      

Selection Algorithm In Python

# Selection Algorithm
#
# @author  : irfanudin ridho
# @email   : irfan.ub@gmail.com
# @date    : December 31, 2010
# @version : 1.0


class SA:

    def __init__(self,data):
        self.data = data
 
    def getMinIndex(self):
        minIndex = 0
        lenn = len(self.data)
        for i in range(lenn):
            if(self.data[i]<self.data[minIndex]):
                minIndex = i
        return minIndex
 
    def getMinValue(self):
        return self.data[self.getMinIndex()]

    def getMaxIndex(self):
        maxIndex = 0
        lenn = len(data)
        for i in range(lenn):
            if(self.data[i]>self.data[maxIndex]):
                maxIndex = i
        return maxIndex

    def getMaxValue(self):
        return self.data[self.getMaxIndex()]
 

List In Javascript

/**
 * A List of ADT. This ADT works well like list in other library.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * Constructor service
 */
function List(){
    this.data = new Array();
}

/**
 * method to add an element to list
 *
 * @param item string,number,boolean value. element to be added
 */
List.prototype.add = function(item){
    var len = this.data.length;
    this.data[len] = item;
};

/**
 * method to remove an element from the list
 *
 * @param item string, number or boolean as what the list contains.
 */
List.prototype.remove = function(item){
    var newData = new Array();
    var len = this.data.length;
    var k = 0;
    for(i=0;i<len;i++){
        if(this.data[i]!=item){
            newData[k] = this.data[i];
            k++;
        }
    }
    this.data = newData;
};


/**
 * method to know how many elements in the list
 *
 * @return number number of elements
 */
List.prototype.size = function(){
    return this.data.length;
};

/**
 * method to give this list as an array
 *
 * @return an array
 */
List.prototype.toArray = function(){
    return this.data;
};
      

Queue In Javascript

/**
 * A Queue ADT. This ADT provide functionality for FIFO 
 * data structure.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Queue(){
    this.data = new Array();
}

/**
 * method to insert an element to the Queue
 *
 * @param item string, number, boolean element to be inserted.
 */
Queue.prototype.push = function(item){
    var len = this.data.length;
    var newData = new Array();
    newData[0] = item;
    for(i=0;i<len;i++){
        newData[i+1]=this.data[i];
    }
    this.data = newData;
};

/**
 * A method to remove the first item inserted.
 *
 * @return first element of the Queue.
 */
Queue.prototype.pop = function(){
    var len = this.data.length;
    var first = this.data[len-1];
    var newData = new Array();
    for(i=1;i<len;i++){
        newData[i-1]=this.data[i];
    }
    this.data = newData;
    return first;
};

/**
 * A method to get how many elements in the Queue
 *
 * @return number number of elements.
 */
Queue.prototype.size = function(){
    return this.data.length;
};

/**
 * A method to represent this queue as an array data structure.
 *
 * @return array of data
 */
Queue.prototype.toArray = function(){
    return this.data;
}

Stack In Javascript

/**
 * An ADT of Stack. This is an implementation of LIFO 
 * data structure.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Stack(){
    this.data = new Array();
}

/**
 * A method to insert an element to the top position.
 *
 * @param item string, number or boolean value to be inserted
 */
Stack.prototype.push = function(item){
    var len = this.data.length;
    this.data[len]=item;
};

/**
 * A method to get the top position from the stack. As well as
 * to remove it.
 *
 * @return the value of the top position.
 */
Stack.prototype.pop = function(){
    var len = this.data.length;
    var last = this.data[len-1];
    var newStack = new Array();
    for(i=0;i<len-1;i++){
        newStack[i]=this.data[i];
    }
    this.data = newStack;
    return last;
};

/**
 * A method to get the size of elements in the Stack.
 *
 * @return number number of elements in Stack
 */
Stack.prototype.size = function(){
     return this.data.length;
};

/**
 * A method to convert the stack to be an array representation.
 *
 * @return an array of data.
 */
Stack.prototype.toArray = function(){
     return this.data;
}
         

Map In Javascript

/**
 * An ADT of Map. This ADT will do functionality as 
 * the key value ADT.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Map(){
    this.key = new Array();
    this.value = new Array();
}

/**
 * A method to add an elements to the Map
 *
 * @param k string, number key element
 * @param v string, number, boolean value element
 */
Map.prototype.add = function(k,v){
    var len = this.key.length;
    this.key[len] = k;
    this.value[len] = v;
};

/**
 * A method to get an element as suitable for the key
 *
 * @param k key of the element
 * @return value as suitable for the key.
 */
Map.prototype.get = function(k){
    var index = 0;
    for(i=0;i<k.length;i++){
        if(this.key[i]==k)
        index = i;
    }
    return this.value[index];
};