Created
April 19, 2015 19:04
-
-
Save toddbranch/7c1730c7989ce24867a8 to your computer and use it in GitHub Desktop.
r/dailyprogrammer #209 - http://www.reddit.com/r/dailyprogrammer/comments/31thwb/20150408_challenge_209_intermediate_packing_a/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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