DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
clandau profile image
Courtney

brute force for me so far.

const readline = require('readline');

function foregone() {
    let argumentsArray = [], answerArray = [];
    let linecounter = 0, tests;

    const rl = readline.createInterface({ 
        input: process.stdin,
        output: process.stdout
    });
    rl.on('line', (line) => {
        linecounter++;
        if(linecounter === 1) tests = line;
        else {
            argumentsArray.push(line);
            if(linecounter > tests) rl.close();
        }
    }).on('close', () => {
        for(let i=0; i< argumentsArray.length; i++) {
            answerArray.push(noFours(argumentsArray[i]));
        }
        for(let i=0; i<answerArray.length; i++) {
            console.log(`Case #${i+1}: ${answerArray[i][0]} ${answerArray[i][1]}`)
        }
        process.exit(0);
    });

    function noFours(num) {
        let a = num - 1, b = num - a;
        while(a.toString().indexOf('4') !== -1 || b.toString().indexOf('4') !== -1 && a >= b) {
            a--, b++;
        }
        if(a.toString().indexOf('4') === -1 && b.toString().indexOf('4') === -1) return [a, b];
        else return undefined 
    }
}