Developers Archive for the 'Developer algorithms' Category

Sudoku

Sudoku Thursday, February 15th, 2007

So the sudoku craze has swept the world. “Everyone” has built sudoku solvers, but where are the generators. This function does just that! It allows you to generate random “solved” sudoku and the associated puzzle. It produces two strings $solution and $puzzle which can be exploded into arrays.

/**
* Method to add puzzle
* puzzle generated using the “Peraita method - Author unknown”
* @param string $difficulty The difficulty raing of the puzzle (used to remove numbers)
* $size the base size of the grid eg. 3 = 9×9 board, 5 = 25*25 board
**/
function generate($difficulty)
{
// set up board size
if($difficulty != 4){
$size = 3;
}else{
$size = 5;
}
// randomize numbers
$numbers = range(1, pow($size, 2));
shuffle($numbers);

// row index sets
$x = 1;
for($i = 1; $i <= pow($size, 2); $i++){
$a = “rowIndex_” . $i; //set up variable names eg $rowIndex_1
for($ii = 1; $ii <= pow($size, 2); $ii++){
${$a}[$ii] = $x; //set up variable eg $rowIndex[0] = 1
$x = $x + 1;
}
$allRows[$i] = $$a; //set up array eg $temp[0] = $rowIndex_1
}
$temp = array_chunk($allRows, $size, true);
foreach($temp as $key => $arrRow){
$a = “arrRow_” . $key; // set up variable names
$$a = $arrRow; // set up variable
$arrAllRows[$key] = $$a; // set up array
}

// column index sets
for($i = 1; $i <= pow($size, 2); $i++){
$a = “colIndex_” . $i; // set up variable names
$x = $i;
for($ii = 1; $ii <= pow($size, 2); $ii++){
${$a}[$ii] = $x; // set up variable
$x = $x + pow($size, 2);
}
$allCols[$i] = $$a; // set up array
}
$temp = array_chunk($allCols, $size, true);
foreach($temp as $key => $arrCol){
$a = “arrCol_” . $key; // set up variable names
$$a = $arrCol; // set up variable
$arrAllCols[$key] = $$a; // set up array
}

// block index sets
$x = 1;
$y = 1;
for($i = 1; $i <= $size; $i++){
for($ii = 1; $ii <= $size; $ii++){
$a = “blockIndex_” . $x; // set up variable names
$z = 1;
for($iii = 1; $iii <= $size; $iii++){
for($iv = 1; $iv <= $size; $iv++){
${$a}[$z++] = $y; // set up variable
$y = $y + 1;
}
$y = $y + ((pow($size, 2)) - ($size));
}
$arrAllBlocks[$x] = $$a; // set up array
$x = $x + 1;
}
$y = ($i * $size) + 1;
}

// set up basic board
for($i = 1; $i <= pow($size, 2); $i++){
foreach($arrAllBlocks as $block){
$temp = $numbers;
foreach($block as $index){
$data[$index] = array_shift($temp);
}
$firstNumber = array_shift($numbers);
$numbers = array_pad($numbers, pow($size, 2), $firstNumber);
}
}
ksort($data);

// shuffle rows
for($i = 0; $i <= $size - 2; $i++){
foreach($arrAllRows as $arrRows){
shuffle($arrRows);
$arrRows = array_slice($arrRows, 0, 2); // takes first 2 rows
foreach($arrRows as $key => $row){
foreach($row as $rowKey => $index){
if($key == 0){
$row_1[$rowKey] = $data[$index];
}else{
$row_2[$rowKey] = $data[$index];
}
}
}
foreach($arrRows as $key => $row){ // swops them
foreach($row as $rowKey => $index){
if($key == 0){
$data[$index] = $row_2[$rowKey];
}else{
$data[$index] = $row_1[$rowKey];
}
}
}
}
}

// shuffle columns
for($i = 0; $i <= $size - 2; $i++){
foreach($arrAllCols as $arrCols){
shuffle($arrCols);
$arrCols = array_slice($arrCols, 0, 2); // takes first 2 columns
foreach($arrCols as $key => $col){
foreach($col as $colKey => $index){
if($key == 0){
$col_1[$colKey] = $data[$index];
}else{
$col_2[$colKey] = $data[$index];
}
}
}
foreach($arrCols as $key => $col){ // swops them
foreach($col as $colKey => $index){
if($key == 0){
$data[$index] = $col_2[$colKey];
}else{
$data[$index] = $col_1[$colKey];
}
}
}
}
}
$solution = implode(”,”, $data);

//remove pairs of numbers symetrically
if($difficulty == 1){
$pairs = 16;
}elseif($difficulty == 2){
$pairs = 22;
}elseif($difficulty == 3){
$pairs = 30;
}else{
$pairs = 170;
}
for($i = 1; $i <= $pairs; $i++){
do{
$number_1 = rand(1, pow($size, 4));
}while($number_1 == (((pow($size, 4) - 1) / 2) + 1));
$data[$number_1] = ‘’;
$number_2 = (pow($size, 4) + 1) - $number_1;
$data[$number_2] = ‘’;
}

$puzzle = implode(”,”, $data);
}

