Jump to content

Recommended Posts

Hi! I am trying to write a function that will compare two or more words and return with the number of letters that happen to be in the same place. For example, melting and helping should return that their are 5 letters in the same place, or 5/7.  The function that I have written could probably be written more efficiently. Any help would be appreciated! For some reason this returns zero instead of 5. Basically what I am trying to design is a guess game. Where you have a list of words and in that list is the correct word. You need to find it by guessing. Choosing a word will tell you how many of the characters are in the correct positions.

 

function compare($s1,$s2)
{
$total = 0;
if ($s1{0} == $s2{0}){
	$total + 1;
}
if ($s1{1} == $s2{1}){
	$total + 1;
}
if ($s1{2} == $s2{2}){
	$total + 1;
}
if ($s1{3} == $s2{3}){
	$total + 1;
}
if ($s1{4} == $s2{4}){
	$total + 1;
}
if ($s1{5} == $s2{5}){
	$total + 1;
}
if ($s1{6} == $s2{6}){
	$total + 1;
}
echo $total;
}
compare("melting","helping");

 

function compareWords($word, $guess, $caseSensitive = false) {
  if ($caseSensitive == false) {
    $word = strtolower($word);
    $guess = strtolower($guess);
  }
  $c = strlen($word);
  for ($pos = 0;$pos < $c;$pos++) {
    if ($word{$pos} === $guess{$pos}) $num++;
  }
  return $num;
} // end compareWords 

 

example use:

echo compareWords('something','something'); // output: 9
echo compareWords('Something','something',true); // output: 8
echo compareWords('something','gomthenig'); // output: 3

I put together a temporary form and it works perfectly. However, I would like help on building the other side of the game, such as a tool to kind of hack it. I would like a user to be able to input a list of words, one of them being the unknown answer. This is like a hack or a cheat for it. So if someone tested a word, it tells them the number of characters correct, and then suggests the next best answer. Here is an example, like from the fallout 3 video game. http://perl.hacker.dk/cgi-bin/fallout_hack.pl

Doesn't PHP's existing similar_text() function do this already?

 

No. And neither does levenshtein.  What those 2 functions do (in their own ways) is calculate the shortest amount of "moves" or "changes" it takes to get from one string to another.  So for instance, if I compared

 

"something" vs. "somethings"  : it would take one "move" to make them equal: remove the "s"

 

"something" vs. "osmething" : would return 2

 

etc..

 

which mostly seems like the opposite of what he wants, but it is not always the opposite, so you can't even exploit the oppositeness.  Not really going to get into the why's of that, as frankly, I do not fully understand the those algorithms, just that in short, as mentioned, they try to calculate the shortest amount of changes it would take to make string1 into string2. 

 

Now, if the OP would like to take a different approach to his game or whatever, those 2 functions might indeed be something of interest to him, but again, they can't really be used for what he's asking.

okay well hold up, as far as suggesting the next best answer, something like levenshtein() should do the trick for you.  For some reason in my last post I was still thinking about your OP.  similar_text() and levenshtein() are indeed useful for figuring out a "next best answer" sort of thing.  Why? Because they return how many "steps" it takes to get from one string to another, so you can loop through each wrong answer, comparing them to the right answer, and the smaller the number returned, the "closer" that string is to the answer.

well levenshtein() is a lot more efficient at doing the same thing, and translating the result into a % is trivial.. I suppose if you're just comparing single words you aren't going to even come close to noticing the difference in efficiency, so sure, go for the similar_text and skip the extra step of translating it into %

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.