diff --git a/index.js b/index.js index e189e725..65540ae3 100644 --- a/index.js +++ b/index.js @@ -1,67 +1,133 @@ -const express = require('express') -const app = express() -const port = 3001 - -const USERS = []; - -const QUESTIONS = [{ - title: "Two states", - description: "Given an array , return the maximum of the array?", - testCases: [{ - input: "[1,2,3,4,5]", - output: "5" - }] -}]; +// import {_allProblemsInfo} from './problems.js'; +// import {USERS} from './userInfo.js'; +// import {SUBMISSIONS} from './submissions.js'; +const { USERS } = require('./userInfo.js'); +const { _allProblems } = require('./problemList.js'); +const { SUBMISSIONS } = require('./submissionsList.js'); -const SUBMISSION = [ - -] +const express = require('express'); +const { Route } = require('react-router-dom'); +const app = express() +const port = 3001 -app.post('/signup', function(req, res) { +// const QUESTIONS = [{ +// title: "Two states", +// description: "Given an array , return the maximum of the array?", +// testCases: [{ +// input: "[1,2,3,4,5]", +// output: "5" +// }] +// }]; + +function checkUserExists(email) { + const user = USERS[email]; + if(user) + return true; + else + return false; +} + +function verifyLogin(email, password) { /// this function does all the functionalites mentioned in LogIN + // Retrieve the user object by email + const user = USERS[email]; + + // Check if the provided password matches the stored password + if (user.password === password) { + console.log("Login Successful !"); + return true; + } else { + console.log("Password is incorrect"); + return false; + } + +} + + +app.get('/signup', function(req, res) { // Add logic to decode body + const { email, password } = req.query; // body should have email and password - + if(!email || !password) { + res.status(400).send('Email and Password are required. You can try /signup?email=bharath&password=bharath'); + } //Store email and password (as is for now) in the USERS array above (only if the user with the given email doesnt exist) - - + if(checkUserExists(email)) { + res.status(400).send('User already exists'); + } + else{ + USERS[email] = {password: password, role: 'user'}; + res.status(200).send(`Hi ${email}, Your account created successfully`); + } // return back 200 status code to the client - res.send('Hello World!') }) -app.post('/login', function(req, res) { - // Add logic to decode body - // body should have email and password +app.get('/login', (req, res) => { + const { email, password } = req.query; + + if (!email || !password) { + return res.status(400).send('Email and Password are required. You can try /login?email=bharath&password=bharath'); + } + + if (USERS[email]) { + if (USERS[email].password === password) { + res.status(200).send(`Hi ${email}! Login Successful. `); + } else { + res.status(400).send('Password did not match'); + } + } else { + res.status(400).send('User Not Found'); + } +}); // Check if the user with the given email exists in the USERS array // Also ensure that the password is the same - - // If the password is the same, return back 200 status code to the client // Also send back a token (any random string will do for now) // If the password is not the same, return back 401 status code to the client - - res.send('Hello World from route 2!') -}) - -app.get('/questions', function(req, res) { +app.get('/problems', function(req, res) { //return the user all the questions in the QUESTIONS array - res.send("Hello World from route 3!") + res.json(_allProblems); }) -app.get("/submissions", function(req, res) { - // return the users submissions for this problem - res.send("Hello World from route 4!") +app.get('/submissions', (req, res) => { + const { title } = req.query; // Access 'title' from query parameters + if(!title) + { + res.status(404).json({ error: 'Title not Provided. Type /submissions?title=Two Sum You can also try title=Add Two Numbers' }); + + } + if (!SUBMISSIONS[title]) { + // Handle case where title is missing or not found + res.status(404).json({ error: 'Title not found' }); + } else { + // Respond with submissions for the requested title + res.json(SUBMISSIONS[title]); + } }); - app.post("/submissions", function(req, res) { + const { title, user, code } = req.body; + const status = Math.random() < 0.5 ? 'Rejected' : 'Accepted'; + + if (!title || !user || !code) { + return res.status(400).send('Title, Username and code are required'); + } + SUBMISSIONS[title][user] = { code, status }; + return res.status(200).send('Submission successful'); // let the user submit a problem, randomly accept or reject the solution // Store the submission in the SUBMISSION array above - res.send("Hello World from route 4!") + +}); + +app.get("/", function(req, res) { + return res.json({ message: 'Hey Try these routes', Routes: ['/signup', '/login', '/problems', '/submissions'] }); + // let the user submit a problem, randomly accept or reject the solution + // Store the submission in the SUBMISSION array above + }); // leaving as hard todos diff --git a/problemList.js b/problemList.js new file mode 100644 index 00000000..230396eb --- /dev/null +++ b/problemList.js @@ -0,0 +1,448 @@ +const _supportedLanguages = ['javascript', 'python', 'cpp']; + + const _allProblems = [ + { + 'title': 'Two Sum', + 'acceptance': 53.1, + 'difficulty': {'level': 'easy', 'color': 'green'} + }, + { + 'title': 'Add Two Numbers', + 'acceptance': 43.4, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Longest Substring Without Repeating Characters', + 'acceptance': 35.1, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Median of Two Sorted Arrays', + 'acceptance': 40.7, + 'difficulty': {'level': 'hard', 'color': 'red'} + }, + { + 'title': 'Longest Palindromic Substring', + 'acceptance': 34.1, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Zigzag Conversion', + 'acceptance': 48.6, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Reverse Integer', + 'acceptance': 28.9, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'String to Integer (atoi)', + 'acceptance': 17.6, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Palindrome Number', + 'acceptance': 56.9, + 'difficulty': {'level': 'easy', 'color': 'green'} + }, + { + 'title': 'Regular Expression Matching', + 'acceptance': 28.3, + 'difficulty': {'level': 'hard', 'color': 'red'} + }, + { + 'title': 'Container With Most Water', + 'acceptance': 55.7, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Integer to Roman', + 'acceptance': 65.5, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Roman to Integer', + 'acceptance': 61.9, + 'difficulty': {'level': 'easy', 'color': 'green'} + }, + { + 'title': 'Longest Common Prefix', + 'acceptance': 43.2, + 'difficulty': {'level': 'easy', 'color': 'green'} + }, + { + 'title': '3Sum', + 'acceptance': 34.9, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': '3Sum Closest', + 'acceptance': 45.9, + 'difficulty': {'level': 'medium', 'color': 'orange'} + }, + { + 'title': 'Letter Combinations of a Phone Number', + 'acceptance': 61.0, + 'difficulty': {'level': 'medium', 'color': 'orange'} + } +]; + +const _allProblemsInfo = [ + { + title: "Two Sum", + description: "Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.", + examples: [ + { + input: { nums: [2, 7, 11, 15], target: 9 }, + output: [0, 1], + explanation: "nums[0] + nums[1] = 2 + 7 = 9" + }, + { + input: { nums: [3, 2, 4], target: 6 }, + output: [1, 2], + explanation: "nums[1] + nums[2] = 2 + 4 = 6" + } + ], + code: { + javascript: ` +function twoSum(nums, target) { + let map = {}; + for (let i = 0; i < nums.length; i++) { + let complement = target - nums[i]; + if (complement in map) { + return [map[complement], i]; + } + map[nums[i]] = i; + } + return []; +} +`, + python: ` +# Python equivalent for reference: +class Solution: + def twoSum(self, nums, target): + map = {} + for i, num in enumerate(nums): + complement = target - num + if complement in map: + return [map[complement], i] + map[num] = i + return [] +`, + cpp: ` +// C++ equivalent for reference: +class Solution { +public: + vector twoSum(vector& nums, int target) { + unordered_map map; + for (int i = 0; i < nums.size(); ++i) { + int complement = target - nums[i]; + if (map.count(complement)) { + return { map[complement], i }; + } + map[nums[i]] = i; + } + return {}; + } +}; +` + } + }, + { + title: "Add Two Numbers", + description: "Given two non-empty linked lists representing two non-negative integers, where each node contains a single digit. Add the two numbers and return the sum as a linked list.", + examples: [ + { + input: { l1: { val: 2, next: { val: 4, next: { val: 3, next: null } } }, l2: { val: 5, next: { val: 6, next: { val: 4, next: null } } } }, + output: { val: 7, next: { val: 0, next: { val: 8, next: null } } }, + explanation: "342 + 465 = 807" + }, + { + input: { l1: { val: 0, next: null }, l2: { val: 0, next: null } }, + output: { val: 0, next: null }, + explanation: "0 + 0 = 0" + } + ], + code: { + javascript: ` +function addTwoNumbers(l1, l2) { + let dummy = new ListNode(0); + let current = dummy; + let carry = 0; + while (l1 || l2 || carry) { + let sum = carry + (l1 ? l1.val : 0) + (l2 ? l2.val : 0); + carry = Math.floor(sum / 10); + current.next = new ListNode(sum % 10); + current = current.next; + l1 = l1 ? l1.next : null; + l2 = l2 ? l2.next : null; + } + return dummy.next; +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "Longest Substring Without Repeating Characters", + description: "Given a string s, find the length of the longest substring without repeating characters.", + examples: [ + { + input: "abcabcbb", + output: 3, + explanation: "The longest substring without repeating characters is 'abc', which has length 3." + }, + { + input: "bbbbb", + output: 1, + explanation: "The longest substring without repeating characters is 'b', which has length 1." + } + ], + code: { + javascript: ` +function lengthOfLongestSubstring(s) { + let map = {}; + let start = 0; + let maxLength = 0; + for (let end = 0; end < s.length; end++) { + if (map[s[end]] !== undefined) { + start = Math.max(map[s[end]] + 1, start); + } + map[s[end]] = end; + maxLength = Math.max(maxLength, end - start + 1); + } + return maxLength; +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "Median of Two Sorted Arrays", + description: "Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.", + examples: [ + { + input: { nums1: [1, 3], nums2: [2] }, + output: 2.0, + explanation: "Merged array = [1, 2, 3], median is 2.0." + }, + { + input: { nums1: [1, 2], nums2: [3, 4] }, + output: 2.5, + explanation: "Merged array = [1, 2, 3, 4], median is (2 + 3) / 2 = 2.5." + } + ], + code: { + javascript: ` +function findMedianSortedArrays(nums1, nums2) { + let merged = []; + let i = 0, j = 0; + while (i < nums1.length && j < nums2.length) { + if (nums1[i] <= nums2[j]) { + merged.push(nums1[i]); + i++; + } else { + merged.push(nums2[j]); + j++; + } + } + while (i < nums1.length) { + merged.push(nums1[i]); + i++; + } + while (j < nums2.length) { + merged.push(nums2[j]); + j++; + } + let n = merged.length; + if (n % 2 === 0) { + return (merged[n / 2 - 1] + merged[n / 2]) / 2; + } else { + return merged[Math.floor(n / 2)]; + } +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "Longest Palindromic Substring", + description: "Given a string s, return the longest palindromic substring in s.", + examples: [ + { + input: "babad", + output: "bab", + explanation: "'bab' is a palindromic substring." + }, + { + input: "cbbd", + output: "bb", + explanation: "'bb' is a palindromic substring." + } + ], + code: { + javascript: ` +function longestPalindrome(s) { + if (s.length < 2) return s; + let start = 0, maxLength = 1; + function expandAroundCenter(left, right) { + while (left >= 0 && right < s.length && s[left] === s[right]) { + let currentLength = right - left + 1; + if (currentLength > maxLength) { + maxLength = currentLength; + start = left; + } + left--; + right++; + } + } + for (let i = 0; i < s.length; i++) { + expandAroundCenter(i, i); + expandAroundCenter(i, i + 1); + } + return s.substring(start, start + maxLength); +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "Zigzag Conversion", + description: "The string 'PAYPALISHIRING' is written in a zigzag pattern on a given number of rows. Return the zigzag pattern as a string.", + examples: [ + { + input: { s: "PAYPALISHIRING", numRows: 3 }, + output: "PAHNAPLSIIGYIR", + explanation: "Rows = 3: P A H N A P L S I I G Y I R" + }, + { + input: { s: "PAYPALISHIRING", numRows: 4 }, + output: "PINALSIGYAHRPI", + explanation: "Rows = 4: P I N A L S I G Y A H R P I" + } + ], + code: { + javascript: ` +function convert(s, numRows) { + if (numRows === 1) return s; + let rows = new Array(numRows).fill(""); + let currentRow = 0; + let goingDown = false; + for (let char of s) { + rows[currentRow] += char; + if (currentRow === 0 || currentRow === numRows - 1) goingDown = !goingDown; + currentRow += goingDown ? 1 : -1; + } + return rows.join(""); +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "Reverse Integer", + description: "Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], return 0.", + examples: [ + { + input: 123, + output: 321, + explanation: "Reverse of 123 is 321." + }, + { + input: -123, + output: -321, + explanation: "Reverse of -123 is -321." + } + ], + code: { + javascript: ` +function reverse(x) { + let reversed = parseInt(Math.abs(x).toString().split('').reverse().join('')) * Math.sign(x); + if (reversed < Math.pow(-2, 31) || reversed > Math.pow(2, 31) - 1) return 0; + return reversed; +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "String to Integer (atoi)", + description: "Implement the atoi function, which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign, followed by as many numerical digits as possible, and interprets them as a numerical value.", + examples: [ + { + input: "42", + output: 42, + explanation: "The input string '42' is converted to the integer 42." + }, + { + input: " -42", + output: -42, + explanation: "The input string ' -42' is converted to the integer -42." + } + ], + code: { + javascript: ` +function myAtoi(s) { + let sign = 1, i = 0, result = 0; + while (s[i] === ' ') i++; + if (s[i] === '+' || s[i] === '-') { + sign = s[i++] === '-' ? -1 : 1; + } + while (i < s.length && s[i] >= '0' && s[i] <= '9') { + result = result * 10 + (s[i++] - '0'); + if (result * sign >= Math.pow(2, 31) - 1) return Math.pow(2, 31) - 1; + if (result * sign <= Math.pow(-2, 31)) return Math.pow(-2, 31); + } + return result * sign; +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + }, + { + title: "Palindrome Number", + description: "Determine whether an integer x is a palindrome. An integer is a palindrome when it reads the same backward as forward.", + examples: [ + { + input: 121, + output: true, + explanation: "121 is a palindrome." + }, + { + input: -121, + output: false, + explanation: "-121 is not a palindrome (reads as '121-')." + } + ], + code: { + javascript: ` +function isPalindrome(x) { + if (x < 0) return false; + let reversed = parseInt(x.toString().split('').reverse().join('')); + return x === reversed; +} +`, + // Add Python and C++ equivalents similarly + python: ``, + cpp: `` + } + } + // Add more problems as needed +]; + +module.exports = { _allProblemsInfo, _allProblems}; \ No newline at end of file diff --git a/submissionsList.js b/submissionsList.js new file mode 100644 index 00000000..178b0075 --- /dev/null +++ b/submissionsList.js @@ -0,0 +1,39 @@ +const SUBMISSIONS = { + 'Two Sum': { + 'bharath': { + 'code': 'print("Hello Bharath")', + 'status': 'Accepted', + }, + 'kirat': { + 'code': 'print("Hello Kirat")', + 'status': 'Accepted', + }, + }, + + + 'Add Two Numbers': { + 'bharath': { + 'code': 'print("Hello Bharath")', + 'status': 'Rejected', + }, + 'kirat': { + 'code': 'print("Hello Kirat")', + 'status': 'Accepted', + }, + }, + + + 'Longest Substring Without Repeating Characters': { + 'bharath': { + 'code': 'print("Hello Bharath")', + 'status': 'Accepted', + }, + 'kirat': { + 'code': 'print("Hello Kirat")', + 'status': 'Rejected', + }, + }, + }; + + +module.exports = { SUBMISSIONS}; \ No newline at end of file diff --git a/userInfo.js b/userInfo.js new file mode 100644 index 00000000..253f64e2 --- /dev/null +++ b/userInfo.js @@ -0,0 +1,15 @@ +const USERS = +{ + // username: {password, role} + 'bharath' : { + 'password': 'bharath', + 'role': 'admin', + }, + + 'kirat' : { + 'password': 'kirat', + 'role': 'user', + }, +} + +module.exports = { USERS }; \ No newline at end of file