Kattis problem - ("I've been everywhere")
I would highly recommend looking at the problem through the link, however I will summarize it a bit here and explain the functionality of each part of my horrendous code.
Example input:
2
7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton
(line 1: number of test cases 2
, lines 2 and 10: number of working trips 7
3
)
Example output:
4
1
The goal is to ouput how many unique locations has been traveled to per test case. (correction, it does not matter if a location was visited already in a previous test case)
So, basically I want some help condensing my code as much as possible, and make it simpler also because I believe my code is a little bit much for such a basic coding question. Also, I want to extend my knowledge beyond spamming for loops XD
My Code
var readline = require('readline');
var input = [];
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (cmd) {
input.push(cmd);
});
rl.on('close', function () {
input.shift();
let counter = 50;
for (let i = 0; i < input.length; i++) {
value = parseInt(input[i]);
if (value / value == 1) {
input[i] = counter;
counter++;
}
}
let index = [];
let unique = [...new Set(input)];
//console.log(unique)
for (let i = 0; i < unique.length; i++) {
value = parseInt(unique[i]);
if (value / value == 1) {
unique[i] = unique.indexOf(unique[i]);
index.push(unique.indexOf(unique[i]));
}
}
index.push(unique.length);
unique.push(unique.length);
//console.log(unique);
//console.log(index);
let cityList = [];
for (let i = 0; i < index.length; i++) {
let start = index[i];
let end = (index[i+1]);
//console.log(start);
//console.log(end);
if (end == undefined) {
break;
}
array = [];
for (let j = start + 1; j < end; j++) {
if (unique[j] == undefined) {
array.push("");
} else {
array.push(unique[j]);
}
}
cityList.push(array);
}
//console.log(cityList);
for (let i = 0; i < cityList.length; i++) {
console.log(cityList[i].length);
}
process.exit(0);
});
//i know it's bad
The code below removes the first element from the input array, changes the integers into distinct numbers to avoid removing them during the filtering later one:
input.shift();
let counter = 50;
for (let i = 0; i < input.length; i++) {
value = parseInt(input[i]);
if (value / value == 1) {
input[i] = counter;
counter++;
}
}
Next, create an array of the indexes of each integer in the input array so we can used them as lower and upper bound limits. Filter out an duplicates. Change the input arrays integer values to their positional value or index for future use as well. Push the length of the input array into the index array and input array so that we have an index for the last upper bound.
let index = [];
let unique = [...new Set(input)];
//console.log(unique)
for (let i = 0; i < unique.length; i++) {
value = parseInt(unique[i]);
if (value / value == 1) {
unique[i] = unique.indexOf(unique[i]);
index.push(unique.indexOf(unique[i]));
}
}
index.push(unique.length);
unique.push(unique.length);
//console.log(unique);
//console.log(index);
Establish an array that will include the unique locations per test case. Loop through the index array, using the value of index[i]
for (start) and index[i+1]
for (end). Create a nested array where it loops through the new input array known as the unique array, where we push every element between the start and end.
let cityList = [];
for (let i = 0; i < index.length; i++) {
let start = index[i];
let end = (index[i+1]);
//console.log(start);
//console.log(end);
if (end == undefined) {
break;
}
array = [];
for (let j = start + 1; j < end; j++) {
if (unique[j] == undefined) {
array.push("");
} else {
array.push(unique[j]);
}
}
cityList.push(array);
}
Finally, output the length of each nested array within cityList
for (let i = 0; i < cityList.length; i++) {
console.log(cityList[i].length);
}
: )