Skip to content

Instantly share code, notes, and snippets.

@tylerpeterson
Created June 8, 2014 00:01
Show Gist options
  • Save tylerpeterson/cab968546f4d42181632 to your computer and use it in GitHub Desktop.
Save tylerpeterson/cab968546f4d42181632 to your computer and use it in GitHub Desktop.
My unpolished solution to a couple of the questions from the Bloomberg coding challenge at JSConf 2014. Seems safe to share now.
//Problem : Giga Ball
//Language : Javascript
//Compiled Using : V8
//Version : Node 0.10.25
//Input for your program will be provided from STDIN
//Print out all output from your program to STDOUT
/**
* My unpolished solution to the Giga Ball question from the Bloomberg coding challenge at JSConf 2014.
* ~Tyler
*/
var readline = require('readline');
var firstLine = true;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var state = "init";
var lCount, mCount, nCount;
var l = [];
var m = [];
var n = [];
var firstLine = true;
rl.on('line', function (cmd)
{
if (state === "init") {
var numbers = cmd.split(' ');
lCount= parseInt(numbers[0]);
mCount = parseInt(numbers[1]);
nCount = parseInt(numbers[2]);
state = "inL";
console.log('in init %d %d %d', lCount, mCount, nCount);
} else if (state === "inL") {
console.log('in L');
if (lCount === l.length) {
console.log('switch to M');
state = "inM";
} else {
console.log('parsed l %s', cmd);
l.push(parseInt(cmd));
}
} else if (state === "inM") {
console.log('in init');
if (mCount === m.length) {
console.log('in switch to N');
state = "inN";
} else {
console.log('in parseN', cmd);
m.push(parseInt(cmd));
}
} else if (state === "inN") {
console.log('in inN');
n.push(parseInt(cmd));
if (nCount === n.length) {
console.log('in computeAnswer');
// Compute answer
var sum;
var hits = {};
var i, j, k;
var best = -1;
var bestCount = -1;
for (i = 0; i < lCount; ++i) {
for (j = 0; j < mCount; ++j) {
for (k = 0; k < nCount; ++k) {
sum = l[i]+m[j]+n[k];
hits[sum] = hits.hasOwnProperty(sum) ? hits[sum] + 1 : 1;
}
}
}
console.log('hits', hits);
var keys = Object.keys(hits);
var x, xCount;
while (keys.length > 0) {
x = keys.shift();
xCount = hits[x];
if (xCount > bestCount) {
console.log('%d with count %d is better than %d with count %d', x, xCount, best, bestCount);
best = x;
bestCount = xCount;
}
}
console.log(best);
} else {
console.log('in parseN', cmd);
}
} else {
console.log('errme');
}
});
//Problem : The Verbal Literal
//Language : Javascript
//Compiled Using : V8
//Version : Node 0.10.25
//Input for your program will be provided from STDIN
//Print out all output from your program to STDOUT
/**
* My unpolished solution to the Verbal question from the Bloomberg coding challenge at JSConf 2014.
* ~Tyler
*/
var readline = require('readline');
var firstLine = true;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
function iterate(input) {
var toks = input.split('');
var cur = toks[0];
var curCount = 0;
var res = [];
var i;
var len = toks.length;
for (i = 0; i < len; ++i) {
var val = toks[i];
if (val === cur) {
curCount = curCount + 1;
} else {
res.push(curCount);
res.push(cur);
cur = val;
curCount = 1;
}
}
res.push(curCount);
res.push(cur);
return res.join('');
};
rl.on('line', function (cmd)
{
var cmdParts = cmd.split(' ');
var seed = cmdParts[0];
var iterations = parseInt(cmdParts[1]);
var iter = 0;
var cur;
var curCount;
var res;
var i;
var len;
var toks = seed.split('');
var val;
len = toks.length
for (i = 0; i < len; ++i) {
toks[i] = parseInt(toks[i]);
}
// console.log('got see %s and iterations %d', seed, iterations);
while (iter < iterations) {
res = [];
iter += 1;
cur = toks[0];
curCount = 0;
len = toks.length;
// console.log('starting ieteration %d, res %s, cur %s, curCount %d, len %d', iter, JSON.stringify(res), cur, curCount, len);
for (i = 0; i < len; ++i) {
val = toks[i];
// console.log('i %d, val %s, res %s', i, val, JSON.stringify(res));
if (val === cur) {
curCount = curCount + 1;
} else {
res.push(curCount);
res.push(cur);
cur = val;
curCount = 1;
}
}
res.push(curCount);
res.push(cur);
// console.log(res.join(''));
toks = res.slice();
}
console.log(res.length);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment