Unscramble A Word
";
mysql_query("insert into sortedWords values('$sortedWord', '$word')", $db);
echo "Added $newWord to dictionary.
";
}
$startTime = getmicrotime();
if (strlen($string) > 0) {
permutate($string, true);
}
$endTime = getmicrotime();
$totalTime = $endTime - $startTime;
# print "Number of permutations: $numPermutations
";
$start = getmicrotime();
$allWords = array();
#sort($checked);
#print "Checking " . count($checked) . " different combinations.
";
for ($i = 0; $i < count($checked); $i++) {
$next = $checked[$i];
# print "$next
";
$result = mysql_query("select word from sortedWords where keyword='$next'", $db);
print mysql_error($db);
for ($j = 0; $j < mysql_num_rows($result); $j++) {
array_push($allWords, mysql_result($result, $j, "word"));
}
}
$processing = getmicrotime() - $startTime;
echo "Processed in $processing seconds.
";
print "Found " . count($allWords) . " words
";
sort($allWords);
#for ($i = 0; $i < count($allWords); $i++) {
# print $allWords[$i] . "
";
#}
for ($j = 3; $j <= strlen($string); $j++) {
for ($i = 0; $i < count($allWords); $i++) {
if ($i == 0) echo "$j letter words
";
if (strlen($allWords[$i]) == $j) {
echo $allWords[$i];
echo "
";
}
}
print "
";
}
$total = getmicrotime() - $start;
# print "Words looked up in $total seconds.
";
function permutate($string, $super) {
global $numPermutations;
$numPermutations++;
$string = strtolower($string);
$length = strlen($string);
$letters = explode(" ", chunk_split($string, 1, " "));
sort($letters);
# for ($k = 0; $k < count($letters); $k++) {
# print "($k, *" . $letters[$k] . "*)
";
# }
$startWord = implode("", $letters);
# print "($startWord, $length)
";
global $checked;
if (in_array($startWord, $checked)) {
# echo "Not checking $startWord, already checked.
";
}
else {
array_push($checked, $startWord);
$end = false;
}
if ($super) {
if ($length > 3) {
# print "Length = $length
";
for ($i = 1; $i <= $length; $i++) {
# print "In i loop: length = $length, string = *$string*
";
//create string with one character missing
$buildMe = "";
for ($j = 1; $j <= $length; $j++) {
if ($j != $i) {
$buildMe = $buildMe . $letters[$j];
# print "Adding " . $letters[$j] . " to word. (i = $i, j = $j)
";
}
}
if (!in_array($buildMe, $checked)) {
# print "permutating $buildMe
";
permutate($buildMe, true);
}
else {
# print "rejected $buildMe
";
}
}
}
}
}
function connectDB() {
$db = mysql_pconnect();
//check for database connection -- if not, then exit
if (!$db) {
echo "Database is currently down. Please try again later.";
exit;
}
mysql_select_db("", $db);
return $db;
}
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
?>