forked from zhaozhiming/fcc_4_29
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (173 loc) · 5.01 KB
/
index.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
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
import fs from 'fs';
const INPUT = './input_file_large.txt';
const OUTPUT = './output.txt';
const TEMP_FILE_1 = './temp1.txt';
const TEMP_FILE_2 = './temp2.txt';
const TEMP_FILE_3 = './temp3.txt';
const TEMP_FILE_4 = './temp4.txt';
const TEMP_FILE_5 = './temp5.txt';
const TEMP_FILE_6 = './temp6.txt';
const TEMP_FILE_7 = './temp7.txt';
const TEMP_FILE_8 = './temp8.txt';
const MAX = 4375000000;
// init: delete template and output files
function init() {
if (fs.existsSync(TEMP_FILE_1)) {
fs.unlinkSync(TEMP_FILE_1);
}
if (fs.existsSync(TEMP_FILE_2)) {
fs.unlinkSync(TEMP_FILE_2);
}
if (fs.existsSync(TEMP_FILE_3)) {
fs.unlinkSync(TEMP_FILE_3);
}
if (fs.existsSync(TEMP_FILE_4)) {
fs.unlinkSync(TEMP_FILE_4);
}
if (fs.existsSync(TEMP_FILE_5)) {
fs.unlinkSync(TEMP_FILE_5);
}
if (fs.existsSync(TEMP_FILE_6)) {
fs.unlinkSync(TEMP_FILE_6);
}
if (fs.existsSync(TEMP_FILE_7)) {
fs.unlinkSync(TEMP_FILE_7);
}
if (fs.existsSync(TEMP_FILE_8)) {
fs.unlinkSync(TEMP_FILE_8);
}
if (fs.existsSync(OUTPUT)) {
fs.unlinkSync(OUTPUT);
}
}
function wtireToTempFile(lines) {
console.time('write template file');
const temp1 = [];
const temp2 = [];
const temp3 = [];
const temp4 = [];
const temp5 = [];
const temp6 = [];
const temp7 = [];
const temp8 = [];
for (const line of lines) {
const [num] = line.split(',');
const number = Number(num);
if (number < MAX / 8) {
temp1.push(line);
} else if (number > MAX / 8 && number < MAX / 8 * 2) {
temp2.push(line);
} else if (number > MAX / 8 * 2 && number < MAX / 8 * 3) {
temp3.push(line);
} else if (number > MAX / 8 * 3 && number < MAX / 8 * 4) {
temp4.push(line);
} else if (number > MAX / 8 * 4 && number < MAX / 8 * 5) {
temp5.push(line);
} else if (number > MAX / 8 * 5 && number < MAX / 8 * 6) {
temp6.push(line);
} else if (number > MAX / 8 * 6 && number < MAX / 8 * 7) {
temp7.push(line);
} else if (number > MAX / 8 * 7 && number < MAX) {
temp8.push(line);
}
}
fs.appendFileSync(TEMP_FILE_1, temp1.join('\n'));
fs.appendFileSync(TEMP_FILE_2, temp2.join('\n'));
fs.appendFileSync(TEMP_FILE_3, temp3.join('\n'));
fs.appendFileSync(TEMP_FILE_4, temp4.join('\n'));
fs.appendFileSync(TEMP_FILE_5, temp5.join('\n'));
fs.appendFileSync(TEMP_FILE_6, temp6.join('\n'));
fs.appendFileSync(TEMP_FILE_7, temp7.join('\n'));
fs.appendFileSync(TEMP_FILE_8, temp8.join('\n'));
console.timeEnd('write template file');
}
function writeResultFromTemp(tempFile) {
console.time('append result file');
const data = fs.readFileSync(tempFile, 'utf8');
const array = data.split('\n');
array.sort((a, b) => {
const numa = Number(a.split(',')[0]);
const numb = Number(b.split(',')[0]);
return numa - numb;
});
fs.appendFileSync(OUTPUT, array.join('\n'));
console.timeEnd('append result file');
}
function writeResult() {
writeResultFromTemp(TEMP_FILE_1);
writeResultFromTemp(TEMP_FILE_2);
writeResultFromTemp(TEMP_FILE_3);
writeResultFromTemp(TEMP_FILE_4);
writeResultFromTemp(TEMP_FILE_5);
writeResultFromTemp(TEMP_FILE_6);
writeResultFromTemp(TEMP_FILE_7);
writeResultFromTemp(TEMP_FILE_8);
}
function sortLargeData() {
let lines = [];
let count = 0;
const inputLineReader = require('readline').createInterface({
input: fs.createReadStream(INPUT),
});
inputLineReader
.on('line', line => {
lines.push(line);
count++;
if (count === 10000) {
wtireToTempFile(lines);
count = 0;
lines = [];
}
})
.on('close', () => {
writeResult();
});
}
function binarySearch(ar, el, compare) {
let m = 0;
let n = ar.length - 1;
while (m <= n) {
const k = n + m >> 1;
const cmp = compare(el, ar[k]);
if (cmp > 0) {
m = k + 1;
} else if (cmp < 0) {
n = k - 1;
} else {
return k;
}
}
return -m - 1;
}
function searchResult(tempFile, num) {
const data = fs.readFileSync(tempFile, 'utf8');
const array = data.split('\n');
const index = binarySearch(array, num, (a, b) => {
return a - Number(b.split(',')[0]);
});
console.log(`result:${array[index]}`);
}
function search(number) {
if (number < MAX / 8) {
searchResult(TEMP_FILE_1);
} else if (number > MAX / 8 && number < MAX / 8 * 2) {
searchResult(TEMP_FILE_2);
} else if (number > MAX / 8 * 2 && number < MAX / 8 * 3) {
searchResult(TEMP_FILE_3);
} else if (number > MAX / 8 * 3 && number < MAX / 8 * 4) {
searchResult(TEMP_FILE_4);
} else if (number > MAX / 8 * 4 && number < MAX / 8 * 5) {
searchResult(TEMP_FILE_5);
} else if (number > MAX / 8 * 5 && number < MAX / 8 * 6) {
searchResult(TEMP_FILE_6);
} else if (number > MAX / 8 * 6 && number < MAX / 8 * 7) {
searchResult(TEMP_FILE_7);
} else if (number > MAX / 8 * 7 && number < MAX) {
searchResult(TEMP_FILE_8);
}
}
// -------------main--------------
init();
sortLargeData();
// 有了template文件后再单独运行search方法
// search(Math.floor(Math.random() * MAX));