perl – 找到与其他单词相同的大多数字母的单词

前端之家收集整理的这篇文章主要介绍了perl – 找到与其他单词相同的大多数字母的单词前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望Perl(5.8.8)找出哪个单词与数组中的其他单词具有最多共同字母 – 但只能找到位于同一位置的字母. (最好不使用libs.)

以这个单词列表为例:

> BAKER
> SALER
> BALER
> CARER
> RUFFR

她的BALER是与其他人共有最多字母的词.它匹配BAKER中的BAxER,SALER中的xALER,CARER中的xAxER和RUFFR中的xxxxR.

我希望Perl能够在具有相同长度和大小写的任意单词列表中为我找到这个单词.似乎我在这里碰到了墙,所以非常感谢帮助!

我到现在为止所尝试过的

目前没有太多的脚本:

use strict;
use warnings; 
my @wordlist = qw(BAKER SALER MALER BARER RUFFR);
foreach my $word (@wordlist) {
    my @letters = split(//,$word);
    # now trip trough each iteration and work magic...
}

评论的地方,我尝试了几种代码,包括for循环和变量.到目前为止,我没有尝试过我需要做的事情.

因此,为了更好地解释:我需要的是逐字逐句地测试,对于每个字母位置,找到与列表中其他字母具有最多共同字母的单词,在该字母的位置.

一种可能的方法是首先检查哪个单词在字母位置0处最常见,然后测试字母位置1,依此类推,直到找到总和中具有最多字母的单词.列表中的其他单词.然后我想打印列表,就像一个矩阵,每个字母位置的分数加上每个单词的总分,与DavidO建议的不同.

你实际上最终得到的是每个单词的矩阵,每个字母位置的分数,以及矩阵中每个单词前面的总分.

该计划的目的

呵呵,我不妨这么说:该程序适用于游戏“辐射3”中的黑客终端.:D我的想法是,这是学习Perl同时享受有趣游戏的好方法.

这是我用于研究的Fallout 3终端黑客教程之一:FALLOUT 3: Hacking FAQ v1.2,我已经制作了一个缩短单词列表的程序,如下所示:

#!/usr/bin/perl
# See if one word has equal letters as the other,and how many of them are equal
use strict;
use warnings; 

my $checkword = "APPRECIATION"; # the word to be checked
my $match = 4; # equal to the match you got from testing your checkword
my @checkletters = split(//,$checkword); #/

my @wordlist = qw(
    PARTNERSHIPS
    REPRIMANDING
    CIVILIZATION
    APPRECIATION
    CONVERSATION
    CIRCUMSTANCE
    PURIFICATION
    SECLUSIONIST
    CONSTRUCTION
    DISAPPEARING
    TRANSMISSION
    APPREHENSIVE
    ENCOUNTERING
);

print "$checkword has $match letters in common with:\n";

foreach my $word (@wordlist) {
    next if $word eq $checkword;
    my @letters = split(//,$word);
    my $length = @letters; # determine length of array (how many letters to check)

    my $eq_letters = 0; # reset to 0 for every new word to be tested
    for (my $i = 0; $i < $length; $i++) {
        if ($letters[$i] eq $checkletters[$i]) {
            $eq_letters++;
        }
    }
    if ($eq_letters == $match) {
        print "$word\n";
    }
}
# Now to make a script on to find the best word to check in the first place...

该脚本将生成CONSTRUCTION和TRANSMISSION作为其结果,就像在游戏FAQ中一样.原始问题的诀窍(以及我自己无法找到的东西)是如何找到最好的词来尝试,即APPRECIATION.

好的,我现在已根据您的帮助提供了我自己的解决方案,并考虑关闭此线程.很多,非常感谢所有的贡献者.你帮助很大,而且我也学到了很多东西. :d

解决方法

作为起点,您可以有效地检查他们共有多少个字母:
$count = ($word1 ^ $word2) =~ y/\0//;

但这只有在你遍历所有可能的单词对时才有用,在这种情况下这是不必要的:

use strict;
use warnings;
my @words = qw/
    BAKER
    SALER
    BALER
    CARER
    RUFFR
/;

# you want a hash to indicate which letters are present how many times in each position:

my %count;
for my $word (@words) {
    my @letters = split //,$word;
    $count{$_}{ $letters[$_] }++ for 0..$#letters;
}

# then for any given word,you get the count for each of its letters minus one (because the word itself is included in the count),and see if it is a maximum (so far) for any position or for the total:

my %max_common_letters_count;
my %max_common_letters_words;
for my $word (@words) {
    my @letters = split //,$word;
    my $total;
    for my $position (0..$#letters,'total') {
        my $count;
        if ( $position eq 'total' ) {
            $count = $total;
        }
        else {
            $count = $count{$position}{ $letters[$position] } - 1;
            $total += $count;
        }
        if ( ! $max_common_letters_count{$position} || $count >= $max_common_letters_count{$position} ) {
            if ( $max_common_letters_count{$position} && $count == $max_common_letters_count{$position} ) {
                push @{ $max_common_letters_words{$position} },$word;
            }
            else {
                $max_common_letters_count{$position} = $count;
                $max_common_letters_words{$position} = [ $word ];
            }
        }
    }
}

# then show the maximum words for each position and in total: 

for my $position ( sort { $a <=> $b } grep $_ ne 'total',keys %max_common_letters_count ) {
    printf( "Position %s had a maximum of common letters of %s in words: %s\n",$position,$max_common_letters_count{$position},join(',',@{ $max_common_letters_words{$position} })
    );
}
printf( "The maximum total common letters was %s in words(s): %s\n",$max_common_letters_count{'total'},@{ $max_common_letters_words{'total'} })
);
原文链接:https://www.f2er.com/Perl/171770.html

猜你在找的Perl相关文章