Blog
eXorithm – Execute Algorithm: All Algorithms
eXorithm – Execute Algorithm: Algorithms Beginning with S
eXorithm – Execute Algorithm: Algorithms Beginning with N
eXorithm – Execute Algorithm: View / Run Algorithm draw_playing_card
function draw_playing_card ($suit, $value, $face_down, $size, $x, $y
{
//based in part on http://www.brainjar.com/css/cards/
switch ($suit) {
case 'diamonds'
$color = 'red'
$symbol = '♦'
break
case 'hearts'
$color = 'red'
$symbol = '♥'
break
case 'clubs'
$color = 'black'
$symbol = '♣'
break
case 'spades'
$color = 'black'
$symbol = '♠'
break
default
return ''
}
if ($face_down) {
$bgcolor = "brown"
} else {
$bgcolor = "#fffff2"
}
// card container
$card = "<div style=""
"border:1px solid black;"
"background-color:$bgcolor;"
"position:absolute;"
"font-size:{$size}px;"
"font-family:Arial,sans-serif;"
"width:3.7em;"
"height:5em;"
"border-radius:.2em;"
"-moz-border-radius:.2em;"
"-webkit-border-radius:.2em;"
"color:$color;"
"left:{$x}px;"
"top:{$y}px;"
"">"
if (!$face_down) {
// number & symbol in top corner
$card .= "<div style=""
"position:absolute;"
"font-size:50%;"
"font-weight:bold;"
"text-align:center;"
"left:0.25em;"
"top:0.25em;"
"">$value<br />$symbol</div>"
// non-face card
if (($value>=2) && ($value<=10)) {
/****** column 1 symbols *****/
if (($value>=4) && ($value<=10)) {
$card .= "<div style="". //row 1
"position:absolute;"
"left:0.6em;"
"top:0.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 5
"position:absolute;"
"left:0.6em;"
"top:3.5em;"
"">$symbol</div>"
}
if (($value==9) || ($value==10)) {
$card .= "<div style="". //row 2
"position:absolute;"
"left:0.6em;"
"top:1.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 4
"position:absolute;"
"left:0.6em;"
"top:2.5em;"
"">$symbol</div>"
}
if (($value==6) || ($value==7) || ($value==8)) {
$card .= "<div style="". //row 3
"position:absolute;"
"left:0.6em;"
"top:2em;"
"">$symbol</div>"
}
/****** column 2 symbols *****/
if (($value==2) || ($value==3)) {
$card .= "<div style="". //row 1
"position:absolute;"
"left:1.55em;"
"top:0.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 5
"position:absolute;"
"left:1.55em;"
"top:3.5em;"
"">$symbol</div>"
}
if (($value==7) || ($value==8) || ($value==10)) {
$card .= "<div style="". //row 2
"position:absolute;"
"left:1.55em;"
"top:1em;"
"">$symbol</div>"
if ($value!=7) {
$card .= "<div style="". //row 4
"position:absolute;"
"left:1.55em;"
"top:3em;"
"">$symbol</div>"
}
}
if (($value==3) || ($value==5) || ($value==9)) {
$card .= "<div style="". //row 3
"position:absolute;"
"left:1.55em;"
"top:2em;"
"">$symbol</div>"
}
/****** column 3 symbols *****/
if (($value>=4) && ($value<=10)) {
$card .= "<div style="". //row 1
"position:absolute;"
"left:2.5em;"
"top:0.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 5
"position:absolute;"
"left:2.5em;"
"top:3.5em;"
"">$symbol</div>"
}
if (($value==9) || ($value==10)) {
$card .= "<div style="". //row 2
"position:absolute;"
"left:2.5em;"
"top:1.5em;"
"">$symbol</div>"
$card .= "<div style="". //row 4
"position:absolute;"
"left:2.5em;"
"top:2.5em;"
"">$symbol</div>"
}
if (($value==6) || ($value==7) || ($value==8)) {
$card .= "<div style="". //row 3
"position:absolute;"
"left:2.5em;"
"top:2em;"
"">$symbol</div>"
}
}
// ace
else if ($value=='A') {
$card .= "<div style=""
"font-size:300%;"
"position:absolute;"
"text-align:center;"
"width:100%;"
"top:0.25em;"
"">$symbol</div>"
}
// jack, king, queen
else if (($value=='J')|| ($value=='Q')|| ($value=='K')) {
if ($value=='J') $sym'♞'
else if ($value=='Q') $sym'♛'
else if ($value=='K') $sym'♚'
$card .= "<div style=""
"border:1px solid black;"
"position:absolute;"
"left:0.6em;"
"top:0.55em;"
"width:2.45em;"
"height:3.85em;"
"">"
$card .= "<div style=""
"font-size:300%;"
"position:absolute;"
"text-align:center;"
"width:100%;"
"">$sym</div>"
$card .= "<div style=""
"position:absolute;"
"left:0.05em;"
"top:0em;"
"">$symbol</div>"
$card .= "<div style=""
"position:absolute;"
"right:0.05em;"
"bottom:0em;"
"">$symbol</div>"
$card .= "</div>"
}
}
$card .= "</div>"
return $card
}
eXorithm – Execute Algorithm: View / Run Algorithm isbn_validator
function isbn_validator ($isbn
{
$isbnpreg_replace'/[^dX]/'''$isbn); //remove all non-numeric or X chars
$casestrlen$isbn);
switch ($case
{
case 10:
if(!isbn_validate$isbn, 2))
return 0; //Failure - not valid ISBN
else
$isbnconv_isbn$isbn);
break
case 13:
$isbnpreg_replace'/[^d]/'''$isbn); //remove all non-numeric chars
if(!isbn_validate$isbn, 1))
return 0; //Failure - not valid ISBN
break
default
return 0;
}
$isbnisbn_hyphenate$isbn);
return $isbn
}
Make Change
<?php /** * make_change * * Calculate the number of different ways there are to make change for a given amount. * * @version 0.1 * @author Contributors at eXorithm * @link /algorithm/view/make_change Listing at eXorithm * @link /algorithm/history/make_change History at eXorithm * @license /home/show/license * * @param number $amount How many cents you want to make change for. * @param array $coins The coin denominations you have. * @return mixed */ function make_change($amount=100,$coins=array(0=>'1',1=>'5',2=>'10',3=>'25')) { $coin_count = count($coins); $table = array(); for ($i = -1; $i <= $amount; $i++) { for($j = -1; $j <= $coin_count; $j++) { // Rules // 1: table[0,0] or table[0,x] = 1 // 2: talbe[i <= -1, x] = 0 // 3: table[x, j <= -1] = 0 $total = 0; // first sub-problem // count(n, m-1) $n = $i; $m = $j-1; if ($n == 0) // rule 1 $total += 1; else if ($n <= -1) // rule 2 $total += 0; else if (($m <= 0) && ($n >= 1)) $total += 0; else $total += $table[$n][$m]; // second sub-problem // count(n-S[m], m) if (($j-1) <= -1) $total += 0; else { $n = $i - $coins[$j - 1]; $m = $j; if ($n == 0) // rule 1 $total += 1; else if ($n <= -1) // rule 2 $total += 0; else if (($m <= 0) && ($n >= 1)) // rule 3 $total += 0; else $total += $table[$n][$m]; } $table[$i][$j] = $total; } } return $table[$i-1][$j-1]; } ?>
eXorithm – Execute Algorithm: View / Run Algorithm unique_image
function unique_image ($string
{
$size=200;
$steps=5;
$step$size$steps
$image = image_create_alpha$size, $size);
$n = 0;
$prev = 0;
$len = strlen$string);
$sum = 0;
for ($i=0;$i$len$i++) $sum += ord$string$i]);
for ($i=0;$i$steps$i++) {
for ($j=0;$j$steps$j++) {
$letter = $string$n++ % $len];
$u = ($n % (ord$letter)+$sum)) + ($prev % (ord$letter)+$len)) + (($sum-1) % ord$letter));
$color = imagecolorallocate$image, pow$u$prev$u$prev+5,2)%256, pow$u$prev$u$prev+3,2)%256, pow$u$prev$u$prev+1,2)%256);
if (($u%2)==0)
imagefilledpolygon$image, array$i$step, $j$step, $i$step$step, $j$step, $i$step, $j$step$step), 3, $color);
$prev = $u
$u = ($n % (ord$letter)+$len)) + ($prev % (ord$letter)+$sum)) + (($sum-1) % ord$letter));
if (($u%2)==0)
imagefilledpolygon$image, array$i$step, $j$step$step, $i$step$step, $j$step$step, $i$step$step, $j$step), 3, $color);
$prev = $u
}
}
return $image
}
eXorithm – Execute Algorithm: View / Run Algorithm morse
function morse ($data, $direction
{
$codes = array
'a' => '.-'
'b' => '-...'
'c' => '-.-.'
'd' => '-...'
'e' => '.'
'f' => '..-.'
'g' => '--.'
'h' => '....'
'i' => '..'
'j' => '.---'
'k' => '-.-'
'l' => '.-..'
'm' => '--'
'n' => '-.'
'o' => '---'
'p' => '.--.'
'q' => '--.-'
'r' => '.-.'
's' => '...'
't' => '-'
'u' => '..-'
'v' => '...-'
'w' => '.--'
'x' => '-..-'
'y' => '-.--'
'z' => '--..'
'0' => '-----'
'1' => '.----'
'2' => '..---'
'3' => '...--'
'4' => '....-'
'5' => '.....'
'6' => '-....'
'7' => '--...'
'8' => '---..'
'9' => '----.'
',' => '--..--'
'.' => '.-.-.-'
'?' => '..--..'
);
$output = ''
if ($direction=='text') {
$codes = array_flip$codes);
$elements = explode' ', $data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element];
}
} else {
$data = strtolower$data);
$elements = str_split$data);
foreach ($elements as $element) {
if (array_key_exists$element, $codes))
$output .= $codes$element].' '
}
$output = trim$output);
}
return $output
}
eXorithm – Execute Algorithm: View / Run Algorithm get_average_color
function get_average_color ($image
{
$y = imagesy$image);
$x = imagesx$image);
$r_total = 0;
$g_total = 0;
$b_total = 0;
$tc = imageistruecolor$image);
for ($ii=0;$ii$y$ii++) {
for ($jj=0;$jj$x$jj++) {
$rgb = imagecolorat$image, $jj, $ii);
if ($tc) {
$r_total += ($rgb >> 16) & 0xFF;
$g_total += ($rgb >> 8) & 0xFF;
$b_total += $rgb & 0xFF;
$count++;
} else {
$rgb = imagecolorsforindex$image, $rgb);
if ($rgb'alpha']==0) {
$r_total += $rgb'red'];
$g_total += $rgb'green'];
$b_total += $rgb'blue'];
$count++;
}
}
}
}
$r = dechexround$r_total/($count)));
$g = dechexround$g_total/($count)));
$b = dechexround$b_total/($count)));
if (strlen$r)==1) $r = '0'$r
if (strlen$g)==1) $g = '0'$g
if (strlen$b)==1) $b = '0'$b
return $r$g$b
}