Jump to content

help with program


ghqwerty

Recommended Posts

ok so i was bored and decided to write a prgram that will be able to work out what you typed in and then say how many attempts it took to get this answer

 

this is what i have so far

 

<?php	
	$letter = array(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
	for($a=0; $a < 36; $a++){			
		if($letter[$a] == $_POST['text']){
			echo $letter[$a];	
			$time = $a+1;
			echo "<br>It took ". $time ." attempts to get the correct word.";			
		}else{

			for($b=0; $b < 36; $b++){
				$stringa = $letter[$a]."".$letter[$b];
				if($stringa == $_POST['text']){					
					echo $stringa;
					$time1 = (36*($a+1))+($b+1);
					echo "<br>It took ". $time1 ." attempts to get the correct word.";
				}
			}
		}  
	}

?>

however if you check out the site

www.blisswars.net23.net/skills.php

and enter 1-9 it will come out all funny.

 

as far as i can tell i think most of the rest works fine.

 

can anyone see the problem ???

 

Edit : the link actually redirects to the right bit now

Link to comment
Share on other sites

well it is supposed to echo what you inputted and how many attempts it took to get this by trial and error

 

so it checks a, if it is not a, it checks b and so on.

 

then say it was z it would echo z and

It took 26 attempts to get the correct word

 

however if you try input 1 you get

01

It took 1000 attempts to get the correct word.1

It took 28 attempts to get the correct word. 

 

whereas it shoudl just be the bottom one

Link to comment
Share on other sites

OK, here's some working code. But beware that this is not a worthwile exercise. The number of combinations needed to find a work increases exponentially with every letter. The word 'apple' took 2,447,285 iterrations to find (and that's only because it starts with the letter a). It took so long for th eword 'house' that I gave up.

 

Of course I didn't write this for efficiency - nonetheless it is a very intensive process.

 

<?php

if (isset($_POST['search_word']))
{

    $letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m',
                     'n','o','p','q','r','s','t','u','v','w','x','y','z',
                     '0','1','2','3','4','5','6','7','8','9');

    $search_word = trim($_POST['search_word']);
    $found_array = array();
    $found_word = '';
    $found = false;

    while ( !$found && count($found_array)<=strlen($search_word) )
    {
        $attempts++;
        $change_letter = false;

        //Starting from the last index (letter) check if the last letter is used
        //If not increase by one
        for ($found_idx=(count($found_array)-1); $found_idx>=0; $found_idx--)
        {
            if ($found_array[$found_idx]<(count($letters)-1))
            {
                $found_array[$found_idx]++;
                $change_letter = true;
                //Reset remaining letters to 0 letter index
                for($found_idx++; $found_idx<count($found_array); $found_idx++)
                {
                    $found_array[$found_idx] = 0;
                }
                break;
            }
        }

        //If no letters can be increased reset all to first letter and add one more
        if (!$change_letter)
        {
            //Reset all values to 0 letter and add 1 more
            foreach($found_array as $key => $value)
            {
                $found_array[$key] = 0;
            }
            $found_array[] = 0;
        }

        //Create the found word for comparisson
        $found_word = '';
        foreach ($found_array as $letter_index)
        {
            $found_word .= $letters[$letter_index];
        }

        //Compare with user entered value
        if (strtolower($found_word)==strtolower($search_word))
        {
            $found=true;
        }
    } 

    if ($found==true)
    {
        $result = "Found your word '$found_word' in $attempts attempts";
    }
    else
    {
        $result = "Did not find your word. You may have used some invalid characters";
    }

}


?>
<html>
<head></head>
<body>
<?php echo $result; ?>
<form action="" method="POST">
Your Word: <input type="text" name="search_word"><br>
<button type="submit">Submit</button>

</form>
</body>
</html>

Link to comment
Share on other sites

Your scipt doesn't even work for aaa. For this you really need to understand combination's and permutations.

 

Supercalifragicictious lol.

 

Did you even run the script or are you saying that from just looking at it? When I run it for 'aaa' I get this result:

Found your word 'aaa' in 1333 attempts

 

And I do understand the difference between permutations and combninations. This script goes in a very systematic,logical sequence to come up will all permutations. If the code only used three letters (a, b & c) it runs in this manner

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
etc.

 

Which will cover every permutation at each character count and then increases the character count by one.

Link to comment
Share on other sites

thanks mjdamato and dink i will add i lloop when i finish the code so it will do it for a infinite amount of letter/numbers :)

 

thanks mjdamato although i'd really rather write it myself too learn from it however i will read yours and then try and make mine similiar to yours to see how it works. thanks :)

Link to comment
Share on other sites

There is no loop needed. The script already loops until the test string ($found_word) is of a greater length than the word you are trying to find ($search_word).

 

The only time it will not find the word is if it includes characters not within the search parameters - which the script also handles. However, it would be more efficient to do a simple validation of the user's word to see if it has any disallowed characters before running the loop to begin with.

Link to comment
Share on other sites

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.