-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
102 lines (94 loc) · 2.85 KB
/
test.js
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
function OR(course_set, operands) {
for (let i = 0; i < operands.length; i++) {
if (operands[i].every((v) => course_set.includes(v.source_id))) {
return operands[i];
}
}
return [null];
}
function construct_req(course_set, req, target_id) {
const { type } = req;
// base case
if (!["or", "and"].includes(type)) {
if (type === "course") {
const {
course: { id },
concurrency_ind,
} = req;
return [
{
source_id: id,
target_id: target_id,
type: concurrency_ind ? "coreq" : "prereq",
},
];
}
return []; // ignore custom and placement exam types
}
const { operands } = req;
let courses = [];
for (let i = 0; i < operands.length; i++) {
if (type === "and") {
courses = courses.concat(
construct_req(course_set, operands[i], target_id)
);
} else {
courses.push(construct_req(course_set, operands[i], target_id));
}
}
if (type === "or") {
return OR(course_set, courses);
}
return courses;
}
const req = {
type: "or",
operands: [
{
type: "and",
operands: [
{ type: "course", course: { id: 1 }, concurrency_ind: false },
{ type: "course", course: { id: 2 }, concurrency_ind: true },
{
type: "or",
operands: [
{
type: "course",
course: { id: 3 },
concurrency_ind: false,
},
{
type: "course",
course: { id: 4 },
concurrency_ind: false,
},
],
},
],
},
{
type: "and",
operands: [
{ type: "course", course: { id: 5 }, concurrency_ind: false },
{ type: "course", course: { id: 6 }, concurrency_ind: false },
{ type: "course", course: { id: 7 }, concurrency_ind: false },
],
},
],
};
const req1 = {
type: "and",
operands: [
{ type: "course", course: { id: 1 }, concurrency_ind: false },
{
type: "or",
operands: [
{ type: "course", course: { id: 2 }, concurrency_ind: false },
{ type: "course", course: { id: 3 }, concurrency_ind: false },
{ type: "course", course: { id: 4 }, concurrency_ind: false },
],
},
],
};
const course_set = [1];
console.log(construct_req(course_set, req1, 999));