Skip to content

Instantly share code, notes, and snippets.

@supernova-at
Created September 1, 2015 22:45
Show Gist options
  • Save supernova-at/fef5963d37022acf3446 to your computer and use it in GitHub Desktop.
Save supernova-at/fef5963d37022acf3446 to your computer and use it in GitHub Desktop.
function getNames(input) {
// Using the array index of map for the numbers
var map = [
// 0
[''],
// 1
[''],
// 2
['a', 'b', 'c'],
// 3
['d', 'e', 'f'],
// 4
['g', 'h', 'i'],
// 5
['j', 'k', 'l'],
// 6
['m', 'n', 'o'],
// 7
['p', 'q', 'r', 's'],
// 8
['t', 'u', 'v'],
// 9
['w', 'x', 'y', 'z']
];
// This is where we're going to store the individual outputs
var permutations = [];
// I'm assuming all valid inputs here, that's probably not a good thing to do
// Get the digits
var firstDigit = parseInt(input.charAt(0));
var secondDigit = parseInt(input.charAt(1));
var thirdDigit = parseInt(input.charAt(2));
var fourthDigit = parseInt(input.charAt(3));
// Get the arrays the digits map to
var firstArray = map[firstDigit];
var secondArray = map[secondDigit];
var thirdArray = map[thirdDigit];
var fourthArray = map[fourthDigit];
// Determine the total number of permutations based on the length of the arrays
var numPermutations = firstArray.length * secondArray.length * thirdArray.length * fourthArray.length;
// Loop through the numbers from 1 to total number
for (var i = 0; i < numPermutations; i++) {
// For each, figure out array index of each digit to target
// We do this very similarly to how you'd figure out hours / minutes / seconds given a total number of seconds
// For example, the first few are:
// 0 = 0,0,0,0
// 1 = 0,0,0,1
// 2 = 0,0,0,2
// 3 = 0,0,1,0
// 4 = 0,0,1,1
// 5 = 0,0,1,2
// 6 = 0,0,2,0
var firstIndex = Math.floor((i / (fourthArray.length * thirdArray.length * secondArray.length))) % firstArray.length;
var secondIndex = Math.floor((i / (fourthArray.length * thirdArray.length))) % secondArray.length;
var thirdIndex = Math.floor((i / fourthArray.length)) % thirdArray.length;
var fourthIndex = i % fourthArray.length;
console.log('indices', '' + firstIndex + '' + secondIndex + '' + thirdIndex + '' + fourthIndex);
// Combine them all into an individual permutation
var firstTarget = firstArray[firstIndex];
var secondTarget = secondArray[secondIndex];
var thirdTarget = thirdArray[thirdIndex];
var fourthTarget = fourthArray[fourthIndex];
// Add this permutation to the permutation list
var thisPermutation = firstTarget.concat(secondTarget).concat(thirdTarget).concat(fourthTarget);
console.log('this permutation', thisPermutation);
permutations.push(thisPermutation);
}
// Return the list as a comma delimited string
return permutations.join(', ');
}
var input = '1023'; // 7777 // 2345
var result = getNames(input);
console.log(result);
@pgreen1014
Copy link

I guess I am somewhat confused because when I translate it to Python I get floats rather than integers. I am guessing that what Math.floor does?

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