XML to Array

XML to Array Tuesday, February 13th, 2007

This class parses XML into an array:
array
(
[_name] => tag name (case sensitive)
[_attributes] => an array of ‘attribute’=>’value’
[_value] => node’s CDATA
[_children] => node’s children
)

<?php
/**
* ArrayFromXML
*
* @author Pawel Gasiorowski
* @copyright Copyright (c) 2005
* @access public
**/
class ArrayFromXML
{
var $parser;
var $iter = 0;
var $path = array();
var $xml = array();

/**
* ArrayFromXML::ArrayFromXML()
*
* @param string $XML
* @return
**/
function ArrayFromXML($XML)
{
$this->parser = xml_parser_create();

xml_set_object($this->parser, &$this);

xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);

xml_set_element_handler($this->parser, “hanleTagStart”, “hanleTagEnd”);
xml_set_character_data_handler($this->parser, “hanleTagCData”);

xml_parse($this->parser, $XML);
xml_parser_free($this->parser);

$this->xml = $this->xml[’_children’][0];
}

/**
* ArrayFromXML::getEvalPath()
*
* @return
**/
function getEvalPath()
{
return ‘$this->xml[’ . “‘” . implode(”‘][’”, $this->path) . “‘” . ‘]’;
}

/**
* ArrayFromXML::hanleTagStart()
*
* @param object $parser
* @param string $tag
* @param array $attributes
* @return
**/
function hanleTagStart($parser, $tag, $attributes)
{
array_push($this->path, ‘_children’);
array_push($this->path, ($this->iter++));

$e = $this->getEvalPath();
eval ($e . “[’_name’] = \$tag;”);
if ($attributes !== array())
{
eval ($e . “[’_attributes’] = \$attributes;”);
}
}

/**
* ArrayFromXML::hanleTagCData()
*
* @param object $parser
* @param string $cdata
* @return
**/
function hanleTagCData($parser, $cdata)
{
$e = $this->getEvalPath();
eval ($e . “[’_value’] = \$cdata;”);
}

/**
* ArrayFromXML::hanleTagEnd()
*
* @param object $parser
* @param string $cdata
* @return
**/
function hanleTagEnd($parser, $tag)
{
array_pop($this->path);
array_pop($this->path);
}
}

?>

Very simply square root finder

Very simply square root finder Monday, February 12th, 2007

The following code is a simple square root finder. this can be used as mathematical function for
/** SAVE THIS as square.php **/

<title>Square Root Finder</title>
<style>
body {background-color:#000000; font-family:verdana; font-size:8pt;}
table, tr, td {font-family:verdana; font-size:8pt; color:#ffffff;}
input {font-family:verdana; font-size:8pt; color:#ffffff; background-color:#000000; border-left:1px solid white; border-right:1px solid white; border-top:0px; border-bottom:0px; padding:2px;}
select, option {font-family:verdana; font-size:8pt; color:#ffffff; background-color:#000000; border:0px;}
</style>
<center>
<table border=0 style=”background-color:#000000; border:1px solid #ff0000; text-align:center;” cellspacing=0 cellpadding=3>
<tr><td style=”background-color:#000000; border:1px solid #ff0000; text-align:center;”>
<form action=square_out.php method=get>
Number 1: <input type=”text” name=”num1″ style=”border-top:1px solid white; border-bottom:1px solid white;”><br>
<input type=”submit” value=”Square Root It” style=”border:1px; solid white;”>
<input type=”reset” value=”Reset” style=”border:1px; solid white;”>
</form>
</table>
</center>

/** SAVE THIS as square_out.php **/

<?php
$a = $_GET[num1];
$b = sqrt($a);
$c = $b * $b;

echo “The square root of $a is $b”;
echo “<br>” . “<br>”;
echo “Check: $b times $b”;
echo “<br>”;
echo “Answer: $c”;
?>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.