function word_counts ($text, $noise
{
  $words = preg_split'/[^A-Za-z]+/', strtolower$text));
  $counts = array();
 Â
  foreach ($words as $word) {
    if (strlen$word)>1) { // 1-letter words are ignored
      if (array_search$word, $noise)===false) { // noise word?
        if (array_key_exists$word, $counts)) {
          $counts$word] = $counts$word]+1;
        } else {
          $counts$word] = 1;
        }
      }
    }
  }
 Â
  return $counts
}Â
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm sum_list
function sum_list ($numbers
{
return array_sum$numbers);
}
eXorithm – Execute Algorithm: View / Run Algorithm validate_email
function validate_email ($email
{
if (preg_match"%^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$%i", $email))
return true;
else
return false;
}
eXorithm – Execute Algorithm: View / Run Algorithm binary_search
function binary_search ($array, $item, $low, $high
{
if ($low==-1) $low = 0;
if ($high==-1) $high = count$array)-1;
if ($low > $high) {
// item not found
return -1;
}
// get the middle
$middle = floor(($low$high)/2);
if ( $array$middle] == $item ) {
// found it
return $middle
} elseif ($item < $array$middle]) {
// search left
return binary_search$array, $item, $low, $middle-1);
} else {
// search right
return binary_search$array, $item, $middle+1, $high);
}
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_cube
function draw_cube ($image_size, $degree_x, $degree_y, $degree_z, $vdist, $dist, $vertex_color, $face_color, $wireframe, $dashes, $rainbow
{
$degree_x = $degree_x % 360;
$degree_y = $degree_y % 360;
$degree_z = $degree_z % 360;
// construct the cube polygons
$size = 400; // the size is arbitrary
$x1$size/2;
$x0=-($size/2);
$y1$size/2;
$y0=-($size/2);
$z1$size/2;
$z0=-($size/2);
$sides = array();
$sides[] = array$x0$y0$z0, $x0$y0$z1, $x0$y1$z1, $x0$y1$z0);
$sides[] = array$x1$y0$z0, $x1$y0$z1, $x1$y1$z1, $x1$y1$z0);
$sides[] = array$x0$y0$z0, $x0$y0$z1, $x1$y0$z1, $x1$y0$z0);
$sides[] = array$x0$y1$z0, $x0$y1$z1, $x1$y1$z1, $x1$y1$z0);
$sides[] = array$x0$y0$z0, $x0$y1$z0, $x1$y1$z0, $x1$y0$z0);
$sides[] = array$x0$y0$z1, $x0$y1$z1, $x1$y1$z1, $x1$y0$z1);
// project each of the 6 polygons that makes up the cube
for ($i=0; $icount$sides); $i++) {
$points[] = project_polygon$sides$i], $degree_x, $degree_y, $degree_z, 0, 0, 0, $vdist+($size/2), $dist+($size/2), true);
}
// scale the image somewhat
$scale = $image_size/($size*1.8);
if ($rainbow) {
$face_color = array'ff0000', '00d000', 'ffff00', 'a000a0', '0000ff', 'FF8040');
}
return render_polygons$points, $vertex_color, $face_color, $wireframe, $dashes, $image_size, $scale);
}
eXorithm – Execute Algorithm: View / Run Algorithm point_in_polygon
function point_in_polygon ($point, $polygon_points
{
  // check points
  if ((count$polygon_points)%2)!=0) {
    throw new Exception'The points must be a list like x1, y1, x2, y2, etc. The number of points therefore must be divisible by two.');
  }
 Â
  // count the number of times a flat line originating from the point and moving right crosses
  // the edges of the polygon -- even number: outside polygon, odd number: inside polygon
  $ints = 0;
 Â
  $count = count$polygon_points);
 Â
  $x = $point[0];
  $y = $point[1];
 Â
  for ($i=2;$i<=$count$i$i+2) {
    $vertex1x = $polygon_points$i-2];Â
    $vertex1y = $polygon_points$i-1];Â
    $vertex2x = $polygon_points$i % count$polygon_points)];
    $vertex2y = $polygon_points[($i+1) % count$polygon_points)];
    // boundary condition: if the point is one of the vertices then we are inside
    if (($x == $vertex1x) && ($y == $vertex1y)) {
      return true;
    }
    if ($vertex1y == $vertex2y) { // horizontal edge
      // boundary condition: if point is on a horizontal polygon edge then we are inside
      if (($vertex1y == $y) && ($x > min$vertex1x, $vertex2x)) && ($x < max$vertex1x, $vertex2x))) {
        return true;
      }
    } else {
      if (($y > min$vertex1y, $vertex2y)) && ($y <= max$vertex1y, $vertex2y)) && ($x <= max$vertex1x, $vertex2x))) {Â
        $xinters = ($y - $vertex1y) * ($vertex2x - $vertex1x) / ($vertex2y - $vertex1y) + $vertex1x;Â
        // boundary condition: if point is on the polygon edge then we are inside
        if ($x == $xinters) {
          return true;
        }
        if ($x < $xinters) {Â
          $ints++;Â
        }
      }Â
    }Â
  }
 Â
  // if line crosses edges even number of times, then we are outside polygon
  if (($ints % 2) == 0) {
    return false;
  } else {
    return true;
  }
}Â
eXorithm – Execute Algorithm: View / Run Algorithm xithm_world_stats_population
function xithm_world_stats_population ($country_list
{
$connectionmysql_connect"xithmdb.aktiv.com""duppie""exorithm");
$y = array();
if$connection) {
$db=@mysql_select_db"xithm"$connection);
$sql = "select * from country_population"
ifis_array$country_list)){
ifcount$country_list) > 0) {
$sql .= " where country in ("
for ($ii=0; $iicount$country_list); $ii++) {
$sql .= "'"mysql_real_escape_string$country_list$ii])."',"
}
$sql .= "'')"
}
}
$mysql_result=@mysql_query$sql$connection);
while ($rowmysql_fetch_array$mysql_result)) {
$y$row[0]] = $row[1];
}
@mysql_close$connection);
} else {
throw new Exception"Could not connect.");
}
return $y
}
eXorithm – Execute Algorithm: View / Run Algorithm invert_image
function invert_image ($image
{
$image_width = imagesx$image);
$image_height = imagesy$image);
// if the image is not true color, make it so
if (!imageistruecolor$image)) {
$image2 = imagecreatetruecolor$image_width, $image_height);
imagecopy$image2$image,0,0,0,0,$image_width$image_height);
$image = $image2
}
// loop through all the pixels
for ($h = 0; $h < $image_height; $h++) {
for ($w = 0; $w < $image_width; $w++) {
// get the color at this pixel
$color = imagecolorsforindex$image, imagecolorat$image, $w, $h));
// invert the color
$color'red'] = 255 - $color'red'];
$color'green'] = 255 - $color'green'];
$color'blue'] = 255 - $color'blue'];
// create the new color
$new_color = imagecolorallocate$image, $color'red'], $color'green'], $color'blue']);
// set the color
imagesetpixel$image, $w, $h, $new_color);
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm validate_domain
function validate_domain ($domain
{
  return preg_match ("/^[a-zA-Z0-9-.]+.(com|org|net|mil|edu)$/i", $domain);
}Â
eXorithm – Execute Algorithm: View / Run Algorithm html_select
function html_select ($name, $items, $selected, $use_keys
{
$html = "<select name="$name">n"
foreach ($items as $key=>$value) {
if (!$use_keys) $key = $value
if ($selected == $key
$s = ' selected'
else
$s = ''
$html .= "<option value=""$key""$s>"$value"</option>n"
}
$html .= "</select>"
return $html
}