Created
May 8, 2016 10:56
-
-
Save jeznag/b5f885f211b210249f91b0723e1ef095 to your computer and use it in GitHub Desktop.
generate permutations of string
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
| process.stdin.resume(); | |
| process.stdin.setEncoding('ascii'); | |
| var input_stdin = ""; | |
| var input_stdin_array = ""; | |
| var input_currentline = 0; | |
| process.stdin.on('data', function (data) { | |
| input_stdin += data; | |
| }); | |
| process.stdin.on('end', function () { | |
| input_stdin_array = input_stdin.split("\n"); | |
| main(); | |
| }); | |
| function readLine() { | |
| return input_stdin_array[input_currentline++]; | |
| } | |
| /////////////// ignore above this line //////////////////// | |
| function main() { | |
| var t = parseInt(readLine()); | |
| for(var a0 = 0; a0 < t; a0++){ | |
| var n = parseInt(readLine()); | |
| console.log(findDecentNumber(n)); | |
| } | |
| } | |
| function findDecentNumber(numDigits) { | |
| if (numDigits === 1) { | |
| return -1; | |
| } | |
| var incremend = 3; | |
| var highestDecentNumber = -1; | |
| var possibleDecentNumbers = generatePossibleDecentNumbers(numDigits); | |
| return possibleDecentNumbers.reduce(function (highest, current) { | |
| return highest > current ? highest : current; | |
| }); | |
| } | |
| function generatePossibleDecentNumbers(numDigits) { | |
| 'use strict'; | |
| let possibilities = ['']; | |
| for (let i = 0; i < numDigits; i++) { | |
| let newPossibilities = []; | |
| for (let j = 0; j < possibilities.length; j++) { | |
| newPossibilities = newPossibilities.concat(addNextDigit(possibilities[j])); | |
| } | |
| possibilities = newPossibilities; | |
| } | |
| return possibilities.filter(isDecentNumber); | |
| } | |
| function addNextDigit(curString) { | |
| return [parseInt(curString + '3'), parseInt(curString + '5')]; | |
| } | |
| function isDecentNumber(num) { | |
| if (('' + num).match(/[^35]/)) { | |
| return false; | |
| } | |
| var stringifiedNum = '' + num; | |
| var matchesWithThree = stringifiedNum.match(/3/g) || [3,3,3,3,3]; | |
| var matchesWithFive = stringifiedNum.match(/5/g) || [5,5,5]; | |
| return matchesWithFive.length % 3 === 0 && matchesWithThree.length % 5 === 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment