Decoding Cryptic Solutions: App Academy’s Caesar Cipher
As I worked through the practice problems for the first time in preparing for the first code challenge of App Academy’s admissions process, I found that it was crucial to my long term learning to walk myself through their given solutions as I solved (or got painfully stuck on) each problem for two key reasons.
For one, my solution may work, but It’s never a bad thing to know more than one way to solve a problem, right?
It’s important to check if, and understand exactly why, their logic differs from mine.
You may realize there are some edge cases that your code misses and theirs doesn’t. You may also end up feeling pretty solid about your solution, but gain ideas for refactoring your code to be even cleaner. You may even find that the (super) humans who’ve graciously taken the time to write and provide these problems and solutions for your benefit are just like you (*gasp!), human, and just as capable of missing bugs. How reassuring is that! More on that in my next post...
Secondly, most of their solutions are confusing as hell to read through, especially if I’m stuck and my brain is leaking out of my nostrils!
After taking the time to break down their solutions, I’m able to quickly go back and solve the problem on my own.
More importantly, what I learn in the short term from that process actually sticks when I come back to race the clock against the problem again the next day. You’ll never understand and be able to implement the underlying concepts by memorizing their solutions and regurgitating them into the IDE! Level up on Codewars like the glorious ninja you are after breaking down, absorbing, and applying the logic behind those solutions and see just what the fuck I mean. Just do it.
Their solution to the Caesar Cipher problem was a hot mess to try to read through. Read on, and hopefully my refactor and line-by-line breakdown will be of use to you.
Write a method that takes in an integer `offset` and a string. Produce a new string, where each letter is shifted by `offset`. You may assume that the string contains only lowercase letters and spaces. When shifting "z" by three letters, wrap around to the front of the alphabet to produce the letter "c".
You'll want to use String's `ord` method and Integer's `chr` method.
`ord` converts a letter to an ASCII number code. `chr` converts an ASCII number code to a letter.
You may look at the ASCII printable characters chart:
http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
Notice that the letter 'a' has code 97, 'b' has code 98, etc., up to 'z' having code 122.
You may also want to use the `%` modulo operation to handle wrapping
of "z" to the front of the alphabet.
Difficulty: hard.
Because this problem relies on outside information, we would not give it to you on the timed challenge. :-)
Here’s their solution untouched.
(Tell me you can read through this once and understand exactly what on Earth is going on with the help of the cryptic variable names and unnecessarily long iterator names, and I’ll call you a lier, or a genius, whichever you prefer. In any case, read on...)
Here’s my refactor-breakdown-comment extravaganza:
Don’t memorize the solutions to coding problems (on Codewars, or anywhere else). Refactor them for clarity, and break them down line by line by commenting the shit out of them until your neurons are exploding with understanding. Then immediately go back and solve the problem on your own again. Then high five yourself and move on to the next problem, rinse and repeat. Re-solve the problems over and over until you literally scoff at how quickly you solve them. Then solve harder problems and repeat the same process. ¡Ándale!