diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 00000000..b1ce32d7 --- /dev/null +++ b/.github/workflows/node.js.yml @@ -0,0 +1,31 @@ +# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs + +name: Node.js CI + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [14.x, 16.x, 18.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - run: npm ci + - run: npm run build --if-present + - run: npm test diff --git a/README.md b/README.md index c55355c4..81e7c9dc 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ # Problem Solving using Javascript -[![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.svg?v=103)](https://github.com/vinitshahdeo/HacktoberFest) [![Build Status](https://travis-ci.org/knaxus/problem-solving-javascript.svg?branch=master)](https://travis-ci.org/knaxus/problem-solving-javascript) [![Coverage Status](https://coveralls.io/repos/github/knaxus/problem-solving-javascript/badge.svg?branch=master)](https://coveralls.io/github/knaxus/problem-solving-javascript?branch=master) ![GitHub stars](https://img.shields.io/github/stars/knaxus/problem-solving-javascript) @@ -41,6 +40,6 @@ Find the detailed contents and problem list here: [Table Of Contents](TOC.md) ## Contribution Guide -It's great to know that you want to contribute to this repo. Thanks for taking interest. please find the [guide here](https://github.com/knaxus/problem-solving-javascript/blob/master/CONTRIBUTING.md) +It's great to know that you want to contribute to this repo. Thanks for taking interest. Please find the [guide here](https://github.com/knaxus/problem-solving-javascript/blob/master/CONTRIBUTING.md) -Keep an eye on this guide, it's subjected to change frequently. +Keep an eye on this guide. It's subject to frequent change. diff --git a/TOC.md b/TOC.md index 7765ff77..58248555 100644 --- a/TOC.md +++ b/TOC.md @@ -76,7 +76,7 @@ - [Next Greater for Every Element in an Array](src/_Problems_/next-greater-element) - [Compose Largest Number](src/_Problems_/compose-largest-number) - [Rotate Image](src/_Problems_/rotate-image) - +- [3 Sum](src/_Problems_/3Sum/) ### Searching - [Binary Search](src/_Searching_/BinarySearch) diff --git a/package-lock.json b/package-lock.json index 7a1c9c73..d3a3ed44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2248,9 +2248,9 @@ } }, "node_modules/decode-uri-component": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz", - "integrity": "sha512-hjf+xovcEn31w/EUYdTXQh/8smFL/dzYjohQGEIgjyNavaJfBY2p5F527Bo1VPATxv0VYTUC2bOcXvqFwk78Og==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.2.tgz", + "integrity": "sha512-FqUYQ+8o158GyGTrMFJms9qh3CqTKvAqgqsTnkLI8sKu0028orqBhxNMFkFen0zGyg6epACD32pjVk58ngIErQ==", "dev": true, "engines": { "node": ">=0.10" @@ -5228,9 +5228,9 @@ "dev": true }, "node_modules/json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true, "bin": { "json5": "lib/cli.js" @@ -7838,9 +7838,9 @@ } }, "node_modules/tsconfig-paths/node_modules/json5": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", - "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz", + "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==", "dev": true, "dependencies": { "minimist": "^1.2.0" @@ -10164,9 +10164,9 @@ "dev": true }, "decode-uri-component": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz", - "integrity": "sha512-hjf+xovcEn31w/EUYdTXQh/8smFL/dzYjohQGEIgjyNavaJfBY2p5F527Bo1VPATxv0VYTUC2bOcXvqFwk78Og==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.2.tgz", + "integrity": "sha512-FqUYQ+8o158GyGTrMFJms9qh3CqTKvAqgqsTnkLI8sKu0028orqBhxNMFkFen0zGyg6epACD32pjVk58ngIErQ==", "dev": true }, "deep-is": { @@ -12459,9 +12459,9 @@ "dev": true }, "json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, "jsprim": { @@ -14504,9 +14504,9 @@ }, "dependencies": { "json5": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", - "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz", + "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==", "dev": true, "requires": { "minimist": "^1.2.0" diff --git a/src/_DataStructures_/Set/setTest.js b/src/_DataStructures_/Set/setTest.js new file mode 100644 index 00000000..36aea9ff --- /dev/null +++ b/src/_DataStructures_/Set/setTest.js @@ -0,0 +1,43 @@ +const letters = require('./index'); + +describe('Data Structure : Set', () => { + + + it('X Set should be a Class', () => { + expect(typeof XSet.prototype.constructor).toEqual('function'); + }); + + describe('Creation of Set', () => { + + it('Should create a new Set with no elements', () => { + letters = new XSet(); + expect(letters === 0); + }); + + it('Should add letter A', () => { + letters.add('a'); + expect(letters.has('a'); + }); + + it('Should add letter B', () => { + letters.add('b'); + expect(letters.has('b'); + }); + + it('Should add letter C', () => { + letters.add('c'); + expect(letters.has('c'); + }); + + it('Should remove letter A', () => { + letters.remove('a'); + expect(!letters.has('a'); + }); + + it('Should remove letter B', () => { + letters.remove('b'); + expect(!letters.has('b'); + }); + + + }); diff --git a/src/_Problems_/3Sum/3sum.js b/src/_Problems_/3Sum/3sum.js new file mode 100644 index 00000000..6e7f5366 --- /dev/null +++ b/src/_Problems_/3Sum/3sum.js @@ -0,0 +1,39 @@ +const threeSum = function(nums) { + // sort the array + nums = nums.sort((a, b) => a - b); + + let result = []; + // iterate through the array and use two pointers to find the sum + for (let i = 0; i < nums.length; ++i) { + let left = i + 1; + let right = nums.length - 1; + while (left < right) { + let sum = nums[i] + nums[left] + nums[right]; + if (sum == 0) { + result.push([nums[i], nums[left], nums[right]]); + left++; + right--; + } + else if (sum < 0) { + left++; + } + else { + right--; + } + } + // skip duplicates + while (i < nums.length - 1 && nums[i] == nums[i + 1]) { + i++; + } + } + + // initialize set to remove duplicate + const set = new Set(result.map(JSON.stringify)); + // final output array + output = (new Array(...set).map(JSON.parse)); + return output; +}; + + +module.exports = threeSum; + diff --git a/src/_Problems_/3Sum/3sum.test.js b/src/_Problems_/3Sum/3sum.test.js new file mode 100644 index 00000000..ad1114ef --- /dev/null +++ b/src/_Problems_/3Sum/3sum.test.js @@ -0,0 +1,19 @@ +const threeSum = require("./3sum"); + +describe("threeSum", () => { + it("Should return [[-1, -1, 2], [-1, 0, 1]]", () => { + expect(threeSum([-1, 0, 1, 2, -1, -4])).toEqual([ + [-1, -1, 2], + [-1, 0, 1], + ]); + }); + + it("Should return [[0, 0, 0]]", () => { + expect(threeSum([0, 0, 0])).toEqual([[0, 0, 0]]); + }); + + it("Should return [[-1, -1, 2]]", () => { + expect(threeSum([-1, 2, -1, -4])).toEqual([[-1, -1, 2]]); + }); + +});