-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem2.js
119 lines (109 loc) · 3.11 KB
/
problem2.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function IsDigit(c) {
const code = c.charCodeAt(0);
return '0'.charCodeAt(0) <= code && code <= '9'.charCodeAt(0);
}
function CheckProblemLera(problem) {
const l = problem.length;
let n = 0;
let m = 0;
for (let i = 0; i < l; i++) {
if (problem[i] !== "+" && problem[i] !== "-" && problem[i] !== "(" &&
problem[i] !== ")" && problem[i] !== " " && !IsDigit(problem[i])) {
return false;
}
if (problem[i] == "(") {
n++;
}
if (problem[i] == ")") {
m++;
}
}
if (m !== n) {
return false;
}
let pos_open = -1;
let pos_closed = -1;
for (let i = 0; i < n; i++) {
for (pos_open++; problem[pos_open] !== '('; pos_open++) {
// Do nothing
}
for (pos_closed++; problem[pos_closed] !== ')'; pos_closed++) {
// Do nothing
}
if (pos_open > pos_closed) {
return false;
}
}
return true;
}
function CheckProblemVictor(problem) {
let num_open_brackets = 0;
let state = '(';
// state:
// * '(' --- we can expect anything we could expect after '('.
// * '1' --- we can expect anything we could expect after '1'.
// * ')' --- we can expect anything we could expect after ')'.
for (let pos = 0; pos < problem.length; pos++) {
let cur = problem[pos];
if (cur === '(') {
num_open_brackets++;
} else if (cur === ')') {
num_open_brackets--;
if (num_open_brackets < 0) {
return false;
}
}
if (state === '(') {
if (cur === ' ' || cur === '(') {
state = '(';
} else if (IsDigit(cur)) {
state = '1';
} else {
return false;
}
} else {
// state === ')' || state === '1'
if (IsDigit(cur) && state === '1') {
state = '1';
} else if (cur === ' ' || cur === ')') {
state = ')';
} else if (cur === '+' || cur === '-') {
state = '(';
} else {
return false;
}
}
}
return num_open_brackets === 0 && (state === '1' || state === ')');
}
function CheckProblem2(solution) {
const examples = [
{input: '1 + 3 - 4', answer: true},
{input: '238rhqwioehfpioqh', answer: false},
{input: '(1)', answer: true},
{input: '((((((((((2))))))))))', answer: true},
{input: '(((((((((2))))))))))', answer: false},
{input: '((((((((((2)))))))))', answer: false},
{input: '1 + 3) + (4 + 7', answer: false},
{input: '(1 + 3) + (4 + 7)', answer: true},
{input: '(1 + 3)) + ((4 + 7)', answer: false},
{input: '(1 + )', answer: false},
{input: ' + ', answer: false},
{input: '', answer: false},
{input: '1 + ()', answer: false},
{input: '49 + 3', answer: true}];
for (let i = 0; i < examples.length; i++) {
const example = examples[i];
const expected = example.answer;
const actual = solution(example.input);
if (expected !== actual) {
console.log('Solution is not correct on example #' + i + ": '" +
example.input + "'");
console.log('Expected: ' + expected + '; actual: ' + actual + '.');
return false;
}
}
console.log('Solution looks correct.');
return true;
}
CheckProblem2(CheckProblemVictor);