/
set-operations.spec.ts
69 lines (50 loc) · 2.18 KB
/
set-operations.spec.ts
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
import { expect } from 'chai';
describe('set operations', () => {
it('calculate size', () => {
const someSet = [1, 2, 3, 4, 5];
var cardinality = someSet.length;
// cardinality = 5;
expect(cardinality).to.eql(5);
});
it('create a subset', () => {
const animals = [
{ name: "Tom", type: "Cat" },
{ name: "Jerry", type: "Mouse" },
{ name: "Pluto", type: "Dog" },
{ name: "Scooby Doo", type: "Dog" }];
const dogs = animals.filter(a => a.type == "Dog");
// dogs = [{name: "Pluto", type: "Dog"}, {name: "Scooby Doo", type: "Dog"}]
expect(dogs).to.eql([
{ name: "Pluto", type: "Dog" },
{ name: "Scooby Doo", type: "Dog" }]);
});
it('summation', () => {
const someSet = [1, 2, 3];
const sum = someSet.reduce((acc, x) => acc + x * 2, 0);
// sum = 6
expect(sum).to.eql(12);
const users = [{ id: 1, email: "email@email.com" }, { id: 2, email: "email2@email2.com" }, { id: 3, email: "email3@email.com" }];
const emails = users.map(u => u.email).reduce((acc, x) => `${acc};${x}`);
// emails = "email@email.com;email2@email2.com;email3@email.com"
expect(emails).to.eql("email@email.com;email2@email2.com;email3@email.com");
});
it('create a powerset', () => {
const expected = [[], [0], [1], [1, 0], [2], [2, 0], [2, 1], [2, 1, 0]];
const someSet = [0, 1, 2];
const powerSet = someSet.reduce((acc, x) => [...acc, ...acc.map(y => [x, ...y])], [[]]);
// powerSet = [[], [0], [1], [1,0], [2], [2,0], [2,1], [2,1,0]]
expect(expected.length).to.eql(8);
expect(powerSet).to.eql(expected);
});
it('power set step by step', () => {
const expected = [[], [0], [1], [1, 0], [2], [2, 0], [2, 1], [2, 1, 0]];
const ps = (acc, x) => [...acc, ...acc.map(y => [x, ...y])];
let acc = ps([[]], 0);
// acc = [[], [0]]
acc = ps(acc, 1);
//[[], [0], [1], [1,0]]
acc = ps(acc, 2);
// acc = [[], [0], [1], [1, 0], [2], [2, 0], [2, 1], [2, 1, 0]]
expect(acc).to.eql(expected);
});
});