

function numericSort(x,y) {
    var a = new Number(x) ;
    var b = new Number(y) ;
    return (a - b) ;
}

function CommonProblem(l1,l2) {                    // Define a problem for comparison
    this.data1 = new List() ;
    this.data2 = new List() ;
    this.intersectionList = new List() ;
    this.count = new Number() ;

    if ((l1 == undefined) || (l1 == null) ) {
        l1 = "" ;
    }
    if ((l2 == undefined) || (l2 == null)) {
        l2 = "" ;
    }
    this.assign(l1,l2) ;
}

CommonProblem.prototype.assign = function (l1,l2) { // Identify to lists to compare.
    (this.data1).assign(l1) ;
    (this.data2).assign(l2) ;
}

CommonProblem.prototype.evaluate = function() { // Locate all matches in the lists.
    
    var i   = new Number() ;
    var j   = new Number() ;
    var str = new String() ;
    
    (this.data1).sort(numericSort) ;   // assure the lists are in numeric order.
    (this.data2).sort(numericSort) ;

    
    (this.data1).reset()   ; // reset the iteration location
    (this.data2).reset()   ; // reset the iteration location
    str = "" ;   

    for ( ; ((this.data1).eof() == false) ;  (this.data1).iterate()) {
        i = (this.data1).current() ;
	
        for ( ; ((this.data2).eof() == false);  (this.data2).iterate()) {
            j = (this.data2).current() ;

	    if (i == j) {
	        if (str.length > 0) {
		    str +="," ;
		}
                str += i ;
		break ;
	    }
	}
        (this.data2).reset()   ;
    }
    this.intersectionList.assign(str) ;
    return str ;
}

CommonProblem.prototype.answerCheck = function(lst) { // Locate exact matches in the lists.
    var x      = new List(lst)  ;
    
    var i      = new Number()   ;
    var j      = new Number()   ;
    var status                  ;
    
    (this.intersectionList).sort(numericSort) ;   // assure the lists are in numeric order.
    x.sort(numericSort) ;

    if ((this.intersectionList).listArray.length == x.listArray.length) {

        status = true                     ; // assume we will match.
    
        (this.intersectionList).reset()   ; // reset the iteration location
        x.reset()   ;                       // reset the iteration location
 
        for ( ; ((this.intersectionList).eof() == false) ;  (this.intersectionList).iterate(), x.iterate()) {
            i = (this.intersectionList).current() ;
	    j = x.current() ;
	    if (parseInt(i) == parseInt(j)) {
                continue ;
	    } else {
	        alert(i + ":" + j) ;
                status = false ;
	        break ;
	    }
        }
    } else {
        status = false ;
    }

    return status ;
}

CommonProblem.prototype.intersection = function() { // return the match list.
    return (this.intersectionList).listArray ;
}

CommonProblem.prototype.least = function()  {  // return the smallest matching number
    return this.intersectionList.item(0) ;
}

CommonProblem.prototype.greatest = function() { // return the largest match
    var last = this.intersectionList.length() ;
    return this.intersectionList.item(last-1) ;
}

