1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#!/usr/bin/env node
// ch-1.js
/*******************************************************************************
* https://theweeklychallenge.org/blog/perl-weekly-challenge-145/
*
* ## Task 1 > Dot Product
* =======================
*
* You are given 2 arrays of the same size, `@a` and `@b`.
*
* Write a script to implement `Dot Product`.
*
* **Example:**
* ```
* @a = (1, 2, 3);
* @b = (4, 5, 6);
*
* $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32
* ```
*
******************************************************************************/
'use strict';
/*******************************************************************************
* Dependencies ****************************************************************
******************************************************************************/
const fs = require('fs');
const path = require('path');
/*******************************************************************************
* PWC Solution ****************************************************************
******************************************************************************/
// Input guard
function areSameLengthArrays(arr1 = [], arr2 = []) {
const { isArray } = Array;
return isArray(arr1) && isArray(arr2) && arr1.length === arr2.length;
}
// Solution
function dotProduct(arr1 = [], arr2 = []) {
const badInput = 'dotProduct expects two equal-length arrays as arguments';
if (!areSameLengthArrays(arr1, arr2)) throw new Error(badInput);
return arr1.reduce((sum, multiplicand, idx) => {
const multiplier = arr2[idx];
return sum + multiplicand * multiplier;
}, 0);
}
/*******************************************************************************
* Utilities *******************************************************************
******************************************************************************/
// Checking file types
function isFile(filePath) {
return fs.lstatSync(filePath).isFile();
}
function isDirectory(filePath) {
return fs.lstatSync(filePath).isDirectory();
}
// String transformation
function numArrayFromString(str = '') {
return str.split(',').map((numStr) => parseInt(numStr.trim(), 10));
}
// File handlers
function parseTestCase(filePath = '') {
if (isFile(filePath)) {
try {
const data = fs.readFileSync(filePath, 'utf8');
const lines = data.split('\n');
const badCase = 'Test cases are improperly formatted';
if (!lines.length) throw new Error(badCase);
const testData = lines.filter(
(line) => line.trim().length !== 0 && line.trim().charAt(0) !== '#',
);
const [firstLine, secondLine, testLine] = testData;
const arr1 = numArrayFromString(firstLine);
const arr2 = numArrayFromString(secondLine);
if (!areSameLengthArrays(arr1, arr2)) throw new Error(badCase);
const test = parseInt(testLine.trim(), 10);
if (Number.isNaN(test)) throw new Error(badCase);
return [arr1, arr2, test];
} catch (error) {
return console.log('Problems parsing test cases: ', error);
}
}
console.log(filePath, 'is not a file');
return null;
}
function printArray(arr = []) {
return Array.isArray(arr) && arr.join(', ');
}
function assertDotProduct([arr1 = [], arr2 = [], test], filePath = '') {
try {
const result = dotProduct(arr1, arr2);
const testPath = filePath !== '' ? `${filePath}: ` : '';
console.log(testPath);
console.log(`@a = ${printArray(arr1)}`);
console.log(`@b = ${printArray(arr2)}`);
console.log(`Dot Product: ${result}`);
if (result === test) {
return console.log('\x1b[32m%s\x1b[0m', 'Passed \u2690\n');
}
return console.log('\x1b[31m%s\x1b[0m', 'Failed \u2715\n');
} catch (error) {
return console.log('Something went wrong: ', error);
}
}
/*******************************************************************************
* Test runner *****************************************************************
******************************************************************************/
(function main() {
const testPath = process.argv[2] || '../test_cases/ch-1';
try {
if (isFile(testPath)) {
const testElements = parseTestCase(testPath);
return assertDotProduct(testElements, testPath);
}
if (isDirectory(testPath)) {
return fs.readdirSync(testPath).forEach((fileName) => {
const filePath = path.join(testPath, fileName);
const testElements = parseTestCase(filePath);
return testElements && assertDotProduct(testElements, filePath);
});
}
return console.log('No tests found');
} catch (error) {
return console.log('Something went wrong: ', error);
}
})();
|