Skip to content

Instantly share code, notes, and snippets.

@toddbranch
Created April 19, 2015 19:04
Show Gist options
  • Select an option

  • Save toddbranch/7c1730c7989ce24867a8 to your computer and use it in GitHub Desktop.

Select an option

Save toddbranch/7c1730c7989ce24867a8 to your computer and use it in GitHub Desktop.
function getDimensions(str) {
for (var height = Math.floor(Math.sqrt(str.length)); height > 0; height--) {
if (str.length % height === 0) {
break;
}
}
return {
height: height,
width: str.length / height
};
}
function packSentence(str) {
str = str.replace(/\s+/g, '');
var width = getDimensions(str).width;
var parts = [];
for (var i = 0; i < str.length; i += width) {
parts.push(str.substr(i, width));
}
var map = parts.map(function (row, index) {
if (index % 2) {
return row.split('').reverse().join('');
}
return row;
});
return {
position: [1, 1],
map: map
};
}
var testSentences = [
'EVERYWHERE IS WITHIN WALKING DISTANCE IF YOU HAVE THE TIME',
'IT IS RAINING CATS AND DOGS OUT THERE'
];
describe('#packSentence', function () {
it('should have a rectangular shape', function () {
// all rows should be the same length1
testSentences.forEach(function (testSentence) {
var map = packSentence(testSentence).map;
var rowLength = map[0].length;
map.forEach(function (row) {
expect(row.length).toBe(rowLength);
});
});
});
function getHistogram(array) {
return array.reduce(function (prev, curr) {
if (!prev[curr]) {
prev[curr] = 1;
} else {
prev[curr] += 1;
}
return prev;
}, {});
}
it('should have the same characters as the input', function () {
// all letters should occur with the same frequency
testSentences.forEach(function (testSentence) {
var expectedChars = testSentence.replace(/ /g, '').split('');
var result = packSentence(testSentence);
var outputChars = result.map.reduce(function (prev, curr) {
return prev + curr;
}, '').split('');
expect(getHistogram(outputChars)).toEqual(getHistogram(expectedChars));
});
});
function getChar(map, position) {
// does this position exist in the bounds of the map?
if (position[0] < 1 || position[0] > map[0].length ||
position[1] < 1 || position[1] > map.length) {
return;
}
return map[position[1] - 1].charAt(position[0] - 1);
}
function pathExists(map, position, string) {
// we've found a path!
if (string.length === 0) {
return true;
}
// this can't be a path
if (getChar(map, position) !== string.charAt(0)) {
return false;
}
var newString = string.substr(1);
// keep searching!
return pathExists(map, [position[0] - 1, position[1]], newString) ||
pathExists(map, [position[0] + 1, position[1]], newString) ||
pathExists(map, [position[0], position[1] - 1], newString) ||
pathExists(map, [position[0], position[1] + 1], newString);
}
it('should allow me to trace a path', function () {
testSentences.forEach(function (testSentence) {
var result = packSentence(testSentence);
expect(pathExists(result.map, result.position, testSentence.replace(/ /g, ''))).toBe(true);
});
});
});
describe('#getDimensions', function () {
it('should return the largest area rectangle that fits the string', function () {
var result = getDimensions(testSentences[0].replace(/ /g, ''));
expect(result.width).toBe(7);
expect(result.height).toBe(7);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment