反转anagram解算器首先显示最长的单词

时间:2013-05-09 06:26:48

标签: php anagram

我的一个网站上有一个anagram解算器,用于搜索txt文件中的单词并返回结果。它将首先返回2个字母单词的结果,然后向下滚动至12个字母单词。我喜欢使用文本文件作为我的数据库,因为它会很快返回结果。

我想颠倒结果的顺序,以便在您向下滚动到2个字母单词时首先显示12个字母的单词。这是我的代码,我将解释到目前为止我所做的更改会产生错误,而不是我想要的结果。我很难理解它。

如果需要,我会列出我的代码以及下载txt文件的链接,这是一个单词列表。

链接到文本文件http://guildfit.com/combined.txt

<?
//Code Here//
$l = 'apple';
if(!empty($l)) { ?>
<table cellpadding="5" cellspacing="0" border="0" width="800" style="text-align:left">
<tr>
    <td style="text-align:center; font-weight:bold; width:100%" colspan="20">Click on words to get definitions</td>
</tr>
<?
$time_start = microtime(true);
$l = $l;
$l = trim($l);
$l = strtolower($l);
$l = str_replace(' ', '_', $l); 
$l = preg_replace('/[^\w]/', '', $l);
$len = strlen($l);

$a = array('a' => 1, 'b' => 1, 'c' => 1, 'd' => 1, 'e' => 1, 'f' => 1, 'g' => 1, 'h' => 1, 'i' => 1, 'j' => 1, 'k' => 1, 'l' => 1, 'm' => 1, 'n' => 1, 'o' => 1, 'p' => 1, 'q' => 1, 'r' => 1, 's' => 1, 't' => 1, 'u' => 1, 'v' => 1, 'w' => 1, 'x' => 1, 'y' => 1, 'z' => 1);

function contains($word) {
    $wlen = strlen($word);
    if($wlen < 2 || $wlen > 12) return FALSE;

    //for($i = $wlen; $i > 1; $i--) {
    for($i = 0; $i < $wlen; $i++) {
        $w[$i] = $word[$i];
    }

    $b = $GLOBALS['b'];
    foreach($b as $n => $c) {
        foreach($w as $k => $v) {
            if($v == $c) {
                unset($w[$k]);
                unset($b[$n]);
                break;
            }
        }
    }
    //echo "count: ". count($w) ."\n";
    if(count($w) > 0) return FALSE;
    return TRUE;
}


//for($i = $wlen; $i > 1; $i--) {
for($i = 0; $i < $len; $i++) {
    $b[$i] = $l[$i];
    unset($a[$l[$i]]);
}

$e = 'cat combined.txt';

while(list($k) = each($a)) {
    if(!$e) {
        $e = "grep -v $k combined.txt";
    } else {
        $e .= ' | grep -v '. $k;
    }
}

exec($e, $o);

settype($w, 'array');
foreach($o as $v) {
    if(contains($v)) {
        $w[] = $v;
    }
}

function mycmp($a, $b) {
    $ca = strlen($a);
    $cb = strlen($b);
    if($ca == $cb) return 0;
    if($ca > $cb) return 1;
    return -1;
}
usort($w, 'mycmp');
$wc = 0; 
foreach($w as $v) {
    $c = strlen($v);
    if($wc != $c) {
        echo '';
        echo "<tr><td style='color:#FF0000'><h2>Words anagram from <em>$l</em></h2></td><td style='color:#FF0000'><h2 style='text-align:center'>Scrabble Points</h2></td><td style='color:#FF0000'><h2 style='text-align:center'>Words With Friends Points</h2></td></tr>";
        echo "<tr><td colspan=2><h2>$c letter words</h2></td></tr>";    
    }
    $wc = $c;
    $getvalues = str_split($v);
    $thisletter1 == 0;
    $thisletter1wwf == 0;
    // Scrabble Points
    foreach($getvalues as $letter) {
        if ($letter == 'a' || $letter == 'e' || $letter == 'i' || $letter == 'l' || $letter == 'n' || $letter == 'o' || $letter == 'r' || $letter == 's' || $letter == 't' || $letter == 'u') {
            $thisletter = 1;
        } elseif ($letter == 'd' || $letter == 'g') {
            $thisletter = 2;
        } elseif ($letter == 'b' || $letter == 'c' || $letter == 'm' || $letter == 'p') {
            $thisletter = 3;
        } elseif ($letter == 'f' || $letter == 'h' || $letter == 'v' || $letter == 'w' || $letter == 'y') {
            $thisletter = 4;
        } elseif ($letter == 'k') {
            $thisletter = 5;
        } elseif ($letter == 'j' || $letter == 'x') {
            $thisletter = 8;
        } elseif ($letter == 'q' || $letter == 'z') {
            $thisletter = 10;
        }
        $thisletter1 = $thisletter1+$thisletter;
    }
    // WWF Points
    foreach($getvalues as $letterwwf) {
        if ($letterwwf == 'a' || $letterwwf == 'e' || $letterwwf == 'i' || $letterwwf == 'o' || $letterwwf == 'r' || $letterwwf == 's' || $letterwwf == 't') {
            $thisletterwwf = 1;
        } elseif ($letterwwf == 'd' || $letterwwf == 'l' || $letterwwf == 'n' || $letterwwf == 'u') {
            $thisletterwwf = 2;
        } elseif ($letterwwf == 'g' || $letterwwf == 'h' || $letterwwf == 'y') {
            $thisletterwwf = 3;
        } elseif ($letterwwf == 'b' || $letterwwf == 'c' || $letterwwf == 'f' || $letterwwf == 'm' || $letterwwf == 'p' || $letterwwf == 'w') {
            $thisletterwwf = 4;
        } elseif ($letterwwf == 'k' || $letterwwf == 'v') {
            $thisletterwwf = 5;
        } elseif ($letterwwf == 'x') {
            $thisletterwwf = 8;
        } elseif ($letterwwf == 'j' || $letterwwf == 'q' || $letterwwf == 'z') {
            $thisletterwwf = 10;
        }
        $thisletter1wwf = $thisletter1wwf+$thisletterwwf;
    }

    echo "";
    echo '';
    $thisletter1 = 0;
    $thisletter1wwf = 0;
}
$time_end = microtime(true);
$time = round($time_end - $time_start, 5);
echo "<tr><td style='text-align:center' colspan='20'>Found <strong>". count($w) ."</strong> words in <strong>$time</strong> seconds</td></tr></table>";
}
// End Code Here//
?>

