Version: 1.0

Type: Function

Category: Algorithms

License: GNU General Public License

Description: if a user has to order items (e.g. in a questionnaire), check if they are correctly ordered. you can check for monotone and strictly monotone lists.



<?

/*
	isMonotoneList & isStrictlyMonotoneList
	by matto, 6/1
	v 1.0

	parm: array of unordered integer values

	returns true, if monotone / strictly monotone list

	example:	1,2,4,5,5,6 = neither
			1,3,2,4,4,5 = monotone
			1,3,2,4,6,5 = strictly monotone

	purpose:	if user has to order items, check if they are correctly ordered

	improvements:	matto@progipark.com
*/

function isMonotoneList($werte)
{
        sort($werte);

        $ok = true;
        if(is_array($werte))
        {
                foreach($werte as $element)
                {
                        if(!is_numeric($element) || $element == 0)
                        {
                                $ok = false;
                        }
                }

                if($ok)
                {
                        for($i=1; $i<=max($werte); $i++)
                        {
                                if(!in_array($i, $werte))
                                {
                                        $ok = false;
                                }
                        }
                }
        }
        elseif($werte != 1)
        {
                $ok = false;
        }

        return $ok;
}

function isStrictlyMonotoneList($werte)
{
        $ok = true;

        foreach($werte as $element)
        {
                if(!is_numeric($element) || $element == 0)
                {
                        $ok = false;
                }
        }

        for($i=1; $i<=max($werte); $i++)
        {
                $gefunden = 0;
                for($j=0; $j<count($werte); $j++)
                {
                        if($werte[$j] == $i)
                                $gefunden++;
                }

                if($gefunden != 1)
                        $ok = false;
        }

        return $ok;
}

?>