Blog

eXorithm – Execute Algorithm: View / Run Algorithm pluralize

function pluralize ($word$exceptions
{
  if (array_key_exists$word$exceptions))
    return $exceptions$word];
  
  if (substr_compare$word'y', -1, 1)==0) {
    if (in_arraysubstr$word, -2, 1), array'a''e''i''o''u''y'))===false)
      return substr$word, 0, -1).'ies'
    else
      return $word's'
  }
  
  if (substr_compare$word'o', -1, 1)==0) {
    if (in_arraysubstr$word, -2, 1), array'a''e''i''o''u'))===false)
      return $word'es'
    else
      return $word's'
  }
  
  if (substr_compare$word's', -1, 1)==0)
    return $word'es'
  
  if (substr_compare$word'x', -1, 1)==0)
    return $word'es'
  
  if (substr_compare$word'z', -1, 1)==0)
    return $word'es'
  
  if (substr_compare$word'ch', -2, 2)==0)
    return $word'es'
  
  if (substr_compare$word'sh', -2, 2)==0)
    return $word'es'
  
  return $word's'

eXorithm – Execute Algorithm: View / Run Algorithm ordinal

function ordinal ($num
{
  // get the last two digits
  $last = $num % 100;
  // get last digit unless the number is in the teens
  $last = ($last < 20) ? $last : $num % 10; 
  $ord = ($last==1) ? 'st' : (($last==2) ? 'nd' : (($last==3) ? 'rd' : 'th'));
  return $num$ord

Stock Ticker

<?php

/**
 * stock_ticker
 *
 * Generate an HTML stock ticker with current quotes.
 *
 * @version 3.2
 * @author Contributors at eXorithm
 * @link /algorithm/view/stock_ticker Listing at eXorithm
 * @link /algorithm/history/stock_ticker History at eXorithm
 * @license /home/show/license
 *
 * @param array $symbols stocks to go in the ticker
 * @param string $background_color (hex color code) color of the ticker background
 * @param string $stock_color (hex color code) color of the stock symbols
 * @param string $price_color (hex color code) color of the prices
 * @param string $up_color (hex color code) color of gains
 * @param string $down_color (hex color code) color of loses
 * @param number $speed speed of scrolling 
 * @return string HTML
 */
function stock_ticker($symbols=array(0=>'AAPL',1=>'GOOG',2=>'DOW',3=>'GCG13.CMX',4=>'P'),$background_color='dddddd',$stock_color='000000',$price_color='0000bb',$up_color='008000',$down_color='ff0000',$speed=6)
{
	sort($symbols);
	
	if ($background_color==$stock_color) {
		// something's wrong, colors weren't specified
		$background_color = invert_color($background_color);
	}
	
	$return = '<div align="center">
	<marquee bgcolor="#'.$background_color.'" loop="20" scrollamount="'.$speed.'">';
	
	foreach ($symbols as $symbol) {
		$data = file_get_contents("http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv");
		
		$values = explode(",", $data);
		$lasttrade = $values[1];
		$change = $values[4];
		
		$return .= "<span style=\"color:#$stock_color\">$symbol</span> &nbsp;";
		$return .= "<span style=\"color:#$price_color\">$lasttrade</span> &nbsp;";
		if ($change<0)
			$return .= "<span style=\"color:#$down_color\">$change</span> &nbsp;";
		else
			$return .= "<span style=\"color:#$up_color\">$change</span> &nbsp;";
		$return .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	
	$return .= '</marque>
	</div>';
	
	return $return;
}

/**
 * invert_color
 *
 * Invert a color.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/invert_color Listing at eXorithm
 * @link /algorithm/history/invert_color History at eXorithm
 * @license /home/show/license
 *
 * @param string $color (hex color code) 
 * @return string hex color code
 */
function invert_color($color='008080')
{
	$new = '';
	for ($i=0;$i<3;$i++){
		$c = 255 - hexdec(substr($color,(2*$i),2));
		$c = dechex($c);
		$new .= (strlen($c) < 2) ? '0'.$c : $c;
	}
	return $new;
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm simplify_equation

function simplify_equation ($equation
{
  if (is_array$equation)) {
    
    // first simply the parameters of the equation
    $equation[1] = simplify_equation$equation[1]);
    if (isset$equation[2])) {
      $equation[2] = simplify_equation$equation[2]);
    }
    
    // simplify based on the operator
    switch ($equation[0]) {
        
      case '+'
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          return $equation[1]+$equation[2];
        }
        if ($equation[1]===0) {
          return $equation[2];
        }
        if ($equation[2]===0) {
          return $equation[1];
        }
        return $equation
        
      case '-'
        if ($equation[1]===$equation[2]) {
          return 0;
        }
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          return $equation[1]-$equation[2];
        }
        if ($equation[1]===0) {
          return array'neg'$equation[2]);
        }
        if ($equation[2]===0) {
          return $equation[1];
        }
        return $equation
        
      case '*'
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          return $equation[1]*$equation[2];
        }
        if ($equation[1]===0) {
          return 0;
        }
        if ($equation[2]===0) {
          return 0;
        }
        if ($equation[1]===1) {
          return $equation[2];
        }
        if ($equation[2]===1) {
          return $equation[1];
        }
        return $equation
        
      case '/'
        if ($equation[2]===0) {
          throw new Exception'division by zero');
        }
        if ($equation[1]===$equation[2]) {
          return 1;
        }
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          if (($equation[1] % $equation[2])==0) {
            return $equation[1]/$equation[2];
          } else {
            // get the greastest common divisor
            $gcd = gcd_euclid$equation[1], $equation[2]);
            $equation[1] = $equation[1]/$gcd
            $equation[2] = $equation[2]/$gcd
          }
        }
        if ($equation[1]===0) {
          return 0;
        }
        if ($equation[2]===1) {
          return $equation[1];
        }
        return $equation
        
      case '^'
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          return pow$equation[1], $equation[2]);
        }
        if ($equation[1]===0) {
          return 0;
        }
        if ($equation[1]===1) {
          return 1;
        }
        if ($equation[2]===0) {
          return 1;
        }
        if ($equation[2]===1) {
          return $equation[1];
        }
        return $equation
        
      case 'neg'
        if (is_array$equation[1])) {
          if ($equation[1][0]=='neg') {
            return $equation[1][1];
          }
        }
        if (is_numeric$equation[1])) {
          return $equation[1]*-1;
        }
        return $equation
        
      case 'sqrt'
        if (is_numeric$equation[1])) {
          return sqrt$equation[1]);
        }
        return $equation
        
      case 'log'
        if ($equation[1]===1) {
          return 0;
        }
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          return log$equation[1], $equation[2]);
        }
        return $equation
        
      case 'ln'
        if (is_numeric$equation[1])) {
          return log$equation[1]);
        }
        return $equation
        
      case 'exp'
        if (is_numeric$equation[1])) {
          return exp$equation[1]);
        }
        return $equation
        
      case 'root'
        if ((is_numeric$equation[1])) && (is_numeric$equation[2]))) {
          return pow$equation[1], 1/$equation[2]);
        }
        if ($equation[1]===0) {
          return 0;
        }
        if ($equation[1]===1) {
          return 1;
        }
        if ($equation[2]===0) {
          throw new Exception'division by zero');
        }
        if ($equation[2]===1) {
          return $equation[1];
        }
        return $equation
        
        // trig
      case 'sin'
        if (is_numeric$equation[1])) {
          return sin$equation[1]);
        }
        return $equation
        
      case 'cos'
        if (is_numeric$equation[1])) {
          return cos$equation[1]);
        }
        return $equation
        
      case 'tan'
        if (is_numeric$equation[1])) {
          return tan$equation[1]);
        }
        return $equation
        
      case 'sec'
        if (is_numeric$equation[1])) {
          return 1/cos$equation[1]);
        }
        return $equation
        
      case 'csc'
        if (is_numeric$equation[1])) {
          return 1/sin$equation[1]);
        }
        return $equation
        
      case 'cot'
        if (is_numeric$equation[1])) {
          return 1/tan$equation[1]);
        }
        return $equation
        
        // hyperbolic trig
      case 'sinh'
        if (is_numeric$equation[1])) {
          return sinh$equation[1]);
        }
        return $equation
        
      case 'cosh'
        if (is_numeric$equation[1])) {
          return cosh$equation[1]);
        }
        return $equation
        
      case 'tanh'
        if (is_numeric$equation[1])) {
          return tanh$equation[1]);
        }
        return $equation
        
      case 'sech'
        if (is_numeric$equation[1])) {
          return 1/cosh$equation[1]);
        }
        return $equation
        
      case 'csch'
        if (is_numeric$equation[1])) {
          return 1/sinh$equation[1]);
        }
        return $equation
        
      case 'coth'
        if (is_numeric$equation[1])) {
          return 1/tanh$equation[1]);
        }
        return $equation
        
        // arc trig
      case 'arcsin'
        if (is_numeric$equation[1])) {
          return asin$equation[1]);
        }
        return $equation
        
      case 'arccos'
        if (is_numeric$equation[1])) {
          return acos$equation[1]);
        }
        return $equation
        
      case 'arctan'
        if (is_numeric$equation[1])) {
          return atan$equation[1]);
        }
        return $equation
        
        // inverse hyperbolic trig
      case 'arsinh'
        if (is_numeric$equation[1])) {
          return asinh$equation[1]);
        }
        return $equation
        
      case 'arcosh'
        if (is_numeric$equation[1])) {
          return acosh$equation[1]);
        }
        return $equation
        
      case 'artanh'
        if (is_numeric$equation[1])) {
          return atanh$equation[1]);
        }
        return $equation
        
      default
        throw new Exception'usupported operator '$operator);
                         }
  }
  return $equation