duanliao6789 2013-05-09 06:26
浏览 47
已采纳

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

I have an anagram solver on one of my sites that searches for words in a txt file and returns the results. It will return the results from 2 letter words first then as you scroll down up to 12 letter words. I like using a text file as my db because it returns the results so quickly.

I want to reverse the order of results so that it shows 12 letter words first then as you scroll down to 2 letter words. Here is my code and I will explain what changes I have made so far that produce errors and not the results I want. I am having a hard time understand it.

I will list my code as well as a link to download the txt file which is a word list if you need it.

Link to text file 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) ."
";
    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//
?>

What I have tried is simply trying to reverse a "for" loop, here is the loop that works currently. Note there are 2 locations where to loop exists.

The current "for" loop that works fine but shows results from 2 - 12

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

and I tried to change it to this to try and reverse the order from 12 - 2

for($i = $wlen; $i > 1; $i--) {

If you notice $wlen is declared as a string length here, $word being the word someone inputs from a form field.

$wlen = strlen($word);

When I change the "for" loop I get an error at line 29 which is

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

Im confused about this error because $c is not declared anywhere before this point, yet the script works fine if you use the first "for" loop and not the second.

Any ideas?

  • 写回答

2条回答 默认 最新

  • duanguoping2016 2013-05-09 06:37
    关注

    Your ordering should be reversed if you change the comparison function used to sort $w, the array of anagrams.

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

    Change it to

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

    And see what happens.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值