Skip to content

Commit

Permalink
Modify table layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-leung-coherent committed Mar 10, 2022
1 parent 0d00aaa commit 7149ff5
Show file tree
Hide file tree
Showing 18 changed files with 2,348 additions and 2,250 deletions.
18 changes: 18 additions & 0 deletions check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const foo = () => {
const a: number[] = [];
const start = Date.now();
for (let i = 0; i < 100000; i++) {
a.unshift(1);
}
return Date.now() - start;
}
console.log((foo()))
const bar = () => {
const a: number[] = [];
const start = Date.now();
for (let i = 0; i < 100000; i++) {
a.push(1);
}
return Date.now() - start;
}
console.log(bar())
404 changes: 202 additions & 202 deletions doc/table-1.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-10.md

Large diffs are not rendered by default.

399 changes: 200 additions & 199 deletions doc/table-11.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-2.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-3.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-4.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-5.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-6.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-7.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-8.md

Large diffs are not rendered by default.

404 changes: 202 additions & 202 deletions doc/table-9.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ cookie=...
* [Questions 1401 to 1600](./doc/table-8.md)
* [Questions 1601 to 1800](./doc/table-9.md)
* [Questions 1801 to 2000](./doc/table-10.md)
* [Questions 2001 to 2197](./doc/table-11.md)
* [Questions 2001 to 2198](./doc/table-11.md)
20 changes: 19 additions & 1 deletion script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dotenv.config({ path: ENV_PATH });
const PAGE_ITEM_SIZE = 200;

const main = async () => {
console.log('Begin script')
console.log('Begin request')
const requests = new Requests(process.env.cookie);
const question_count = await requests.get_question_count();
if (!question_count) {
Expand All @@ -37,13 +39,25 @@ const main = async () => {
console.error('Ids for attempted questions cannot be retrieved');
return;
}
console.log('End request')
console.log('Begin processing')
const completed_ids_set = new Set(completed_ids.map(({ id }) => id));
const attempted_ids_set = new Set(attempted_ids.map(({ id }) => id));
const title_value_to_id = Object.fromEntries(
raw_question_details.map(q => [q.title_value, q.id])
)
const title_value_to_question_folder = new Set(
raw_question_details
.map(q => q.title_value)
.filter(t => fs.existsSync(path.resolve(__dirname, `../Notes/Questions/${t}`)))
)
// const title_value_to_topic_folder = new Set(
// raw_question_details
// .map(q => q.title_value)
// .filter(t => fs.existsSync(path.resolve(__dirname, `../Notes/Topics/${t}`)))
// )
const post_processed_question_details = raw_question_details.map(
q => post_process_question_detail(q, completed_ids_set, attempted_ids_set, title_value_to_id)
q => post_process_question_detail(q, completed_ids_set, attempted_ids_set, title_value_to_id, title_value_to_question_folder)
);
const question_details_chunks = splice_array_chunks(post_processed_question_details, PAGE_ITEM_SIZE);
const tables = question_details_chunks.map(question_chunk => (
Expand All @@ -62,11 +76,15 @@ const main = async () => {
))
.join('\n')
);
console.log('End processing')
console.log('Begin file I/O')
const readme_text = readme_template_text.replace('{{ REPLACEMENT }}', content_bullets);
fs.writeFileSync(README_TARGET_PATH, readme_text);
tables.forEach((table, i) => {
fs.writeFileSync(path.resolve(__dirname, `../doc/table-${i + 1}.md`), table);
})
console.log('End file I/O')
console.log('End script')
}

main();
9 changes: 7 additions & 2 deletions script/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ export interface QueryBody {
}

export interface DisplayValue {
display: string,
value: string
display: string;
value: string;
}

export interface DisplayValues {
display: string;
values: string[];
}

export interface RawQuestionDetail {
Expand Down
26 changes: 13 additions & 13 deletions script/utils/make_display_value_lists.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { DisplayValue } from '../types';
import { DisplayValues } from '../types';

const sanitize_inline_html = (inline_html: string): string => (
inline_html.replace(/\n|\s{2,}|(<!--.*-->)/g, '')
);

const make_display_value_lists = (topics: DisplayValue[]) => {
const topic_elements = topics.map(({ display, value }) => (/* html */`
<dt>
[${sanitize_inline_html(display)}](${value})
</dt>
`));
const full_topic_list = /* html */`
<dl>
${topic_elements.join('')}
</dl>
`;
return sanitize_inline_html(full_topic_list);
const make_display_value_lists = (topics: DisplayValues[]) => {
const topic_elements = topics.map(({ display, values }) => {
const sanitized_display = /* html */`<dt>${sanitize_inline_html(display)}</dt>`;
const sanitized_values = (
values
.map(v => (/* html */`<dd>${sanitize_inline_html(v)}</dd>`))
.join('<br />')
);
return sanitized_display + sanitized_values;
});
// const full_topic_list = /* html */`<dl>${topic_elements.join('')}</dl>`;
return /* html */`<dl>${topic_elements.join('')}</dl>`;
};

export default make_display_value_lists;
44 changes: 36 additions & 8 deletions script/utils/post_process_question_detail.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import make_display_value_lists from './make_display_value_lists';
import { DIFFICULTY, STATUS, PROBLEMS_URL, TAG_URL } from '../constants';
import { DisplayValue, RawQuestionDetail } from '../types';
import { DisplayValues, RawQuestionDetail } from '../types';
import { splice_array_chunks } from '../utils';

const delimiter = (items: string[]) => {
if (items[0] === ' ') {
items.shift();
}
else {
items.unshift('-');
}
};

const post_process_question_detail = (
{
Expand All @@ -15,22 +25,34 @@ const post_process_question_detail = (
completed_ids_set: Set<RawQuestionDetail['id']>,
attempted_ids_set: Set<RawQuestionDetail['id']>,
title_value_to_id: Record<string, string>,
title_value_to_question_folder: Set<RawQuestionDetail['id']>,
): Record<string, string> => {
const post_process_title = (display: string, value: string) => ({
const post_process_display_values = (display: string, value: string): DisplayValues => ({
display: `${title_value_to_id[value]}. ${display}`,
value: `${PROBLEMS_URL}/${value}`,
// display: (
// splice_array_chunks(`${title_value_to_id[value]}. ${display}`.split(''), 30, { delimiter })
// .map(it => it.join(''))
// .join('<br />')
// ),
values: (
[
'[LeetCode Link]' + `(${PROBLEMS_URL}/${value})`,
title_value_to_question_folder.has(value) ? '[My Notes]' + `(to be filled)` : null
]
.filter((arg): arg is string => Boolean(arg))
),
});

// Fine to have question that have no similar questions
// TODO: Enhance stricter type
let similar_questions: DisplayValue[];
let similar_questions: DisplayValues[];
try {
similar_questions = (
JSON.parse(raw_similar_questions)
// Seems like data returned from backend is well-sorted
// BUT, just in case any data is dirty due to unknown issues
.sort((q1, q2) => +title_value_to_id[q1.titleSlug as string] - +title_value_to_id[q2.titleSlug as string])
.map(({ title, titleSlug }) => post_process_title(title, titleSlug))
.map(({ title, titleSlug }) => post_process_display_values(title, titleSlug))
);
}
catch {
Expand All @@ -46,11 +68,17 @@ const post_process_question_detail = (
: STATUS.TODO
);

const title: DisplayValue = post_process_title(title_display, title_value);
const title: DisplayValues = post_process_display_values(title_display, title_value);

const topics = raw_topics.map(({ display, value }) => ({
const topics: DisplayValues[] = raw_topics.map(({ display, value }) => ({
display,
value: `${TAG_URL}/${value}`
values: (
[
'[LeetCode Link]' + `(${TAG_URL}/${value})`,
title_value_to_question_folder.has(value) ? '[My Notes]' + `(to be filled)` : null
]
.filter((arg): arg is string => Boolean(arg))
)
}));

const is_free_access = !is_premium;
Expand Down
40 changes: 34 additions & 6 deletions script/utils/splice_array_chunks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { is_stringified_safe_integer } from '../validators';

const splice_array_chunks = <T, >(raw_array: T[], chunkSize: number) => {
if (!is_stringified_safe_integer(chunkSize.toString()) || chunkSize > raw_array.length) {
const splice_array_chunks = <T, >(
raw_array: T[],
chunkSize: number,
options?: {
delimiter?: (items: T[]) => void;
},
// delimiter?: T
) => {
if (!is_stringified_safe_integer(chunkSize.toString())) {
return [];
}
const res: T[][] = [];
while (raw_array.length > 0) {
res.push(raw_array.splice(0, chunkSize));
if (chunkSize > raw_array.length) {
return [raw_array];
}
return res;
const result: T[][] = [];
while (raw_array.length > 1) {
result.push(raw_array.splice(0, chunkSize));
options?.delimiter?.(raw_array);
}
return result;
}


// const test_string = "2006. Count";
// console.log(
// splice_array_chunks(test_string.split(''), 9, {
// delimiter: (items: string[]) => {
// if (items[0] === ' ') {
// items.shift();
// }
// else {
// items.unshift('-');
// }
// },
// })
// .map(it => it.join(''))
// // .join('<br />')
// )

export default splice_array_chunks;

0 comments on commit 7149ff5

Please sign in to comment.