Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Challange: Anagram

    solve an anagram

    anagram is $anagram = "ndesjambo";

    solution is a $string = "jamesbond";

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

    PHP Code:
    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!
    Last edited by funnymoney; 10-23-2009 at 04:12 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Are we allowed to use anything from here?
    http://www.google.com/search?hl=en&q...&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?

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Bah, rules...
    PHP Code:
    function anagram_chars$str )
    {
        
    $str strtolower$str );
        
    $str preg_replace'/[^a-z0-9]/'''$str );
        return 
    count_chars$str);
    }

    $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) 
    Last edited by kbluhm; 10-22-2009 at 10:07 PM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    lemme explain a bit better..

    you are given this two strings

    PHP Code:
    $anagram "ndesjambo";

    $string "jamesbond"
    with those two strings you need to get a solution
    PHP Code:
    $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! 
    Last edited by funnymoney; 10-23-2009 at 04:12 PM.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.
    PHP Code:
    $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?
    Last edited by kbluhm; 10-23-2009 at 03:47 PM.

  • #7
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by kbluhm View Post
    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..

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I think I'm doing that here, or are we misunderstanding each other.
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    function anagram_chars$str )
    {
        
    $str strtolower$str );
        
    $str preg_replace'/[^a-z0-9]/'''$str );
        return 
    count_chars$str);
    }

    $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) 

  • Users who have thanked kbluhm for this post:

    funnymoney (10-23-2009)

  • #9
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    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

    PHP Code:
    $anagram "ndesjambo";

    $string "jamesbond"
    'o' was extra :P

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

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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?

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.
    Last edited by mlseim; 10-23-2009 at 04:34 PM.

  • #12
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •