Skip to content

Instantly share code, notes, and snippets.

@dlaxar
Created October 3, 2012 09:02
Show Gist options
  • Save dlaxar/3825913 to your computer and use it in GitHub Desktop.
Save dlaxar/3825913 to your computer and use it in GitHub Desktop.
MPCA #1
var count = {};
var array = ["str1", "str2", "string 3", "string 4", "s5", "string 4", "s5", "str2", "string 3", "string 4", "s5", "string 4", "str1"];
for(var i = 0; i < array.length; i++) {
if(count[array[i]]) {
count[array[i]]++;
}
else {
count[array[i]] = 1;
}
}
console.log(count);
count = {}
a = "str1", "str2", "string 3", "string 4", "s5", "string 4", "s5", "str2", "string 3", "string 4", "s5", "string 4", "str1"
for x in a:
if x in count:
count[x] = count[x] + 1
else:
count[x] = 1
print count
/*
How to use:
node l6.js
Then use the command line to put the input
Link:
http://htl.reneheinzl.net/htl_contest_prog.html
*/
//status variables
var dataSets = -1;
var currentSet = -1;
var meta;
var readMeta = false;
var conversionData;
var readConversion = false;
var setData;
var dataLinesRead = -1;
//smallest el in array
function min(a) {
var min = a[0];
for (var i = 0; i < a.length; i++) {
if(a[i] < min) {
min = a[i];
}
}
return min;
}
//biggest el in array
function max(a) {
var max = a[0];
for (var i = 0; i < a.length; i++) {
if(a[i] > max) {
max = a[i];
}
}
return max;
}
//init session
process.stdin.resume();
process.stdin.setEncoding("utf8")
process.stdin.on('data', function(chunk) {
var str = chunk.toString();
var splitted = str.trim().split(" ");
if(dataSets < 0) {
dataSets = parseInt(str);
currentSet = 0;
conversionData = [];
setData = [];
meta = [];
dataLinesRead = 0;
}
else if(!readMeta) {
meta[currentSet] = str.split(" ");
setData[currentSet] = [];
readMeta = true;
}
else if(!readConversion) {
if(splitted.length != meta[currentSet][0] - 1) {
console.log("error! please specify exactly " + (meta[currentSet][0] - 1) + " denominations");
}
else {
conversionData[currentSet] = calcCurrencyRate(splitted);
readConversion = true;
}
}
else {
if(splitted.length != meta[currentSet][0]) {
console.log("error! please specify exactly " + (meta[currentSet][1]) + " prices");
}
else {
setData[currentSet][dataLinesRead] = splitted;
if(++dataLinesRead == meta[currentSet][1]) {
currentSet++;
calculateResult();
//restart session
readMeta = false;
readConversion = false;
dataLinesRead = 0;
}
}
}
});
// calculate the results
function calculateResult() {
if(currentSet >= dataSets) {
var prices = [];
for(var i = 0; i < setData.length; i++) { // each dataset
prices[i] = [];
for(var x = 0; x < setData[i].length; x++) { // each line
prices[i][x] = 0;
line = setData[i][x];
for(var n = 0; n < line.length; n++) { // each column
prices[i][x] += parseInt(line[n]) * parseInt(conversionData[i][n]);
}
}
console.log("Data Set " + (i + 1));
console.log(max(prices[i]) - min(prices[i]));
}
dataSets = -1;
}
}
// input: 2 3
// output: 6 (2*3) 3 (3) 1 (3/3)
// input 2
// output 2 1
function calcCurrencyRate(a) {
var out = [];
for(var i = 0; i <= a.length; i++) {
out[i] = 1;
for (var n = i; n < a.length; n++) {
out[i] *= a[n];
}
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment