...

View Full Version : Challange: Anagram



funnymoney
10-22-2009, 10:36 PM
solve an anagram

anagram is $anagram = "ndesjambo";

solution is a $string = "jamesbond";

you need to get a $solution variable with given anagram and string :)


if ($solution == $string) {
echo "true";
}
else {
echo "false";
}



you have to use only control structures, not php embeded functions like array_sorting, slicing or similar...

for the first solution you get thanks!

mlseim
10-22-2009, 10:58 PM
Are we allowed to use anything from here?
http://www.google.com/search?hl=en&q=php+anagram+solver+script&aq=f&oq=&aqi=

and how does it know when it's solved?
do you have some sort of online dictionary with the solved anagrams?

kbluhm
10-22-2009, 11:01 PM
Bah, rules...


function anagram_chars( $str )
{
$str = strtolower( $str );
$str = preg_replace( '/[^a-z0-9]/', '', $str );
return count_chars( $str, 1 );
}

$test = ( anagram_chars( 'ondesjambo' ) == anagram_chars( 'jamesbond' ) );
var_dump( $test ); // bool(false)

$test = ( anagram_chars( 'This is a sentence' ) == anagram_chars( 'A sentence, this is.' ) );
var_dump( $test ); // bool(true)

mlseim
10-23-2009, 02:02 AM
hmmm ...

So the challenge was to simply test if two strings have the same characters?

I have no idea what the challenge means, I guess.

funnymoney
10-23-2009, 04:35 PM
lemme explain a bit better..

you are given this two strings



$anagram = "ndesjambo";

$string = "jamesbond";



with those two strings you need to get a solution


$solution =""; #???

//you need to create some sort of function
//or control structure or similar so that from
//$anagram string with the help of $string string,
//you get $solution = "jamesbond"

//and of course, solution can't be $solution = $string,


//when you get that solution test it like this

if ($solution == $string) {
echo "true";
}
else {
echo "false";
}

//if echoes true you get thanks!

kbluhm
10-23-2009, 04:39 PM
This is rather odd. You could use my anagram_chars() function to return the original test string, or FALSE if it doesn't match up. Aside from that, I'm not really seeing how else it could be accomplished.


$solution = ( anagram_chars( $string ) == anagram_chars( $anagram ) ) ? $string : FALSE;


Otherwise, you'd have to build an array of all possible unique combinations of the original $anagram characters, and then check if $string has been found using in_array()... and the size of that array would grow exponentially based upon the number of characters in $anagram. Any value over 7 or 8 characters would incur a massive amount of overhead.

Are you looking for the most efficient solution, or the most difficult?

funnymoney
10-23-2009, 04:50 PM
Are you looking for the most efficient solution, or the most difficult?

well it's not supposed to be anagram solver, just hard coded script that solves this problem. if someone want's an anagram solver then let him order it.

i thought about solving simply by creating array from given $anagram string an then sort it to fit $string, but without array_key_exists, or in_array and similar built in functions... dunno if it's solvable though couse i'aven't tried it, but i think it should be..

kbluhm
10-23-2009, 05:01 PM
I think I'm doing that here, or are we misunderstanding each other.



function anagram_chars( $str )
{
$str = strtolower( $str );
$str = preg_replace( '/[^a-z0-9]/', '', $str );
return count_chars( $str, 1 );
}

$test = ( anagram_chars( 'ondesjambo' ) == anagram_chars( 'jamesbond' ) );
var_dump( $test ); // bool(false)

$test = ( anagram_chars( 'This is a sentence' ) == anagram_chars( 'A sentence, this is.' ) );
var_dump( $test ); // bool(true)

funnymoney
10-23-2009, 05:10 PM
you just checked weather letters in one are also in another.. By all means, you did write a good script couse now i figured out that anagram is false.. Thanks for that..

BUT, that is not what i wanted.

i realized that variables i gave you before are not anagrams (sorry for that) so these are anagrams


$anagram = "ndesjambo";

$string = "jamesbond";

'o' was extra :P

take a look at the post number #5 for more information of what i wanted

kbluhm
10-23-2009, 05:22 PM
What the heck... if $solution is ultimately equivalent to $string, then we already know what the result is, we just need to test TRUE or FALSE, correct?

$anagram = 'BAC'
$solution = 'ABC'

Do they contain the identical character count? TRUE

$anagram = 'BDC'
$solution = 'ABC'

Do they contain the identical character count? FALSE

What else is left to check?

mlseim
10-23-2009, 05:31 PM
I agree with kbluhm ...

Post #6 is correct in that to shows the idea of solving by combinations. You first have to know
what the "real" string is, then go through every million or so combinations to get there.

What's the point of the challenge?

If you already know the "real string" (solution), you don't have to figure it out.
No point in doing all the looping, as it could be too many combinations for PHP.

If someone were to use kbluhm's script in Post #8 against a dictionary, one might be
able to quickly come-up with a few matching anagrams and quickly solve it. To me,
he has succeeded in the challenge and deserves the diminutive "Thanks". In fact,
it would be interesting to run an anagram against a dictionary database using his script.

funnymoney
10-24-2009, 02:11 AM
well you can use explode of course? i will try to solve this anagram in few days if someone else doesn't do the same. i think i know how...

i hope my solution brings light to darkness just to try other challanges in future.. for all new coders trying to get a shot at solving somewhat difficult challange for them...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum