Commit a55a3860 authored by CrioUser's avatar CrioUser
Browse files

Initial commit

parents
node_modules/
*.lock
.idea/
*.log
package-lock.json
This diff is collapsed.
const axios = require('axios');
const objectsToCsv = require('objects-to-csv');
const fs = require('fs');
function getApiURL() {
// Returns a String denoting the API url which fetches all problems
return "https://leetcode.com/api/problems/all/";
}
async function getAllProblems() {
/* Returns a Promise object of the response on calling
the API to fetch all problems
*/
return new Promise((resolve, reject) => {
let url = getApiURL();
axios.get(url).then((response) => {
resolve(response.data);
}).catch((error) => {
reject(error);
});
});
}
function filterPaidProblems(data) {
/* Returns an Array with data on problems that aren't for paid users only
Input:
data - raw JSON data from the API response
Output:
Array of objects each denoting a free problem
*/
let dataToFilter = data.stat_status_pairs;
let filteredData = dataToFilter.filter(problem => !(problem.paid_only))
return filteredData;
}
function compare(first, second) {
return second.submissions - first.submissions;
}
function sortBySubmission(data) {
/* Returns data sorted by total submissions count in descending order
Input:
data - Array of objects denoting the problems with the id, question_title, submissions properties
Output:
input data sorted by the "submissions" property in descending order
*/
return data.sort(compare);
}
function getTopNProblems(N, data) {
/* Returns top n problems
Input:
N - number of problems to return
data - Array of objects denoting the problems with the id, question_title, submissions properties
Output:
Slice of top N problems
*/
return data.slice(0, N);
}
function reducer(accumulator, object) {
/* Returns an Array of objects reduced in its number of properties
Input:
accumulator - object which accumulates the reduced data
object - Object with properties question_id, question__article__live, question__article__slug, question__title, question__title_slug, question__hide, total_acs, total_submitted, frontend_question_id, is_new_question
Output:
accumulator storing data reduced to the properties: id denoting question_id, question__title, and submissions denoting total_submitted
*/
accumulator.push({
id: object.stat.frontend_question_id,
question_title: object.stat.question__title,
submissions: object.stat.total_submitted
});
return accumulator;
}
function transformData(data) {
/*
Returns transformed data with only required properties
Input:
data - Array of objects with properties question_id, question__article__live, question__article__slug, question__title, question__title_slug, question__hide, total_acs, total_submitted, frontend_question_id, is_new_question
Output:
Array of objects with properties id denoting question_id, question__title, and submissions denoting total_submitted
*/
let transformedData = data.reduce(reducer,[]);
return transformedData;
}
function getTopHundredProblems(allProblems) {
/* Returns the top 100 most submitted problems
Input:
allProblems - Raw API response
Output:
Array of objects with the question id, title and total submissions values for the
top 100 problems ordered by their total submissions in descending order
*/
let unpaidProblems = filterPaidProblems(allProblems);
let transformedProblems = transformData(unpaidProblems);
let problemsSortedBySubmission = sortBySubmission(transformedProblems);
let topHundredProblems = getTopNProblems(100, problemsSortedBySubmission);
return topHundredProblems;
}
async function writeToCSV(data, destination) {
/* Write data to a CSV file
Input
data - data to write
destination - CSV file destination
*/
const csv = new objectsToCsv(data);
await csv.toDisk(destination)
}
async function main() {
// Fetches data from the API and stores
// question id, title and total submissions for top 100 problems to CSV file
let allProblems = await getAllProblems();
console.log(allProblems);
// fs.writeFile("./problems-all.json", JSON.stringify(allProblems, null, 4), (err) => {
// if (err) {
// console.error(err);
// return;
// };
// });
let topHundredProblems = await getTopHundredProblems(allProblems);
writeToCSV(topHundredProblems, './list.csv');
}
//module.exports = {getApiURL};
//module.exports = {getApiURL, getAllProblems};
//module.exports = {getApiURL, getAllProblems, getTopHundredProblems};
module.exports = {getApiURL, getAllProblems, getTopHundredProblems, writeToCSV};
\ No newline at end of file
id,question_title,submissions
1,Two Sum,7213758
3,Longest Substring Without Repeating Characters,5586821
7,Reverse Integer,4693117
2,Add Two Numbers,4509985
8,String to Integer (atoi),3942817
15,3Sum,3795730
5,Longest Palindromic Substring,3472792
20,Valid Parentheses,2827945
98,Validate Binary Search Tree,2706525
4,Median of Two Sorted Arrays,2481397
26,Remove Duplicates from Sorted Array,2412275
53,Maximum Subarray,2404665
14,Longest Common Prefix,2296890
33,Search in Rotated Sorted Array,2279293
28,Implement strStr(),2079875
9,Palindrome Number,2074602
21,Merge Two Sorted Lists,2042626
151,Reverse Words in a String,2021076
19,Remove Nth Node From End of List,1935636
121,Best Time to Buy and Sell Stock,1908421
125,Valid Palindrome,1839949
29,Divide Two Integers,1809322
146,LRU Cache,1791383
69,Sqrt(x),1739470
91,Decode Ways,1735788
50,"Pow(x, n)",1731031
23,Merge k Sorted Lists,1723368
206,Reverse Linked List,1720630
200,Number of Islands,1708764
10,Regular Expression Matching,1706412
141,Linked List Cycle,1677807
56,Merge Intervals,1669957
88,Merge Sorted Array,1657172
35,Search Insert Position,1602978
66,Plus One,1592102
70,Climbing Stairs,1590942
283,Move Zeroes,1570220
189,Rotate Array,1535479
127,Word Ladder,1535198
34,Find First and Last Position of Element in Sorted Array,1517618
101,Symmetric Tree,1502470
55,Jump Game,1482671
139,Word Break,1481058
79,Word Search,1472892
136,Single Number,1448533
11,Container With Most Water,1410116
17,Letter Combinations of a Phone Number,1393608
27,Remove Element,1386944
198,House Robber,1383588
13,Roman to Integer,1358413
104,Maximum Depth of Binary Tree,1342958
6,ZigZag Conversion,1331286
155,Min Stack,1328591
49,Group Anagrams,1295524
322,Coin Change,1284548
278,First Bad Version,1279846
94,Binary Tree Inorder Traversal,1245645
160,Intersection of Two Linked Lists,1240279
215,Kth Largest Element in an Array,1231795
76,Minimum Window Substring,1229065
112,Path Sum,1227545
31,Next Permutation,1222743
58,Length of Last Word,1222552
204,Count Primes,1217410
102,Binary Tree Level Order Traversal,1209745
152,Maximum Product Subarray,1201364
138,Copy List with Random Pointer,1196029
344,Reverse String,1193308
169,Majority Element,1182994
234,Palindrome Linked List,1182691
111,Minimum Depth of Binary Tree,1171642
124,Binary Tree Maximum Path Sum,1167068
122,Best Time to Buy and Sell Stock II,1161458
54,Spiral Matrix,1160426
75,Sort Colors,1155710
65,Valid Number,1150277
42,Trapping Rain Water,1144064
100,Same Tree,1134606
41,First Missing Positive,1131685
217,Contains Duplicate,1110730
67,Add Binary,1106718
236,Lowest Common Ancestor of a Binary Tree,1097735
387,First Unique Character in a String,1094350
242,Valid Anagram,1088577
16,3Sum Closest,1086638
110,Balanced Binary Tree,1084666
83,Remove Duplicates from Sorted List,1080418
202,Happy Number,1072458
153,Find Minimum in Rotated Sorted Array,1063484
32,Longest Valid Parentheses,1060974
207,Course Schedule,1059027
44,Wildcard Matching,1040099
18,4Sum,1035195
46,Permutations,1023736
133,Clone Graph,1022457
39,Combination Sum,1021524
78,Subsets,1005980
203,Remove Linked List Elements,984006
238,Product of Array Except Self,983729
24,Swap Nodes in Pairs,983326
{
"name": "byte",
"version": "1.0.0",
"description": "",
"main": "leetcode.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0",
"jest": "^26.4.2",
"objects-to-csv": "^1.3.6"
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment