aboutsummaryrefslogtreecommitdiff
path: root/challenge-127/iangoodnight/javascript/ch-1.js
blob: 8daf2fe6e7c7b01843e632d6089ec5b701b37275 (plain)
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
#!/usr/bin/env node

/**
 * Task #1 > Disjoint Sets
 * =======================
 *
 * You are given two sets with unique integers.
 * Write a script to figure out if they are disjoint.
 *
 * The two sets are disjoint if they don't have any common members
 *
 * EXAMPLE
 * Input: const s1 = [1, 2, 5, 3, 4];
 *        const s2 = [4, 6, 7, 8, 9];
 * Output: false as the given sets have common member 4.
 *
 * Input: const s1 = [1, 3, 5, 7, 9];
 *        const s2 = [0, 2, 4, 6, 8];
 * Output: true as the given two sets do no have a common member
 **/

'use strict';

/**
 * Node dependencies
 **/

const fs = require('fs');

const path = require('path');

/**
 * Here, the function to test our sets (PWC solution)
 **/

function isDisjoint(set1 = [], set2 = []) {
  const testSet = [...set1];           // shallow copy of the first set

  let disjoint = true;                 // trust, but verify
  while (disjoint && testSet.length) {
    const test = testSet.pop();
   if (set2.includes(test)) disjoint = false;
  }
  return disjoint;
}

/**
 * Followed by some utilities to test our solution
 **/

const isFile = (filePath) => fs.lstatSync(filePath).isFile();

const isDirectory = (filePath) => fs.lstatSync(filePath).isDirectory();

function parseTestCase(filePath = '') {
  try {
    const data = fs.readFileSync(filePath, 'utf8');

    const lines = data.split('\n');

    if (!lines.length) throw new Error('Test cases improperly formatted');

    const testData = lines.filter((line) => {
      return line.length !== 0 && line.charAt(0) !== '#';
    });

    const [ firstLine, secondLine, result ] = testData;

    const set1 = listToArray(firstLine);

    const set2 = listToArray(secondLine);

    const test = parseInt(result.trim()) === 1 ? true: false;

    return [set1, set2, test];
  } catch (err) {
    console.log('Problems parsing test files: ', err);
  }
}

function listToArray(str) {
  return str.split(',').map(el => el.trim());
}

function assertDisjoint([set1, set2, test], filePath = '') {
  const disjoint = isDisjoint(set1, set2);

  const testPath = filePath !== '' ? `${filePath}: `: '';

  if (disjoint === test) {
    return console.log(`${testPath}\x1b[32m%s\x1b[0m`, 'Passed \u2690');
  }
  return console.log(`${testPath}\x1b[31m%s\x1b[0m`, 'Failed \u2715');
}

/**
 * And, our test runner
 **/

(function main() {
  const testPath = process.argv[2] || '../test_cases/ch1';

  try {
    if (isFile(testPath)) {
      const test = parseTestCase(data);

      return assertDisjoint(test, testPath);
    }
    if (isDirectory(testPath)) {
      fs.readdirSync(testPath).map(fileName => {
        const filePath = path.join(testPath, fileName);

        const test = parseTestCase(filePath);

        assertDisjoint(test, filePath);
      });
      return;
    }
    return 'No tests found';
  } catch (error) {
    console.log('Something went wrong: ', error);
  }
})();