dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Taking a word and decoding it

  1. #1
    Join Date
    Nov 2009
    Location
    Norton, MA
    Posts
    64

    resolved [RESOLVED] Taking a word and decoding it

    I need some help on taking in a word and decoding it. What I'm supposed to do is take the word "dtgcm" and decode it into "break".

    My teacher said that we are supposed to put the alphabet into an array, and explode() it. We are then supposed to take "dtgcm" and explode that. Somehow, I'm supposed to back up by 2 (speaking about the alphabet and the location of the letters) for each letter in "dtgcm" which should output "break".

    Here's what I have so far:

    PHP Code:
    $alphabet "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";
    $codeword "d t g c m";
    $expcode explode(" "$codeword);
    $expalpha explode(", "$alphabet);

    foreach(
    $expcode as $decode)
    {
    echo 
    array_search(d$expcode);
    echo 
    array_search(t$expcode);

    I'm kind of stuck on this so I'm not sure if I'm using array_search in the right way (even though my teacher gave me a hint at that's what I'm supposed to use).

    Any help would be greatly appreciated!

    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,023
    Well, I'd go about it in a rather different way:
    PHP Code:
    <?php
    $encoded 
    'dtgcm';
    $decoded '';
    for(
    $ix 0$size strlen($encoded); $ix $size$ix++)
    {
       if(
    strcmp($encoded[$ix], 'c') < 0)
       {
          
    $decoded .= chr(ord($encoded[$ix]) + 24);
       }
       else
       {
          
    $decoded .= chr(ord($encoded[$ix]) - 2);
       }
    }
    echo 
    $decoded;
    Now the question is: can you figure out why that works, and then explain it to your teacher when s/he asks why you did it that way?

    If you want to pursue the array approach, a few things that would streamline things a bit:
    • You can use the range() function to create your a-z array.
    • Likewise, you don't need to explode the encoded word, just use str_split().
    • You can use array_search() to find the array index in the alphabet array for any given letter


    For extra credit, can you convert whichever method you use into a generalized function that can accept the encoded string and any offset (positive or negative) integer for the decoding offset, so that it will work with any valid offset value (-25 to +25)? (Which incidentally could also be used to encode a string, too.)
    Last edited by NogDog; 12-04-2009 at 01:32 PM.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Nov 2009
    Location
    Norton, MA
    Posts
    64
    Quote Originally Posted by NogDog View Post
    Well, I'd go about it in a rather different way:
    PHP Code:
    <?php
    $encoded 
    'dtgcm';
    $decoded '';
    for(
    $ix 0$size strlen($encoded); $ix $size$ix++)
    {
       if(
    strcmp($encoded[$ix], 'c') < 0)
       {
          
    $decoded .= chr(ord($encoded[$ix]) + 24);
       }
       else
       {
          
    $decoded .= chr(ord($encoded[$ix]) - 2);
       }
    }
    echo 
    $decoded;
    Now the question is: can you figure out why that works, and then explain it to your teacher when s/he asks why you did it that way?

    If you want to pursue the array approach, a few things that would streamline things a bit:
    • You can use the range() function to create your a-z array.
    • Likewise, you don't need to explode the encoded word, just use str_split().
    • You can use array_search() to find the array index in the alphabet array for any given letter


    For extra credit, can you convert whichever method you use into a generalized function that can accept the encoded string and any offset (positive or negative) integer for the decoding offset, so that it will work with any valid offset value (-25 to +25)? (Which incidentally could also be used to encode a string, too.)
    yeah, I wouldn't be able to jump into that and figure it out to explain, lol.
    i still don't think I'm understanding this right. here's what I have so far:

    PHP Code:
    $codeword "d t g c m";
    $explode explode(" "$codeword);

    $alpha range(az);

    foreach (
    $explode as $key => $value)
    {
    echo 
    array_search('b'$alpha);
    echo 
    array_search('r'$alpha);

    the problem with using str_split is that I have to use explode for this assignment.
    the code that i've done above just outputs the numbers 117117117117117. I am confused. Is there something in particular that I'm doing wrong?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,023
    array_search() returns the array index of the matching value. So, once you get that index, to get the transposed letter, add or subtract the offset (-2) in this case and get that array element, instead. (Right now you're echoing the index for 'b' (1) and for 'r' (17) on each iteration of the loop.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Nov 2009
    Location
    Norton, MA
    Posts
    64
    Quote Originally Posted by NogDog View Post
    array_search() returns the array index of the matching value. So, once you get that index, to get the transposed letter, add or subtract the offset (-2) in this case and get that array element, instead. (Right now you're echoing the index for 'b' (1) and for 'r' (17) on each iteration of the loop.)
    I see now that, like you said, is echoing 1 and 17 for every letter in dtgcm.
    I am stumped as how to take the dtgcm code and offset each letter by -2 based on the array indexes of the alphabet.

    Do I need to do something like
    PHP Code:
    array_search('d', -2$alpha
    ?
    I just tried to do
    PHP Code:
    echo array_search('b'$alpha) -2
    but that didn't work. it just output -115-115-115-115-115

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,023
    You need to get the result from the array_search(), apply the -2 to it, then use that result as the index to the array. It can be done it pieces to keep it clear what is going on:
    PHP Code:
    $ix array_search('d'$alpha);
    $ix $ix 2;
    echo 
    $alpha[$ix]; 
    Or you can smash it all into one command to make it look more efficient (though if there is any gain at all you're probably talking a few microseconds):
    PHP Code:
    echo $alpha[array_search('d'$alpha) - 2]; 
    (I hope you don't mind that I'm trying not to just hand you the answer, since this is a lesson and I want you to discover as much of the answer yourself as you can.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Join Date
    Nov 2009
    Location
    Norton, MA
    Posts
    64
    Quote Originally Posted by NogDog View Post
    You need to get the result from the array_search(), apply the -2 to it, then use that result as the index to the array. It can be done it pieces to keep it clear what is going on:
    PHP Code:
    $ix array_search('d'$alpha);
    $ix $ix 2;
    echo 
    $alpha[$ix]; 
    Or you can smash it all into one command to make it look more efficient (though if there is any gain at all you're probably talking a few microseconds):
    PHP Code:
    echo $alpha[array_search('d'$alpha) - 2]; 
    (I hope you don't mind that I'm trying not to just hand you the answer, since this is a lesson and I want you to discover as much of the answer yourself as you can.)
    yes, i don't mind and understand what you mean. i'll try to work with this and see if I can figure it out. I think I might have actually started to get it. I'll post back if I run into anything else.

    Thank you so much for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles