Skip to content

Commit 13d72e0

Browse files
authored
Merge pull request #5 from Rubinder25/reverse_words
Reverse words
2 parents b279ba5 + 956e5c7 commit 13d72e0

File tree

4 files changed

+112
-40
lines changed

4 files changed

+112
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.vscode
2+
.idea
23
/env
34
__pycache__
45
/.pytest_cache

medium/reverse_words/reverse_words.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const reverseWord = word => {
2+
return word
3+
.split('')
4+
.reverse()
5+
.join('');
6+
};
7+
8+
/**
9+
* @param {string} s
10+
* @returns {string}
11+
*/
12+
function reverse_words_js_functions(s) {
13+
let reverse_words = s
14+
.trim()
15+
.split(' ')
16+
.reverse(' ')
17+
.join(' ');
18+
19+
return reverse_words;
20+
}
21+
22+
/**
23+
* @param {string} s
24+
* @returns {string}
25+
*/
26+
function reverse_words_single_loop(s) {
27+
s = s.trim();
28+
const reverse_words_arr = [];
29+
let startIndex = 0;
30+
31+
for (let i = 0; i < s.length; i++) {
32+
if (s[i] === ' ') {
33+
reverse_words_arr.unshift(s.slice(startIndex, i));
34+
startIndex = i + 1;
35+
}
36+
}
37+
38+
reverse_words_arr.unshift(s.slice(startIndex, s.length + 1));
39+
return reverse_words_arr.join(' ');
40+
}
41+
42+
module.exports = [reverse_words_single_loop, reverse_words_js_functions];

medium/reverse_words/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"desc": 2,
1111
"args": ["Hi"],
12-
"exp": "iH"
12+
"exp": "Hi"
1313
},
1414
{
1515
"desc": 3,

tests/run_tests.js

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const testSpecified = process.argv[2];
66
let totalFilesTested = 0;
77
let totalTestsPassed = 0;
88
let totalTestsFailed = 0;
9+
const tests = [];
910

1011
function escapeRegExp(s) {
1112
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
@@ -14,7 +15,7 @@ function escapeRegExp(s) {
1415
function arrContains(list, val) {
1516
let isContains = false;
1617

17-
list.forEach((ele) => {
18+
list.forEach(ele => {
1819
let eleRegExp;
1920

2021
if (typeof ele === 'string') {
@@ -37,15 +38,18 @@ function isDir(dirPath) {
3738
}
3839

3940
function fileScannerSync(args) {
40-
if (fs.existsSync(args.path) && !arrContains(args.excludeList, path.basename(args.path))) {
41+
if (
42+
fs.existsSync(args.path) &&
43+
!arrContains(args.excludeList, path.basename(args.path))
44+
) {
4145
if (!isDir(args.path)) {
4246
args.callback(path.normalize(args.path), false);
4347
return;
4448
}
4549

4650
const dirList = fs.readdirSync(args.path);
4751

48-
dirList.forEach((item) => {
52+
dirList.forEach(item => {
4953
if (!arrContains(args.excludeList, path.basename(item))) {
5054
const relativePath = path.join(args.path, item);
5155

@@ -66,14 +70,14 @@ function fileScannerSync(args) {
6670
}
6771

6872
function isEqual(a, b) {
69-
if (typeof a !== 'object' && typeof b !== ' object') {
73+
if (typeof a !== 'object' && typeof b !== 'object') {
7074
return a === b;
7175
}
7276

7377
var aProps = Object.getOwnPropertyNames(a);
7478
var bProps = Object.getOwnPropertyNames(b);
7579

76-
if (aProps.length != bProps.length) {
80+
if (aProps.length !== bProps.length) {
7781
return false;
7882
}
7983

@@ -113,12 +117,12 @@ function runTest(test, func) {
113117

114118
function displayTestSuite(tSuite) {
115119
process.stdout.write(`\nTest: ${tSuite.fileName} => ${tSuite.funcName}\n\n`);
116-
tSuite.tests.forEach((test) => {
120+
tSuite.tests.forEach(test => {
117121
if (test.hasPassed) {
118122
process.stdout.write(
119-
`Test: ${test.desc} \u2713 | ${test.args.join(', ')} => ${test.exp} in ${
120-
test.timeTaken
121-
} ms\n`,
123+
`Test: ${test.desc} \u2713 | ${test.args.join(', ')} => ${
124+
test.exp
125+
} in ${test.timeTaken} ms\n`,
122126
);
123127
} else {
124128
process.stdout.write(
@@ -135,45 +139,69 @@ function displayTestSuite(tSuite) {
135139
);
136140
}
137141

138-
function displayFinalStats(totalFilesTested, totalTestsPassed, totalTestsFailed) {
142+
function displayFinalStats(
143+
totalFilesTested,
144+
totalTestsPassed,
145+
totalTestsFailed,
146+
) {
139147
process.stdout.write(`\nTotal files tested: ${totalFilesTested}\n\n`);
140-
process.stdout.write(`Total tests ran: ${totalTestsPassed + totalTestsFailed}\n`);
148+
process.stdout.write(
149+
`Total tests ran: ${totalTestsPassed + totalTestsFailed}\n`,
150+
);
141151
process.stdout.write(`Passed: ${totalTestsPassed}\n`);
142152
process.stdout.write(`Failed: ${totalTestsFailed}\n`);
143153
}
144154

155+
function getImports(pathFile) {
156+
const importTobeTested = require(pathFile);
157+
let funcsToBeTested = [];
158+
159+
if (typeof importTobeTested === 'function') {
160+
funcsToBeTested.push(importTobeTested);
161+
} else if (Array.isArray(importTobeTested)) {
162+
funcsToBeTested = Object.assign([], importTobeTested);
163+
} else {
164+
throw `${tSuite.fileName} doesn't export a function or array or functions`;
165+
}
166+
167+
return funcsToBeTested;
168+
}
169+
145170
fileScannerSync({
146171
path: '.',
147172
recursive: true,
148173
excludeList: ['tests', '.git'],
149174
callback: (relativePath, isDir) => {
150-
if (!isDir) {
151-
const tests = [];
152-
const tSuite = {
153-
fileName: '',
154-
funcName: '',
155-
tests: [],
156-
};
157-
158-
if (
159-
path.extname(relativePath) === '.js' &&
160-
(testSpecified === undefined || relativePath.indexOf(testSpecified) !== -1)
161-
) {
162-
const testPath = path.join(path.dirname(relativePath), 'test.json');
163-
164-
if (fs.existsSync(testPath)) {
165-
totalFilesTested++;
166-
const funcToBeTested = require(path.join('..' + path.sep, relativePath));
167-
168-
if (!tests[testPath]) {
169-
tests[testPath] = JSON.parse(fs.readFileSync(testPath, 'utf-8'));
170-
}
171-
172-
tSuite.fileName = path.basename(relativePath);
173-
tSuite.funcName = funcToBeTested.name;
174-
175-
tests[testPath].tests.forEach((test) => {
176-
const testRes = runTest(test, funcToBeTested);
175+
if (
176+
!isDir &&
177+
path.extname(relativePath) === '.js' &&
178+
(testSpecified === undefined ||
179+
relativePath.indexOf(testSpecified) !== -1)
180+
) {
181+
const testPath = path.join(path.dirname(relativePath), 'test.json');
182+
183+
if (fs.existsSync(testPath)) {
184+
totalFilesTested++;
185+
186+
const tSuite = {
187+
fileName: '',
188+
funcName: '',
189+
tests: [],
190+
};
191+
192+
tSuite.fileName = path.basename(relativePath);
193+
const pathImport = path.join('..' + path.sep, relativePath);
194+
const funcsToBeTested = getImports(pathImport);
195+
196+
if (!tests[testPath]) {
197+
tests[testPath] = JSON.parse(fs.readFileSync(testPath, 'utf-8'));
198+
}
199+
200+
funcsToBeTested.forEach((func, index) => {
201+
tSuite.funcName = func.name;
202+
203+
tests[testPath].tests.forEach(test => {
204+
const testRes = runTest(test, func);
177205
tSuite.tests.push(Object.assign(test, testRes));
178206

179207
if (testRes.hasPassed) {
@@ -183,7 +211,8 @@ fileScannerSync({
183211
}
184212
});
185213
displayTestSuite(tSuite);
186-
}
214+
tSuite.tests = [];
215+
});
187216
}
188217
}
189218
},

0 commit comments

Comments
 (0)