Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Last active September 11, 2015 21:48
Show Gist options
  • Select an option

  • Save al-the-x/f28e2d4f3b8b18ffc8b5 to your computer and use it in GitHub Desktop.

Select an option

Save al-the-x/f28e2d4f3b8b18ffc8b5 to your computer and use it in GitHub Desktop.
Coding Dojo for TIY-Durham FEE on 2015-09-11
var assert = require('chai').assert;
// [The ROT13 cipher](https://en.wikipedia.org/wiki/ROT13) is a simple extension
// of this idea: rotate every character in every word by _13_ places, excluding
// spaces. For example, here's a common printing phrase with ROT13 encoding:
//
// > The Quick Brown Fox Jumps Over The Lazy Dog
// > Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt
//
// Since the English alphabet has exactly 26 characters, what happens if you
// ROT13 an already rotated phrase? Well, you'll find out soon enough!
// Either way, you'll want to give yourself some more test cases than you're provided.
//
// Hey, while you're learning about all this rotating stuff, you should finish
// that `decode` function I started, then refactor `encode` to do the same kind
// of thing. Pair up, and commit often!
/**
* The function `reverse` computes the reversal
* of a given `String` (don't copy-pasta!)
*
* @param {String} S to reverse
* @return {String}
*/
function reverse(S){
if ( S.length < 2 ){
return S;
}
var reversed = '', N = 1;
while (N <= S.length){
reversed = reversed + S[S.length - N];
N = N + 1;
}
// FIXME: Can you refactor `while` to `for`...?
return reversed;
}
it('should reverse a string (for practice)', function(){
/**
* input | output
* =======|========
* "" | ""
* "A" | "A"
* "B" | "B"
* "a" | "a"
* "b" | "b"
* "AB" | "BA"
* "aB" | "Ba"
* "cat" | "tac"
* "dog" | "god"
* "bird" | "drib"
* "math" | "htam"
*/
assert.equal(reverse(""), ""); // Really?
assert.equal(reverse("A"), "A"); // Jerk.
assert.equal(reverse("B"), "B");
assert.equal(reverse("a"), "a");
assert.equal(reverse("b"), "b");
assert.equal(reverse("AB"),"BA");
assert.equal(reverse("Ba"),"aB");
assert.equal(reverse("cat"), "tac");
assert.equal(reverse("dog"), "god");
assert.equal(reverse("ward"), "draw");
assert.equal(reverse("bird"), "drib");
assert.equal(reverse("books"), "skoob"); // Rokey, dokey...
assert.equal(
reverse("we don't want no trouble"),
"elbuort on tnaw t'nod ew"
);
}); // END reverse
/**
* Function `encode` accepts a `String` and produces
* the appropriate ROT13 "encoded" version, i.e. every
* character in `phrase` is "rotated" ahead by 13 characters.
*
* @see String.prototype.charCodeAt
* @see String.prototype.fromCharCode
* @see http://en.wikipedia.org/wiki/ROT13
*
* // Start with just `phrase`...
* @param {String} phrase to encode
* // Add `N` in part 2!
* // @param {Number} N rotation to apply, default 13
* @return {String} encoded with ROT13
*/
function encode(phrase/*, N */){
// YOUR CODE HERE... EMPHASIS ON **YOUR**
}
/**
* Function `decode` accepts a `phrase` and `N` and
* decoded it appropriately, i.e. every _word_ character
* in `phrase` is rotated backward by `N` characters.
*
* @param {String} phrase to decode
* @param {Number} N rotation to apply, default 13
* @return {String} decoded by ROT-N
*/
function decode(phrase, N){
// YOUR CODE HERE
}
it('should encode (ROT13) some words', function(){
// Produce more examples, please...
assert.equal(encode("hello"), "uryyb");
assert.equal(encode("uryyb"), "hello");
}); // END ROT13
it.skip('should encode (ROT-n) some words', function(){
assert.equal(encode("hello", 2) === "jgnnq");
assert.equal(decode("jgnnq", 2) === "hello");
}); // END ROT-n
@al-the-x
Copy link
Copy Markdown
Author

Me and @drumslayert rockin' the dojo!

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