И раз такая пъянка пошла, как думаете что не так с решением этого задания ?
Цитата:
|
3) Read a text file containing a set of flat polygons represented as one polygon per line. Each line contains a comma-separated list of side lengths (for example “3,4,8,5,7”). Write a function to classify the set of polygons into four mutually exclusive subsets: triangles, rectangles, squares, and everything else. The union of all four subsets should be the original set of polygons. All the sides of the polygons are connected and the angles between them are irrelevant. Only consider the lengths.
|
const readline = require('readline');
const fs = require("fs");
const Stream = require('stream');
function isTriangle(sides) {
if (sides.length !== 3) {
return false;
}
const a = parseInt(sides[0]), b = parseInt(sides[1]), c = parseInt(sides[2]);
return a + b > c && a + c > b && b + c > a;
}
function isSquare(sides) {
const firstSide = sides[0];
return sides.length === 4 && sides.every(side => side === firstSide);
}
function isRectangle(sides) {
return sides.length === 4 &&
sides[0] === sides[2] && sides[1] === sides[3]
}
function getLineStream(filePath) {
const readFileStream = fs.createReadStream(filePath);
const outputStream = new Stream();
return readline.createInterface(readFileStream, outputStream);
}
function getPolygons(lineStream) {
return new Promise((resolve, reject) => {
const all = [];
const triangles = [];
const squares = [];
const rectangles = [];
const others = [];
lineStream.on("line", set => {
set = set.split(",");
if (isTriangle(set)) {
triangles.push(set)
}
else if (isRectangle(set)) {
rectangles.push(set);
} else if (isSquare(set)) {
squares.push(set);
} else {
others.push(set);
}
all.push(set);
});
lineStream.on("error", reject);
lineStream.on("close", () => {
resolve({all, triangles, squares, rectangles, others})
})
})
}
(async function () {
const {all, triangles, rectangles, others, squares} = await getPolygons(getLineStream("./file"));
console.log(`Triangles ${triangles.join("")}`);
console.log(`Squares ${squares}`);
console.log(`Rectangles ${rectangles}`);
console.log(`Others ${others}`);
console.log(`All ${all}`);
}());