我所尝试的只是尝试反转“for”循环,这是当前有效的循环。请注意,存在2个存在循环的位置。

当前“for”循环工作正常但显示2 - 12

的结果
for($i = 0; $i < $wlen; $i++) {

我尝试将其更改为此尝试从12 - 2

反转顺序
for($i = $wlen; $i > 1; $i--) {

如果您注意到$ wlen在此处被声明为字符串长度,则$ w​​ord是某人从表单字段输入的单词。

$wlen = strlen($word);

当我更改“for”循环时,我在第29行遇到错误

foreach($b as $n => $c) { 

我对此错误感到困惑,因为在此之前$ c未在任何地方声明,但如果您使用第一个“for”循环而不是第二个循环,则脚本可以正常工作。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果更改用于对$w(字谜数组)进行排序的比较函数,则应该颠倒您的顺序。

function mycmp($a, $b) {
    $ca = strlen($a);
    $cb = strlen($b);
    if($ca == $cb) return 0;
    if($ca > $cb) return 1;
    return -1;
}

将其更改为

function mycmp($a, $b) {
    $ca = strlen($a);
    $cb = strlen($b);
    if($ca == $cb) return 0;
    if($ca > $cb) return -1;
    return 1;
}

看看会发生什么。

答案 1 :(得分:0)

根据Patashu的回答,比较功能可以更简单(更容易看到反转时发生的事情):

// Smallest to largest:
function stringLengthCompare($a, $b) {
    return strlen($a) - strlen($b)
}

// Largest to smallest:
function stringLengthCompare($a, $b) {
    return strlen($b) - strlen($a)
}