Created
January 28, 2021 04:13
-
-
Save shivam-tripathi/97f3d806bc9fe6dc4147fbe08b2c80ac to your computer and use it in GitHub Desktop.
Get all leetcode problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
async function main() { | |
const url = 'https://leetcode.com/api/problems/all/'; | |
const { data } = await axios({ method: 'GET', url }); | |
const getDifficulty = (level) => { | |
switch(level) { | |
case 1: return 'easy'; | |
case 2: return 'medium'; | |
case 3: return 'hard'; | |
default: return 'unknown'; | |
} | |
}; | |
const result = data.stat_status_pairs.sort((a, b) => a.difficulty.level - b.difficulty.level); | |
const questions = result.reduce((acc, item) => { | |
const difficultyLevel = getDifficulty(item.difficulty.level); | |
acc[difficultyLevel] = acc[difficultyLevel] || { problems: [] }; | |
acc[difficultyLevel].problems.push({ | |
url: `https://leetcode.com/problems/${item.stat.question__title_slug}`, | |
title: item.stat.question__title, | |
id: item.stat.frontend_question_id, | |
difficulty: difficultyLevel, | |
}); | |
return acc; | |
}, {}); | |
Object.keys(questions).forEach(level => { | |
questions[level].count = questions[level].problems.length; | |
}); | |
console.log(JSON.stringify(questions, null, 2)); | |
} | |
main().catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment