DEV Community

Discussion on: Coding Puzzles: Week of 4/8

Collapse
 
aspittel profile image
Ali Spittel

Friday (CodeJam): Foregone Solution

codingcompetitions.withgoogle.com/...

Collapse
 
choroba profile image
E. Choroba
#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use Math::BigInt;

chomp( my $cases = <> );
for my $case (1 .. $cases) {
    chomp( my $n = <> );
    (my $i = $n) =~ tr/4/3/;
    my $j = 'Math::BigInt'->new($n) - 'Math::BigInt'->new($i);
    say "Case #$case: $i $j";
}
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 
    }
}
Collapse
 
aspittel profile image
Ali Spittel
n_cases = int(input())
for i in range(1, n_cases + 1):
    check_amount = int(input())
    unfound = True
    fours = [int(i) for i in str(check_amount)]
    others = ["1" if n == 4 else "0" for n in fours]
    n = int(''.join(others))
    print("Case #{}: {} {}".format(i, n, check_amount - n))