Skip to content

Instantly share code, notes, and snippets.

@mjyoung
Forked from mjseaman/Rotationalcipher.md
Created April 25, 2014 15:19
Show Gist options
  • Save mjyoung/11293274 to your computer and use it in GitHub Desktop.
Save mjyoung/11293274 to your computer and use it in GitHub Desktop.

Please complete the following challenge and submit your response in a gist.

Write your code with the following priorities in mind, in order from most to least important:

  • Correctness and completeness
  • Clarity and readability
  • Conciseness and performance

Without further ado:

A rotational cipher, ROT(X), is an ancient encoding that, given a message, returns a message where each letter has been replaced by the letter exactly X letters after it in the alphabet ordering. Letters near the end of the alphabet wrap around. For example, the translation table for ROT(13) looks like:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

NOPQRSTUVWXYZABCDEFGHIJKLM

and therefore the message HELLO becomes URYYB

Please implement rotx in Ruby, which given a rotation number and a string, outputs the string rotated by that many letters. It should preserve capitalization and ignore any non-alphabetic character. It should also take a parameter that decrypts the string instead of encrypting it. For example:

def rotx(x, string, encrypt=true)
  # Your implementation here...
end

rotx 10, 'Hello, World'
# => "Rovvy, Gybvn"
rotx 10, 'Rovvy, Gybvn', false
# => "Hello, World"

# Rotation numbers greater than 26 should work as well
rotx 36, 'Hello, World'
# => "Rovvy, Gybvn"

Good luck!

Side note: you could find plenty of examples of this code online. Since this is a test of our candidates' ability to solve problems rather than search Google for solutions, we ask that you complete this challenge without checking someone else's implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment