-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_checker.gs
229 lines (229 loc) · 7.54 KB
/
range_checker.gs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*! (c) 2014-2017 みらい研究室実行委員会
* Released under the MIT license.
*/
require("colors");
var jsdiff = require("diff");
/**
* @brief detect type.
* @param obj {any} target variable
* @return {string}
*/
function decltype(obj){
return (typeof(obj) === "undefined") ? "undefined" : Object.prototype.toString.call(obj).slice(8, -1);
}
/**
* @brief check type.
* @param type {string} typename(String,Number,Boolean,Date,Error,Array,Function,RegExp,Object, ...)
* @param obj {any} target variable
* @return {boolean}
*/
function is(type, obj) {
return obj !== undefined && obj !== null && decltype(obj) === type;
}
/**
* @brief convert 2d-array to 1d-array.
* @param {numbers[]|numbers[][]} range
* @returns {numbers[]}
*/
function range2array(range){
if(!Array.isArray(range)) throw new TypeError("unexpected input. range:" + decltype(range));
switch(decltype(range[0])){
case "Number":
return range;
case "String":
return range.map(function(e){ return parseInt(e, 10); });
case "Array":
switch(decltype(range[0][0])){
case "Number":
return function(){
var arr = [];
var i;
var l = range.length;
for(i = 0; i < l; ++i) arr.push.apply(arr, range[i]);
return arr;
}();
case "String":
return function(){
var arr = [];
var i;
var l = range.length;
for(i = 0; i < l; ++i) arr.push.apply(arr, range[i].map(function(e){ return parseInt(e, 10); }));
return arr;
}();
default:
throw new TypeError(
"unexpected input. range:" + decltype(range)
+ " range[0]:" + decltype(range[0])
+ " range[0][0]:" + decltype(range[0][0])
);
}
default:
throw new TypeError(
"unexpected input. range:" + decltype(range)
+ " range[0]:" + decltype(range[0])
);
}
}
/**
* @brief check range and list-up missing numbers.
* @param {numbers[]|numbers[][]|string[]|string[][]} numbers investigate target.
* @param {numbers} min
* @param {numbers} max
* @returns {numbers[]}
*/
function list_missing_num(numbers, min, max) {
if(
Array.isArray(numbers) && is("Number", min) && is("Number", max) && (
(Array.isArray(numbers[0]) && (is("Number", numbers[0][0]) || is("String", numbers[0][0])))
|| is("Number", numbers[0]) || is("String", numbers[0])
)
){
var arr = range2array(numbers);
var i;
var j;
arr.sort(function(a, b){ return a- b; });
//重複を弾く
//http://qiita.com/cocottejs/items/7afe6d5f27ee7c36c61f
arr = arr.filter(function (x, i, self) {
return self.indexOf(x) === i;
});
var re = [];
var push_back = function(min, max){
for(var i = min; i <= max; ++i) re.push(i);
};
if(arr[arr.length - 1] < min || max < arr[0]) {
return Array(max - min + 1).fill(0).map(function(_,i){ return i + min; });
}
for(i = 0; arr[i] < min; ++i);//skip
for(j = min; i < arr.length && arr[i] <= max; j = arr[i] + 1, ++i) {
if(0 !== arr[i] - j){
push_back(j, arr[i] - 1);
}
}
push_back(arr[i - 1] + 1, max);
return (re.length === 0) ? [] : re;
}
else{
throw new TypeError("unexpected input. numbers:" + decltype(numbers) + " min:" + decltype(min) + " max:" + decltype(max));
}
}
/**
* @brief check range and list-up missing numbers.
* @param {numbers[]|numbers[][]|string[]|string[][]} numbers investigate target.
* @param {numbers} min
* @param {numbers} max
* @returns {string} missing numbers string(separated ,).
*/
function list_missing_num_as_string(numbers, min, max) {
var re = list_missing_num(numbers, min, max);
return (0 !== re.length) ? re.join() : "nothing";
}
/**
* @brief check range and list-up duplicated numbers.
* @param {numbers[]|numbers[][]} numbers investigate target.
* @returns {numbers[]}
*/
function list_duplicate_num(numbers) {
if(
Array.isArray(numbers) && (
(Array.isArray(numbers[0]) && (is("Number", numbers[0][0]) || is("String", numbers[0][0])))
|| is("Number", numbers[0]) || is("String", numbers[0])
)
){
var re = range2array(numbers);
re.sort(function(a, b){ return a- b; });
//重複のみをリスト
//http://qiita.com/cocottejs/items/7afe6d5f27ee7c36c61f
return re.filter(function (x, i, self) {
return self.indexOf(x) === i && i !== self.lastIndexOf(x);
});
}
else{
throw new TypeError("unexpected input. numbers:" + decltype(numbers));
}
}
/**
* @brief check range and list-up duplicated numbers.
* @param {numbers[]|numbers[][]} numbers investigate target.
* @returns {string} duplicated numbers string(separated ,).
*/
function list_duplicate_num_as_string(numbers) {
var re = list_duplicate_num(numbers);
return (re.length === 0) ? "nothing" : re.join();
}
/**
*
* @callback JudgeCallback
* @param {string} param
* @return {boolean}
*/
/**
* @brief check 2*n range and list-up when `cond_f` return true.
* @param {object[][]} params An array of pair of number and string.
* @param {JudgeCallback} cond_f A function whether list-up or not.
* @return {numbers[]}
*/
function list_if(params, cond_f) {
if(Array.isArray(params) && Array.isArray(params[0]) && 2 === params[0].length && is("Function", cond_f)){
var i;
/** @type {numbers[]} */
var re = [];
for(i = 0; i < params.length; ++i){
var first_type = decltype(params[i][0]);
if(!("Number" === first_type || "String" === first_type) || !is("String", params[i][1]) || 2 !== params[i].length){
throw new TypeError("unexpected input. params[" + i + "][0]:" + decltype(params[i][0]) + " params[" + i + "][1]:" + decltype(params[i][1]));
}
if(cond_f(params[i][1])) {
re.push(("String" === first_type) ? parseInt(params[i][0], 10) : params[i][0]);
}
}
return re.sort(function(a,b){return a - b});
}
else{
throw new TypeError("unexpected input. params:" + decltype(params)).join();
}
}
/**
* @brief check 2*n range and list-up when `params[i][1]` is equal to `check_string`.
* @param {object[][]} params
* @param {string} check_string
* @returns {numbers[]}
*/
function list_if_equal(params, check_string) {
return list_if(params, function(s){ return s === check_string; });
}
/**
* @brief check 2*n range and list-up when `params[i][1]` is equal to `check_string`.
* @param {object[][]} params
* @param {string} check_string
* @returns {string} numbers string(separated ,).
*/
function list_if_equal_as_string(params, check_string) {
return list_if_equal(params, check_string).join();
}
function testf(){
var f = function(expression_statements, correct){
console.log(expression_statements);
var re = eval(expression_statements);
if(re !== correct){
console.log("fail:");
jsdiff.diffChars(re, correct).forEach(function(part){
// green for additions, red for deletions
// grey for common parts
var color = part.added ? "green" :
part.removed ? "red" : "grey";
process.stderr.write(part.value[color]);
});
process.stderr.write("\n");
}else{
console.log("pass.");
}
};
f("list_duplicate_num_as_string([1, 2, 3], 1, 3)", "nothing");
f("list_duplicate_num_as_string([1, 3, 4], 1, 4)", "2");
f("list_duplicate_num_as_string([1, 3, 4], 5, 6)", "5,6");
f("list_duplicate_num_as_string([3, 4, 8, 11], 1, 12)", "1,2,5,6,7,9,10,12");
f("list_duplicate_num_as_string([3, 4, 8, 11, 13], 1, 12)", "1,2,5,6,7,9,10,12");
f("list_duplicate_num_as_string([3, 4, 8, 11, 13, 14, 15, 17, 18, 19, 20, 21, 23, 16, 15, 24, 32, 27], 1, 33)", "1,2,5,6,7,9,10,12,22,25,26,28,29,30,31,33");
}
testf();