susanBuck / e15-spring22

0 stars 0 forks source link

Project 1 code #23

Closed susanBuck closed 2 years ago

susanBuck commented 2 years ago

Hi everyone,

I just released Project 1 grades in Canvas, and with all submissions complete I thought I would share the solution code from the P1 demo.

You can view the code here: https://github.com/susanBuck/e15p1

At minimum I recommend taking a scan through the process.php page just to compare/contrast the string processing code to your own solutions.

If you want to share links to your own project code, feel free to reply to this thread and maybe share a bit about your techniques. It's always useful to look at different solutions to the same kind of problem.

gabichuela85 commented 2 years ago

For my project 1 to shift one letter I used the following code:

$shifty=['A' => 'B', 'a' => 'b', 'B' => 'C', 'b' => 'c', 'C' => 'D', 'c' => 'd',
        'D' => 'E', 'd' => 'e', 'E' => 'F', 'e' => 'f', 'F' => 'G', 'f' => 'g',
        'G' => 'H', 'g' => 'h', 'H' => 'I', 'h' => 'i', 'I' => 'J', 'i' => 'j',
        'J' => 'K', 'j' => 'k', 'K' => 'L', 'k' => 'l', 'L' => 'M', 'l' => 'm',
        'M' => 'N', 'm' => 'n', 'N' => 'O', 'n'=> 'o', 'O' => 'P', 'o' => 'p',
        'P' => 'Q', 'p' => 'q', 'Q' => 'R', 'q' => 'r', 'R' => 'S', 'r' => 's',
        'S' => 'T', 's' => 't', 'T' => 'U', 't' => 'u', 'U' => 'V', 'u' => 'v',
        'V' => 'W', 'v' => 'w', 'W' => 'X', 'w' => 'x', 'X' => 'Y', 'x' => 'y',
        'Y' => 'Z', 'y' => 'z', 'Z' => 'A', 'z' => 'a' ];

$coded = strtr($word, $shifty);

While the associative array worked well for shifting one letter to encode the word, using the if else statement like you did in the sample project would make it easier to shift the characters in the word by any number of spaces.

Thanks for sharing!

susanBuck commented 2 years ago

@gabichuela85 - Your use of strtr was really clever; introduced me to a new php function I hadn't encountered before. :)