function sorted ($array
{
$len = count$array);
for$i=1; $i$len; $i++) {
if$array$i-1] > $array$i])
return false;
}
return true;
}
Category: View
eXorithm – Execute Algorithm: View / Run Algorithm magic8ball
function magic8ball ()
{
$answers =array'It is certain', 'It is decidedly so', 'Without a doubt'
'Yes – definitely', 'You may rely on it', 'As I see it, yes'
'Most likely', 'Outlook good', 'Signs point to yes', 'Yes'
'Reply hazy, try again', 'Ask again later'
'Better not tell you now', 'Cannot predict now'
'Concentrate and ask again', 'Don't bet on it'
'My reply is no', 'My sources say no', 'Outlook not so good'
'Very doubtful' );
$index = rand(0, count$answers));
return ($answers$index]);
}
eXorithm – Execute Algorithm: View / Run Algorithm round_up
function round_up ($num, $precision
{
$pow = pow(10, $precision);
return ceil$num * $pow)/$pow
}
eXorithm – Execute Algorithm: View / Run Algorithm books_by
function books_by ($author
{
// construct the query
$query = array'limit'=>100,
'name'=>null,
'type'=>'/book/written_work',
'author'=>array'name'=>$author));
// issue the query
$results = freebase_query$query);
$books = array();
foreach ($results as $result) {
if (isset$result'name'])) {
$books[] = $result'name'];
}
}
return $books
}
eXorithm – Execute Algorithm: View / Run Algorithm diceRoll
function diceRoll ($numSides, $numDice
{
$rolls = array();
/*Rolls each die and stores it's value while also keeping a running
total of the rolls.*/
for ($i = 0; $i < $numDice; $i++)
{
$dieValue = rand(1, $numSides);
$rollTotal += $dieValue
Array_push$rolls, $dieValue);
}
//Adds the roll total to the final index.
Array_push$rolls, $rollTotal);
return $rolls
}
eXorithm – Execute Algorithm: View / Run Algorithm freebase_query
function freebase_query ($query
{
$queryarray = array'q'=>array'query'=>array$query)));
$jsonquerystr = json_encode$queryarray);
$jsonquerystr = urlencode$jsonquerystr);
$apiendpoint = "http://api.freebase.com/api/service/mqlread?queries"
$ch = curl_init();
curl_setopt$ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt$ch, CURLOPT_HEADER, 0);
curl_setopt$ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec$ch);
curl_close$ch);
$resultarray = json_decode$jsonresultstr, true);
if (isset$resultarray'q']['result'])) {
return $resultarray'q']['result'];
} else {
if (isset$resultarray'q']['messages'][0]['message'])) {
throw new Exception"Error: "$resultarray'q']['messages'][0]['message']);
} else {
throw new Exception"Error");
}
}
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_triangle
function draw_triangle ($points, $color
{
if (count$points)!=6) {
throw new Exception'The points must be an array of 6 integers.');
}
$image = image_create_alphamax$points[0], $points[2], $points[4])+1, max$points[1], $points[3], $points[5])+1);
$red = hexdecsubstr$color, 0, 2));
$green = hexdecsubstr$color, 2, 2));
$blue = hexdecsubstr$color, 4, 2));
$color = imagecolorallocatealpha$image, $red, $green, $blue, 0);
imagefilledpolygon$image, $points, 3, $color);
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm simple_recursor
function simple_recursor ($count
{
if ($count>=10) {
return $count
} else {
$x = simple_recursor$count+1);
return $x" - "$count
}
}
eXorithm – Execute Algorithm: View / Run Algorithm draw_pyramid
function draw_pyramid ($image_size, $degree_x, $degree_y, $degree_z, $vdist, $dist, $vertex_color, $side_color, $wireframe, $dashes
{
$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$x1*-1;
$y1$size/2;
$y0$y1*-1;
$z1$size/2;
$z0$z1*-1;
$sides = array();
$sides[] = array$x0$y0$z0, 0,0,$z1, $x0$y1$z0);
$sides[] = array$x1$y0$z0, 0,0,$z1, $x1$y1$z0);
$sides[] = array$x0$y0$z0, 0,0,$z1, $x1$y0$z0);
$sides[] = array$x0$y1$z0, 0,0,$z1, $x1$y1$z0);
$sides[] = array$x0$y0$z0, $x0$y1$z0, $x1$y1$z0, $x1$y0$z0);
// 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.3);
return render_polygons$points, $vertex_color, $side_color, $wireframe, $dashes, $image_size, $scale);
}
eXorithm – Execute Algorithm: View / Run Algorithm add_transparency
function add_transparency ($image, $percent
{
$x = imagesx$image);
$y = imagesy$image);
$image2 = image_create_alpha$x, $y);
imagecopy$image2, $image, 0, 0, 0, 0, $x, $y);
imagesavealpha$image2, true);
if ($percent>0) {
for ($ii=0;$ii$x$ii++) {
for ($jj=0;$jj$y$jj++) {
$c = imagecolorat$image2, $ii, $jj);
$r = ($c >> 16) & 0xFF;
$g = ($c >> 8) & 0xFF;
$b = $c & 0xFF;
$alpha = ($c >> 24) & 0xFF;
$c = imagecolorallocatealpha$image2, $r, $g, $b, $alpha+($percent/100*(127-$alpha)));
imagesetpixel$image2, $ii, $jj, $c);
}
}
}
return $image2
}