diff --git a/.editorconfig b/.editorconfig index 1a6d62e..3281cce 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,9 +6,9 @@ end_of_line = lf indent_size = 2 indent_style = tab insert_final_newline = true -max_line_length = 130 +max_line_length = 155 trim_trailing_whitespace = true [*.md] -max_line_length = 130 +max_line_length = 155 trim_trailing_whitespace = true diff --git a/.eslintrc.json b/.eslintrc.json index fd4d4cf..a9d94b6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,27 +1,30 @@ { - "env": { - "browser": true, - "commonjs": true, - "es2021": true - }, - "extends": [ - "eslint:recommended", - "airbnb-base" - ], - "parserOptions": { - "ecmaVersion": 12 - }, - "rules": { - "indent": ["error", "tab"], - "no-tabs": ["error", { "allowIndentationTabs": true }], - "max-len": ["error", { "code": 130 }], - "no-var": "error", - "semi": "error", - "no-multi-spaces": "error", - "space-in-parens": "error", - "no-multiple-empty-lines": "error", - "prefer-const": "error", - "no-use-before-define": "error" - } - } - + "env": { + "browser": true, + "commonjs": true, + "es2021": true + }, + "extends": ["eslint:recommended", "airbnb-base"], + "parserOptions": { + "ecmaVersion": 12 + }, + "rules": { + "indent": ["error", "tab"], + "no-tabs": ["warn", { "allowIndentationTabs": true }], + "max-len": ["error", { "code": 155 }], + "no-var": "error", + "semi": "error", + "no-multi-spaces": "error", + "space-in-parens": "error", + "no-multiple-empty-lines": "error", + "prefer-const": "error", + "no-use-before-define": "error", + "max-classes-per-file": ["warn", 1], + "no-plusplus": "off", + "no-param-reassign": "warn", + "no-restricted-syntax": "warn", + "no-continue": "warn", + "no-bitwise": "off", + "guard-for-in": "warn" + } +} diff --git a/.travis.yml b/.travis.yml index 9ef231a..133897e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,4 @@ install: - npm install script: - npm run test -- npm run lint +- eslint src test --ext .js diff --git a/Arrays/2d-array-ds.js b/Arrays/2d-array-ds.js deleted file mode 100644 index 1e5a28e..0000000 --- a/Arrays/2d-array-ds.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - Title: 2D Array - DS - Difficulty: Easy - Score: 15 - Link: https://www.hackerrank.com/challenges/2d-array/ -*/ -function hourglassSum(arr) { - var sum = -1000 - let max = sum - for(let i = 0 ; i<4;i++){ - for(let j = 0 ; j<4; j++){ - sum = arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2] - if(sum>max) max = sum - } - } - console.log(max) -} diff --git a/Arrays/apple-and-orange.js b/Arrays/apple-and-orange.js deleted file mode 100644 index ffaf16a..0000000 --- a/Arrays/apple-and-orange.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - Title: Apple and Orange - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/apple-and-orange -*/ - -function countApplesAndOranges(s, t, a, b, apples, oranges) { - countFruitsFallInsideHome(apples, s - a, t - a); - countFruitsFallInsideHome(oranges, s - b, t - b); -} - - -function countFruitsFallInsideHome(fruits, startHome, EndHome) { - let counter = 0; - fruits.forEach(fruit => { - if (fruit >= startHome && fruit <= EndHome) { - return counter++; - } - }); - - return console.log(counter); -} - -countApplesAndOranges(7, 11, 5, 15, 3, 2, [-2, 2, 1] , [5 -6]); - diff --git a/Arrays/array-manipulation.js b/Arrays/array-manipulation.js deleted file mode 100644 index 45eaf1d..0000000 --- a/Arrays/array-manipulation.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - Title: Array Manipulation - Difficulty: Hard - Score: 60 - Link: https://www.hackerrank.com/challenges/crush/problem -*/ - -function arrayManipulation(n, queries) { - let arr = Array(n).fill(0); - - for (let a = 0; a < queries.length; a++) { - for (let index = queries[a][0] - 1; index < queries[a][1]; index++) { - arr[index] += queries[a][2]; - } - } - - return Math.max(...arr); -} diff --git a/Arrays/birthday-cake-candles.js b/Arrays/birthday-cake-candles.js deleted file mode 100644 index 35ed0d7..0000000 --- a/Arrays/birthday-cake-candles.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - Title: Birthday Cake Candles - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/birthday-cake-candles/ -*/ - -module.exports = function birthdayCakeCandles(arr) { - let tallestCandle = 0; - let candlesCounter = 0; - for (let index = 0; index < arr.length; index++) { - if (arr[index] > tallestCandle) { - tallestCandle = arr[index]; - candlesCounter = 1; - } else if (arr[index] === tallestCandle) { - candlesCounter++; - } - } - - return candlesCounter; -} diff --git a/Arrays/breaking-best-and-worst-records.js b/Arrays/breaking-best-and-worst-records.js deleted file mode 100644 index 1afad77..0000000 --- a/Arrays/breaking-best-and-worst-records.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - Title: Breaking the Records - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/breaking-best-and-worst-records -*/ - -function breakingRecords(scores) { - - let counters = { - min: 0, - max: 0 - }; - - let values = { - min: 0, - max: 0 - } - let isDefaultSet = false; - - scores.forEach(score => { - if (!isDefaultSet) { - values.min = score; - values.max = score; - isDefaultSet = true; - return ; - } - - if (score > values.max) { - values.max = score; - counters.max++; - } else if (score < values.min) { - values.min = score; - counters.min++; - } - }) - - return [counters.max, counters.min]; - -} - -breakingRecords([10, 5, 20, 20, 4, 5, 2, 25, 1]) diff --git a/Arrays/compare-the-triplets.js b/Arrays/compare-the-triplets.js deleted file mode 100644 index 75d6007..0000000 --- a/Arrays/compare-the-triplets.js +++ /dev/null @@ -1,21 +0,0 @@ -/* -Title: Compare the Triplets -Difficulty: Easy -Max Score: 10 -Link: https://www.hackerrank.com/challenges/compare-the-triplets/problem -*/ - -function compareTriplets(a, b) { - let points = [0, 0]; - for (let index = 0; index < a.length; index++){ - if (a[index] > b[index]) { - points[0] += 1; - } else if (a[index] < b[index]) { - points[1] += 1; - } - } - - return points; -} - -console.log(compareTriplets([5, 6, 7], [3, 6, 10])) diff --git a/Arrays/diagonal-difference.js b/Arrays/diagonal-difference.js deleted file mode 100644 index 336b05d..0000000 --- a/Arrays/diagonal-difference.js +++ /dev/null @@ -1,22 +0,0 @@ - -/* - Title:Diagonal Difference - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/diagonal-difference -*/ - -function diagonalDifference(arr) { - let leftToRight = 0; - let rightToLeft = 0; - let tmpLength = arr.length; - for (let i = 0; i < arr.length; i++){ - leftToRight += arr[i][i]; - rightToLeft += arr[i][--tmpLength]; - } - - return Math.abs(leftToRight - rightToLeft); -} - -let input = [[11, 2, 4], [4, 5, 6], [10, 8, -12]] -console.log(diagonalDifference(input)) diff --git a/Arrays/jumping-on-the-clouds.js b/Arrays/jumping-on-the-clouds.js deleted file mode 100644 index 9f5c7d1..0000000 --- a/Arrays/jumping-on-the-clouds.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - Title: Jumping on the Clouds - Difficulty: Easy - Score: 20 - Link: https://www.hackerrank.com/challenges/jumping-on-the-clouds/ -*/ - -function jumpingOnClouds(c) { - - let jump = 0; - for (let i = 0; i < c.length;){ - if (c[i + 2] == 0) { - jump++; - i += 2; - } else if (c[i + 1] == 0) { - jump++; - i += 1; - } else { - i++; - } - } - - return jump; -} diff --git a/Arrays/minimum-swaps-2.js b/Arrays/minimum-swaps-2.js deleted file mode 100644 index c382556..0000000 --- a/Arrays/minimum-swaps-2.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - Title: Minimum Swaps 2 - Difficulty: Medium - Score: 40 - Link: https://www.hackerrank.com/challenges/minimum-swaps-2 -*/ - - -function minimumSwaps(arr) { - var origArray = arr.slice(0); - var sortedArray = arr.sort((a,b) => a-b); - let numberOfSwap = 0; - for (var i = 0; i < origArray.length; i++) { - if (origArray[i] != sortedArray[i]) { - for (var j = i + 1; j < origArray.length; j++) { - if (origArray[j] === sortedArray[i]) { - [origArray[i], origArray[j]] = [origArray[j], origArray[i]] - numberOfSwap++ - break; - } - } - } - } - - return numberOfSwap; -} diff --git a/Arrays/newyear-chaos.js b/Arrays/newyear-chaos.js deleted file mode 100644 index 0ae6270..0000000 --- a/Arrays/newyear-chaos.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: New Year Chaos - Difficulty: Medium - Score: 40 - Link: https://www.hackerrank.com/challenges/new-year-chaos/ -*/ - -function minimumBribes(q) { - let swaps = 0; - let min = q.length; - for (var i = q.length -1 ; i >= 0; i--) { - if (q[i] - i > 3) { - return `Too chaotic`; - } - if (q[i] > i + 1) { - swaps += (q[i] - (i + 1)); - } else { - if (min > q[i]) { - min = q[i]; - } else if (q[i] != min) { - swaps++; - } - } - } - - return swaps; -} diff --git a/Arrays/sock-merchant.js b/Arrays/sock-merchant.js deleted file mode 100644 index e865834..0000000 --- a/Arrays/sock-merchant.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Sock Merchant - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/sock-merchant -*/ - - -function sockMerchant(n, ar) { - let tmpArr = []; - let counter = 0; - ar.map(value => { - if (tmpArr.includes(value)) { - var index = tmpArr.indexOf(value); - if (index > -1) { - tmpArr.splice(index, 1); - } - counter++; - } else { - tmpArr.push(value); - } - - }) - - return counter; - -} diff --git a/Data Structures/cycle-detection.js b/Data Structures/cycle-detection.js deleted file mode 100644 index 5bd280a..0000000 --- a/Data Structures/cycle-detection.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - Title: Cycle Detection - Difficulty: Medium - Score: 5 - Link: https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem -*/ - -function hasCycle(head) { - if (head === null || head.next === null || head.next.next === null) return false; // edge cases - let hare = head; - let tortoise = head; - do { - hare = hare.next.next; // 'hare' moves twice per tick - tortoise = tortoise.next; // 'tortoise' moves once per tick - } while (hare !== tortoise && hare.next !== null && hare.next.next !== null); - return hare === tortoise; -} - -// NODE CLASS - -class Node { - constructor (data) { - this.data = data; - this.next = null; - } - insert (data) { - this.next = new Node(data); - return this.next; // for chaining - } -} - -module.exports = { - hasCycle, - Node, -}; \ No newline at end of file diff --git a/Data Structures/reverse-doubly-linked-list.js b/Data Structures/reverse-doubly-linked-list.js deleted file mode 100644 index 04dcde3..0000000 --- a/Data Structures/reverse-doubly-linked-list.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict'; -/* - Title: Reverse a doubly linked list - Difficulty: Easy - Score: 5 - Link: https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem -*/ - - -function reverse(head) { - while (head.next) { - head = head.next - } - - const newList = new DoublyLinkedList(); - - while (head) { - newList.insertNode(head.data); - head = head.prev - } - return newList.head -} - -class DoublyLinkedListNode { - constructor(nodeData) { - this.data = nodeData; - this.next = null; - this.prev = null; - } -}; - -class DoublyLinkedList { - constructor() { - this.head = null; - this.tail = null; - } - - insertNode(nodeData) { - let node = new DoublyLinkedListNode(nodeData); - - if (this.head == null) { - this.head = node; - } else { - this.tail.next = node; - node.prev = this.tail; - } - - this.tail = node; - } -}; - -module.exports = { - reverse, - DoublyLinkedList, - DoublyLinkedListNode -} diff --git a/Dictionaries and Hashmaps/count-triplet.js b/Dictionaries and Hashmaps/count-triplet.js deleted file mode 100644 index ac8d48c..0000000 --- a/Dictionaries and Hashmaps/count-triplet.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - Title: Count Triplets - Difficulty: Medium - Score: 35 - Link: https://www.hackerrank.com/challenges/count-triplets-1/ -*/ - -function countTriplets(arr, r) { - let second = {} - let third = {} - let count = 0; - for (let index = 0; index < arr.length; index++) { - - const val = arr[index]; - - //If value found on third then add it to count - if(third[val]){ - count += third[val]; - } - - //If value found on second , convey it to third - - if(second[val]){ - third[val*r] = third[val*r] + second[val] || second[val]; - } - - //add current element into second value - - second[val*r] = second[val*r]+1 || 1; - } - - return count; -} - -console.log(countTriplets([1,2 ,2,4], 2)); diff --git a/Dictionaries and Hashmaps/custom-hashmap.js b/Dictionaries and Hashmaps/custom-hashmap.js deleted file mode 100644 index c2431ec..0000000 --- a/Dictionaries and Hashmaps/custom-hashmap.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - Title: Custom Hashmap - Difficulty: - - Score: - - Link: - -*/ -class customHashMap { - constructor() { - this.list = []; - } - - get(x) { - return this.list[this.hashCode(x)]; - } - - set(x, y) { - this.list[this.hashCode(x)] = y; - } - - hashCode(str) { - let hash = 0; - if (str.length == 0) return hash; - for (let i = 0; i < str.length; i++) { - let char = str.charCodeAt(i); - hash = ((hash << 5) - hash) + char; - hash = hash & hash; // Convert to 32bit integer - } - return hash; - } -} - -let m = new customHashMap(); - -m.set('hello', 'world'); -console.log(m.get('hello')); diff --git a/Dictionaries and Hashmaps/frequency-queries.js b/Dictionaries and Hashmaps/frequency-queries.js deleted file mode 100644 index c3f4d5a..0000000 --- a/Dictionaries and Hashmaps/frequency-queries.js +++ /dev/null @@ -1,45 +0,0 @@ -/* - Title: Frequency Queries - Difficulty: Medium - Score: 40 - Link: https://www.hackerrank.com/challenges/frequency-queries/ -*/ - -function freqQuery(arr) { - const result = []; - const hash = {}; - const freq = []; - - for (let i = 0; i < arr.length; i += 1) { - const [action, value] = arr[i]; - const initValue = hash[value] || 0; - - if (action === 1) { - hash[value] = initValue + 1; - freq[initValue] = (freq[initValue] || 0) - 1; - freq[initValue + 1] = (freq[initValue + 1] || 0) + 1; - } - - if (action === 2 && initValue > 0) { - hash[value] = initValue - 1; - freq[initValue - 1] += 1; - freq[initValue] -= 1; - } - - if (action === 3) result.push(freq[value] > 0 ? 1 : 0); - } - - return result; -} - -let input = [ - [1, 5], - [1, 6], - [3, 2], - [1, 10], - [1, 10], - [1, 6], - [2, 5], - [3, 2] - ]; -console.log(freqQuery(input)); diff --git a/Dictionaries and Hashmaps/game-of-thrones.js b/Dictionaries and Hashmaps/game-of-thrones.js deleted file mode 100644 index 0d50f1e..0000000 --- a/Dictionaries and Hashmaps/game-of-thrones.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - Title: Game Of Thrones - I - Difficulty: Easy - Score: 30 - Link: https://www.hackerrank.com/challenges/game-of-thrones/problem -*/ - -function gameOfThrones(s) { - const frequencies = {}; - for (let char of s) frequencies[char] = frequencies[char] + 1 || 1; - let oddFrequencies = Object.values(frequencies).filter(frequency => frequency % 2); - return oddFrequencies.length <= 1 ? 'YES' : 'NO'; -} - -console.log(gameOfThrones('aaabbbb')); // YES -console.log(gameOfThrones('cdefghmnopqrstuvw')); // NO -console.log(gameOfThrones('cdcdcdcdeeeef')); // YES \ No newline at end of file diff --git a/Dictionaries and Hashmaps/ransom-note.js b/Dictionaries and Hashmaps/ransom-note.js deleted file mode 100644 index bf7003a..0000000 --- a/Dictionaries and Hashmaps/ransom-note.js +++ /dev/null @@ -1,57 +0,0 @@ -/* - Title: Hash Tables: Ransom Note - Difficulty: Easy - Score: 25 - Link: https://www.hackerrank.com/challenges/ctci-ransom-note/ -*/ - -class ExtendedMap { - - constructor(){ - this.map = new Map(); - } - - get(x) { - let value = this.map.get(x); - if (value === 1) { - this.map.delete(x); - return true; - } - - return this.map.set(x, --value); - } - - set(x, y) { - if (this.map.get(x)) { - return this.map.set(x, this.map.get(x) + 1); - } - - return this.map.set(x, y); - } -} - - -function checkMagazine(magazine, note) { - - let m = new ExtendedMap() - - for (let i = 0; i < magazine.length; i++) { - m.set(magazine[i], 1) - } - - for (let i = 0; i < note.length; i++) { - if (!m.get(note[i])) { - console.log('No'); - return; - }; - } - - console.log('Yes'); -} - -let magazine = [ 'give', 'me', 'one', 'grand', 'today', 'night' ]; -let note = [ 'give', 'one', 'grand', 'today' ]; - -console.time('Time Taken'); -checkMagazine(magazine, note); -console.timeEnd('Time Taken'); diff --git a/Dictionaries and Hashmaps/sherlock-and-anagrams.js b/Dictionaries and Hashmaps/sherlock-and-anagrams.js deleted file mode 100644 index 138f0db..0000000 --- a/Dictionaries and Hashmaps/sherlock-and-anagrams.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Sherlock and Anagrams - Difficulty: Medium - Score: 50 - Link: https://www.hackerrank.com/challenges/sherlock-and-anagrams/ -*/ -function sherlockAndAnagrams(s) { - let count = 0; - let strLength = s.length; //get length - for (let i = 1; i < strLength; i++) { - let found = {}; - for (let j = 0; j + i <= strLength; j++) { - let substr = s.substr(j, i); //device string into substr - substr = substr.split('').sort().join(''); //sort it - if (found[substr]) { - count += found[substr]; //count total - found[substr]++; - } else { - found[substr] = 1; - } - } - } - - return count; -} - -console.log(sherlockAndAnagrams('cdcd')); diff --git a/Dictionaries and Hashmaps/two-strings.js b/Dictionaries and Hashmaps/two-strings.js deleted file mode 100644 index f9f7e3d..0000000 --- a/Dictionaries and Hashmaps/two-strings.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Two Strings - Difficulty: Easy - Score: 25 - Link: https://www.hackerrank.com/challenges/two-strings/ -*/ - -function twoStrings(s1, s2) { - m = new Map(); - for(let i=0; i { - if (grade < 38) { - return grade; - } - - let roundRemainder = grade % 5; - - if (roundRemainder === 0 || roundRemainder <= 2) { - return grade; - } - - return grade + (5 - roundRemainder); - }) - - return newGrades; - -} diff --git a/Numbers/kangaroo.js b/Numbers/kangaroo.js deleted file mode 100644 index 06495a1..0000000 --- a/Numbers/kangaroo.js +++ /dev/null @@ -1,56 +0,0 @@ -/* - Title: Kangaroo - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/kangaroo -*/ - -// Solution 1 -function kangaroo(x1, v1, x2, v2) { - if ((x2 > x1 && v2 > v1) || v1 === v2) { - return 'NO'; - } - - let x1Distance = x1; - let x2Distance = x2; - - while (x1Distance !== x2Distance) { - x1Distance += v1; - x2Distance += v2; - - if (x1Distance > x2Distance) { - return 'NO'; - } - } - - return 'YES'; - -} - - -//Solution 2 - - -/* - -(x1 - x2) % (v2 - v1) will solve the problem - -based on requirements - -x1 + (y * v1) = x2 + (y * v2) -x1 + (y * v1) - x2 = y * v2 -x1 - x2 = (y * v2) - (y * v1) -x1 - x2 = y * (v2 - v1) -x1 - x2 / v2 - v1 = y - -so we can find y with all 4 inputs if it is integer and greater than 0 then both will meet -*/ - -function kangaroo(x1, v1, x2, v2) { - if (x2 > x1 && v2 > v1) { - return 'NO' - } - - let steps = (x1 - x2) % (v2 - v1); - return (steps >= 0 && Number.isInteger(steps)) ? 'YES' : 'NO'; -} diff --git a/Numbers/plus-minus.js b/Numbers/plus-minus.js deleted file mode 100644 index b754238..0000000 --- a/Numbers/plus-minus.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Plus Minus - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/plus-minus/problem -*/ - -module.exports = function plusMinus(arr) { - let result = { - positive: 0, - negative: 0, - zero: 0, - } - - let arrLength = arr.length; - for (let index = 0; index < arrLength; index++){ - if (arr[index] === 0) { - ++result.zero; - } else if (arr[index] > 0) { - ++result.positive; - } else { - ++result.negative; - } - } - - return result; -} diff --git a/Sorting/bubble-sort.js b/Sorting/bubble-sort.js deleted file mode 100644 index d6d9c81..0000000 --- a/Sorting/bubble-sort.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Bubble Sort - Difficulty: Easy - Score: 30 - Link: https://www.hackerrank.com/challenges/ctci-bubble-sort/problem -*/ - -function countSwaps(a) { - let isSorted = false; - let counter = 0; - while (!isSorted) { - isSorted = true; - for (let i = 0; i < a.length - 1; i++) { - if (a[i] > a[i + 1]) { - [a[i], a[i + 1]] = [a[i + 1], a[i]]; - counter++; - isSorted = false; - } - } - } - - console.log(`Array is sorted in ${counter} swaps.`); - console.log(`First Element: ${a[0]}`); - console.log(`Last Element: ${a[a.length -1 ]}`); -} - -countSwaps([5, 2, 1]); diff --git a/Sorting/mark-and-toys.js b/Sorting/mark-and-toys.js deleted file mode 100644 index cabb6db..0000000 --- a/Sorting/mark-and-toys.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - Title: Mark and Toys - Difficulty: Easy - Score: 35 - Link: https://www.hackerrank.com/challenges/mark-and-toys/problem -*/ - -function maximumToys(prices, balance) { - prices = prices.sort((a, b) => a - b) - let toysBought = 0; - console.log(prices); - for (let i = 0; i < prices.length; i++){ - if (balance >= prices[i]) { - toysBought++; - balance -= prices[i]; - } else { - break; - } - } - return toysBought; - -} - -let arr = [33324560, 77661073, 31948330, 21522343, 97176507, 5724692, 24699815, 12079402, 6479353, 28430129, 42427721, 57127004, 26256001, 29446837, 65107604, 9809008, 65846182, 8470661, 13597655, 360]; -console.log(maximumToys(arr, 100000)); diff --git a/Sorting/mini-max-sum.js b/Sorting/mini-max-sum.js deleted file mode 100644 index 83bde36..0000000 --- a/Sorting/mini-max-sum.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - Title:Mini-Max Sum - Difficulty: Easy - Score: 10 - Link: https://www.hackerrank.com/challenges/mini-max-sum/problem -*/ - -function miniMaxSum(arr) { - let sum = [0, 0]; - arr = arr.sort((a, b) => { return a - b }); - for (let i = 0; i < 4; i++){ - sum[0] += arr[i]; - sum[1] += arr[arr.length - (i+1)]; - } - - console.log(sum[0], sum[1]); -} - -miniMaxSum(1, 2, 3, 4, 5); diff --git a/Strings/alternating-characters.js b/Strings/alternating-characters.js deleted file mode 100644 index b02de77..0000000 --- a/Strings/alternating-characters.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Alternating Characters - Difficulty: Easy - Score: 20 - Link: https://www.hackerrank.com/challenges/alternating-characters/problem -*/ - -function alternatingCharacters(string) { - let prev = ''; - let counter = 0; - for (let index = 0; index < string.length; index++) { - if(!prev){ - prev = string[index]; - continue; - } - - if(prev === string[index]){ - counter++; - } - - prev = string[index]; - } - - return counter; -} - -console.log(alternatingCharacters('BBBBB')); diff --git a/Strings/counting-valleys.js b/Strings/counting-valleys.js deleted file mode 100644 index dbb44ae..0000000 --- a/Strings/counting-valleys.js +++ /dev/null @@ -1,27 +0,0 @@ -/* - Title: Counting Valleys - Difficulty: Easy - Score: 15 - Link: https://www.hackerrank.com/challenges/counting-valleys -*/ - -function countingValleys(n, s) { - let currentValley = 0; - let count = 0; - for (let i = 0; i < n; i++) { - - if (s[i] === 'U') { - currentValley++; - } - if (s[i] === 'D') { - currentValley--; - } - - if (currentValley == 0 && s[i] === 'U') { - count++; - } - } - - return count; - -} diff --git a/Strings/hackerrank-in-string.js b/Strings/hackerrank-in-string.js deleted file mode 100644 index 1e8ebbd..0000000 --- a/Strings/hackerrank-in-string.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - Title: HackerRank in a String! - Difficulty: Easy - Score: 20 - Link: https://www.hackerrank.com/challenges/hackerrank-in-a-string -*/ - -module.exports = function hackerrankInString(s) { - const mask = Array.from("hackerrank"); - for (let i = 0; i < s.length; i++) { - const currentChar = s.charAt(i); - if (mask[0] === currentChar) { - mask.shift(); - } - } - return mask.length === 0 ? "YES" : "NO"; -}; diff --git a/Strings/making-anagrams.js b/Strings/making-anagrams.js deleted file mode 100644 index 4de1579..0000000 --- a/Strings/making-anagrams.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - Title: Making Anagrams - Difficulty: Easy - Score: 25/ - Link: https://www.hackerrank.com/challenges/ctci-making-anagrams/problem?h_r=internal-search -*/ - - -function makeAnagram(string1, string2){ - string1 = count(string1); - string2 = count(string2); - return findDiff(string1, string2); -} - -function count(a) { - let arr = a.split('').sort(); - let counter = {}; - arr.forEach(value => { - if (counter[value] !== undefined) { - counter[value] += 1; - } else { - counter[value] = 1; - } - }) - - return counter; -} - -function findDiff(arr1, arr2) { - let removeCount = 0; - - for (let key in arr1) { - if (!arr2[key]) { - removeCount += arr1[key]; - } else { - removeCount += Math.abs(arr2[key] - arr1[key]); - } - } - - for (let key in arr2) { - if (!arr1[key]) { - removeCount += arr2[key]; - } - } - return removeCount; -} - -let a = "fcrxzwscanmligyxyvym"; -let b = "jxwtrhvujlmrpdoqbisbwhmgpmeoke"; -console.log(makeAnagram(a,b )); diff --git a/Strings/repeated-string.js b/Strings/repeated-string.js deleted file mode 100644 index 4497df6..0000000 --- a/Strings/repeated-string.js +++ /dev/null @@ -1,16 +0,0 @@ -/* - Title: Repeated String - Difficulty: Easy - Score: 20 - Link: https://www.hackerrank.com/challenges/repeated-string -*/ - -module.exports = function repeatedString(s, n) { - let initialCount = s.split('a').length - 1; - let cocent = Math.floor(n / s.length); - let total = cocent * initialCount; - let remainder = n % s.length; - let remaindLetter = s.slice(0, remainder); - total += remaindLetter.split('a').length - 1; - return total; -} diff --git a/Strings/sherlock-and-the-valid-string.js b/Strings/sherlock-and-the-valid-string.js deleted file mode 100644 index 5846fcf..0000000 --- a/Strings/sherlock-and-the-valid-string.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - Title: Sherlock and Valid String - Difficulty: Medium - Score: 35 - Link: https://www.hackerrank.com/challenges/sherlock-and-valid-string -*/ - -function isValid(string) { - let counter = {}; - - let arr = string.split('').sort(); - let smallValue = 0; - let allowedTimes = 0; - arr.forEach(element => { - counter[element] = counter[element] === undefined ? 1 : counter[element]+1; - }); - - for(let key in counter){ - if(smallValue === 0){ - smallValue = counter[key]; - continue ; - } - - if(smallValue !== counter[key]){ - if(counter[key] === 1){ - allowedTimes += 1; - } else { - allowedTimes += Math.abs(smallValue - counter[key]); - } - } - - if(allowedTimes > 1){ - break; - } - } - - return allowedTimes > 1 ? false : true; -} - - -console.log(isValid("abcdefghhgfedecba")); diff --git a/Strings/strong-password.js b/Strings/strong-password.js deleted file mode 100644 index d3fd7c0..0000000 --- a/Strings/strong-password.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - Title: Strong Password - Difficulty: Easy - Score: 15 - Link: https://www.hackerrank.com/challenges/strong-password -*/ - -module.exports = function passwordStrengthMinimumNumber(n, password) { - const STRONG_LENGTH = 6; - const missingChars = STRONG_LENGTH - n; - let score = 0; - - const passwordAsArray = Array.from(password); - - const hasNumber = passwordAsArray.some(char => "0123456789".includes(char)); - const hasLowerCase = passwordAsArray.some(char => - "abcdefghijklmnopqrstuvwxyz".includes(char) - ); - const hasUpperCase = passwordAsArray.some(char => - "ABCDEFGHIJKLMNOPQRSTUVWXYZ".includes(char) - ); - const hasSpecialChar = passwordAsArray.some(char => - "!@#$%^&*()-+".includes(char) - ); - - if (!hasNumber) score++; - if (!hasLowerCase) score++; - if (!hasUpperCase) score++; - if (!hasSpecialChar) score++; - - return Math.max(score, missingChars); -}; diff --git a/Strings/time-conversion.js b/Strings/time-conversion.js deleted file mode 100644 index 6cabfbc..0000000 --- a/Strings/time-conversion.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - Title: Time Conversion - Difficulty: Easy - Score: 15 - Link: https://www.hackerrank.com/challenges/time-conversion/ -*/ - - -module.exports = function timeConversion(time) { - let cycle = time.substring(time.length - 2, time.length); - let timeWithoutCycle = time.substring(0, time.length - 2); - let timeArr = timeWithoutCycle.split(':'); - - if (cycle === 'AM') { - if (timeArr[0] != 12) { - return timeWithoutCycle; - } - - timeArr[0] = '00'; - return timeArr.join(':'); - } - - if (timeArr[0] != 12) { - timeArr[0] = parseInt(timeArr[0]) + 12; - } - - return timeArr.join(':') -} diff --git a/src/Arrays/2d-array-ds.js b/src/Arrays/2d-array-ds.js new file mode 100644 index 0000000..398f4fd --- /dev/null +++ b/src/Arrays/2d-array-ds.js @@ -0,0 +1,23 @@ +/* + Title: 2D Array - DS + Difficulty: Easy + Score: 15 + Link: https://www.hackerrank.com/challenges/2d-array/ +*/ +module.exports = function hourglassSum(arr) { + let sum = -1000; + let max = sum; + for (let i = 0; i < 4; i++) { + for (let j = 0; j < 4; j++) { + sum = arr[i][j] + + arr[i][j + 1] + + arr[i][j + 2] + + arr[i + 1][j + 1] + + arr[i + 2][j] + + arr[i + 2][j + 1] + + arr[i + 2][j + 2]; + if (sum > max) max = sum; + } + } + console.log(max); +}; diff --git a/src/Arrays/apple-and-orange.js b/src/Arrays/apple-and-orange.js new file mode 100644 index 0000000..3aea64e --- /dev/null +++ b/src/Arrays/apple-and-orange.js @@ -0,0 +1,24 @@ +/* + Title: Apple and Orange + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/apple-and-orange +*/ +function countFruitsFallInsideHome(fruits, startHome, EndHome) { + let counter = 0; + fruits.forEach((fruit) => { + if (fruit >= startHome && fruit <= EndHome) { + counter++; + } + return counter; + }); + + return console.log(counter); +} + +function countApplesAndOranges(s, t, a, b, apples, oranges) { + countFruitsFallInsideHome(apples, s - a, t - a); + countFruitsFallInsideHome(oranges, s - b, t - b); +} + +countApplesAndOranges(7, 11, 5, 15, 3, 2, [-2, 2, 1], [5 - 6]); diff --git a/src/Arrays/array-manipulation.js b/src/Arrays/array-manipulation.js new file mode 100644 index 0000000..a04ae2b --- /dev/null +++ b/src/Arrays/array-manipulation.js @@ -0,0 +1,18 @@ +/* + Title: Array Manipulation + Difficulty: Hard + Score: 60 + Link: https://www.hackerrank.com/challenges/crush/problem +*/ + +module.exports = function arrayManipulation(n, queries) { + const arr = Array(n).fill(0); + + for (let a = 0; a < queries.length; a++) { + for (let index = queries[a][0] - 1; index < queries[a][1]; index++) { + arr[index] += queries[a][2]; + } + } + + return Math.max(...arr); +}; diff --git a/src/Arrays/birthday-cake-candles.js b/src/Arrays/birthday-cake-candles.js new file mode 100644 index 0000000..674311e --- /dev/null +++ b/src/Arrays/birthday-cake-candles.js @@ -0,0 +1,21 @@ +/* + Title: Birthday Cake Candles + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/birthday-cake-candles/ +*/ + +module.exports = function birthdayCakeCandles(arr) { + let tallestCandle = 0; + let candlesCounter = 0; + for (let index = 0; index < arr.length; index++) { + if (arr[index] > tallestCandle) { + tallestCandle = arr[index]; + candlesCounter = 1; + } else if (arr[index] === tallestCandle) { + candlesCounter++; + } + } + + return candlesCounter; +}; diff --git a/src/Arrays/breaking-best-and-worst-records.js b/src/Arrays/breaking-best-and-worst-records.js new file mode 100644 index 0000000..9df7f33 --- /dev/null +++ b/src/Arrays/breaking-best-and-worst-records.js @@ -0,0 +1,40 @@ +/* + Title: Breaking the Records + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/breaking-best-and-worst-records +*/ + +function breakingRecords(scores) { + const counters = { + min: 0, + max: 0, + }; + + const values = { + min: 0, + max: 0, + }; + let isDefaultSet = false; + + scores.forEach((score) => { + if (!isDefaultSet) { + values.min = score; + values.max = score; + isDefaultSet = true; + return; + } + + if (score > values.max) { + values.max = score; + counters.max++; + } else if (score < values.min) { + values.min = score; + counters.min++; + } + }); + + return [counters.max, counters.min]; +} + +breakingRecords([10, 5, 20, 20, 4, 5, 2, 25, 1]); diff --git a/src/Arrays/compare-the-triplets.js b/src/Arrays/compare-the-triplets.js new file mode 100644 index 0000000..b542423 --- /dev/null +++ b/src/Arrays/compare-the-triplets.js @@ -0,0 +1,21 @@ +/* +Title: Compare the Triplets +Difficulty: Easy +Max Score: 10 +Link: https://www.hackerrank.com/challenges/compare-the-triplets/problem +*/ + +function compareTriplets(a, b) { + const points = [0, 0]; + for (let index = 0; index < a.length; index++) { + if (a[index] > b[index]) { + points[0] += 1; + } else if (a[index] < b[index]) { + points[1] += 1; + } + } + + return points; +} + +console.log(compareTriplets([5, 6, 7], [3, 6, 10])); diff --git a/src/Arrays/diagonal-difference.js b/src/Arrays/diagonal-difference.js new file mode 100644 index 0000000..a8290c3 --- /dev/null +++ b/src/Arrays/diagonal-difference.js @@ -0,0 +1,25 @@ +/* + Title:Diagonal Difference + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/diagonal-difference +*/ + +function diagonalDifference(arr) { + let leftToRight = 0; + let rightToLeft = 0; + let tmpLength = arr.length; + for (let i = 0; i < arr.length; i++) { + leftToRight += arr[i][i]; + rightToLeft += arr[i][--tmpLength]; + } + + return Math.abs(leftToRight - rightToLeft); +} + +const input = [ + [11, 2, 4], + [4, 5, 6], + [10, 8, -12], +]; +console.log(diagonalDifference(input)); diff --git a/src/Arrays/jumping-on-the-clouds.js b/src/Arrays/jumping-on-the-clouds.js new file mode 100644 index 0000000..0488a2f --- /dev/null +++ b/src/Arrays/jumping-on-the-clouds.js @@ -0,0 +1,23 @@ +/* + Title: Jumping on the Clouds + Difficulty: Easy + Score: 20 + Link: https://www.hackerrank.com/challenges/jumping-on-the-clouds/ +*/ + +module.exports = function jumpingOnClouds(c) { + let jump = 0; + for (let i = 0; i < c.length;) { + if (c[i + 2] === 0) { + jump++; + i += 2; + } else if (c[i + 1] === 0) { + jump++; + i += 1; + } else { + i++; + } + } + + return jump; +}; diff --git a/Arrays/left-rotation.js b/src/Arrays/left-rotation.js similarity index 58% rename from Arrays/left-rotation.js rename to src/Arrays/left-rotation.js index 17ed7de..7881ba7 100644 --- a/Arrays/left-rotation.js +++ b/src/Arrays/left-rotation.js @@ -6,14 +6,13 @@ */ module.exports = function rotLeft(a, d) { + if (!a.length) { + return []; + } - if(!a.length){ - return [] - } + for (let i = 0; i < d; i++) { + a.push(a.shift()); + } - for (let i = 0; i < d; i++) { - a.push(a.shift()); - } - - return a; -} + return a; +}; diff --git a/src/Arrays/minimum-swaps-2.js b/src/Arrays/minimum-swaps-2.js new file mode 100644 index 0000000..95c93fc --- /dev/null +++ b/src/Arrays/minimum-swaps-2.js @@ -0,0 +1,25 @@ +/* + Title: Minimum Swaps 2 + Difficulty: Medium + Score: 40 + Link: https://www.hackerrank.com/challenges/minimum-swaps-2 +*/ + +module.exports = function minimumSwaps(arr) { + const origArray = arr.slice(0); + const sortedArray = arr.sort((a, b) => a - b); + let numberOfSwap = 0; + for (let i = 0; i < origArray.length; i++) { + if (origArray[i] !== sortedArray[i]) { + for (let j = i + 1; j < origArray.length; j++) { + if (origArray[j] === sortedArray[i]) { + [origArray[i], origArray[j]] = [origArray[j], origArray[i]]; + numberOfSwap++; + break; + } + } + } + } + + return numberOfSwap; +}; diff --git a/src/Arrays/newyear-chaos.js b/src/Arrays/newyear-chaos.js new file mode 100644 index 0000000..e9a022f --- /dev/null +++ b/src/Arrays/newyear-chaos.js @@ -0,0 +1,25 @@ +/* + Title: New Year Chaos + Difficulty: Medium + Score: 40 + Link: https://www.hackerrank.com/challenges/new-year-chaos/ +*/ + +module.exports = function minimumBribes(q) { + let swaps = 0; + let min = q.length; + for (let i = q.length - 1; i >= 0; i--) { + if (q[i] - i > 3) { + return 'Too chaotic'; + } + if (q[i] > i + 1) { + swaps += q[i] - (i + 1); + } else if (min > q[i]) { + min = q[i]; + } else if (q[i] !== min) { + swaps++; + } + } + + return swaps; +}; diff --git a/src/Arrays/sock-merchant.js b/src/Arrays/sock-merchant.js new file mode 100644 index 0000000..c75cf50 --- /dev/null +++ b/src/Arrays/sock-merchant.js @@ -0,0 +1,25 @@ +/* + Title: Sock Merchant + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/sock-merchant +*/ + +module.exports = function sockMerchant(n, ar) { + const tmpArr = []; + let counter = 0; + ar.map((value) => { + if (tmpArr.includes(value)) { + const index = tmpArr.indexOf(value); + if (index > -1) { + tmpArr.splice(index, 1); + } + counter++; + } else { + tmpArr.push(value); + } + return value; + }); + + return counter; +}; diff --git a/src/Data Structures/cycle-detection.js b/src/Data Structures/cycle-detection.js new file mode 100644 index 0000000..ed53bab --- /dev/null +++ b/src/Data Structures/cycle-detection.js @@ -0,0 +1,38 @@ +/* + Title: Cycle Detection + Difficulty: Medium + Score: 5 + Link: https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem +*/ + +function hasCycle(head) { + if (head === null || head.next === null || head.next.next === null) { + return false; + } // edge cases + let hare = head; + let tortoise = head; + do { + hare = hare.next.next; // 'hare' moves twice per tick + tortoise = tortoise.next; // 'tortoise' moves once per tick + } while (hare !== tortoise && hare.next !== null && hare.next.next !== null); + return hare === tortoise; +} + +// NODE CLASS + +class Node { + constructor(data) { + this.data = data; + this.next = null; + } + + insert(data) { + this.next = new Node(data); + return this.next; // for chaining + } +} + +module.exports = { + hasCycle, + Node, +}; diff --git a/src/Data Structures/reverse-doubly-linked-list.js b/src/Data Structures/reverse-doubly-linked-list.js new file mode 100644 index 0000000..71a4f23 --- /dev/null +++ b/src/Data Structures/reverse-doubly-linked-list.js @@ -0,0 +1,52 @@ +/* + Title: Reverse a doubly linked list + Difficulty: Easy + Score: 5 + Link: https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem +*/ +class DoublyLinkedListNode { + constructor(nodeData) { + this.data = nodeData; + this.next = null; + this.prev = null; + } +} + +class DoublyLinkedList { + constructor() { + this.head = null; + this.tail = null; + } + + insertNode(nodeData) { + const node = new DoublyLinkedListNode(nodeData); + + if (this.head == null) { + this.head = node; + } else { + this.tail.next = node; + node.prev = this.tail; + } + + this.tail = node; + } +} + +function reverse(head) { + while (head.next) { + head = head.next; + } + + const newList = new DoublyLinkedList(); + + while (head) { + newList.insertNode(head.data); + head = head.prev; + } + return newList.head; +} +module.exports = { + reverse, + DoublyLinkedList, + DoublyLinkedListNode, +}; diff --git a/src/Dictionaries and Hashmaps/count-triplet.js b/src/Dictionaries and Hashmaps/count-triplet.js new file mode 100644 index 0000000..38656ad --- /dev/null +++ b/src/Dictionaries and Hashmaps/count-triplet.js @@ -0,0 +1,34 @@ +/* + Title: Count Triplets + Difficulty: Medium + Score: 35 + Link: https://www.hackerrank.com/challenges/count-triplets-1/ +*/ + +function countTriplets(arr, r) { + const second = {}; + const third = {}; + let count = 0; + for (let index = 0; index < arr.length; index++) { + const val = arr[index]; + + // If value found on third then add it to count + if (third[val]) { + count += third[val]; + } + + // If value found on second , convey it to third + + if (second[val]) { + third[val * r] = third[val * r] + second[val] || second[val]; + } + + // add current element into second value + + second[val * r] = second[val * r] + 1 || 1; + } + + return count; +} + +console.log(countTriplets([1, 2, 2, 4], 2)); diff --git a/src/Dictionaries and Hashmaps/custom-hashmap.js b/src/Dictionaries and Hashmaps/custom-hashmap.js new file mode 100644 index 0000000..f7a2fe6 --- /dev/null +++ b/src/Dictionaries and Hashmaps/custom-hashmap.js @@ -0,0 +1,35 @@ +/* + Title: Custom Hashmap + Difficulty: - + Score: - + Link: - +*/ +class customHashMap { + constructor() { + this.list = []; + } + + get(x) { + return this.list[this.hashCode(x)]; + } + + set(x, y) { + this.list[this.hashCode(x)] = y; + } + + static hashCode(str) { + let hash = 0; + if (str.length === 0) return hash; + for (let i = 0; i < str.length; i++) { + const char = str.charCodeAt(i); + hash = (hash << 5) - hash + char; + hash &= hash; // Convert to 32bit integer + } + return hash; + } +} + +const m = customHashMap(); + +m.set('hello', 'world'); +console.log(m.get('hello')); diff --git a/src/Dictionaries and Hashmaps/frequency-queries.js b/src/Dictionaries and Hashmaps/frequency-queries.js new file mode 100644 index 0000000..40ccb6b --- /dev/null +++ b/src/Dictionaries and Hashmaps/frequency-queries.js @@ -0,0 +1,45 @@ +/* + Title: Frequency Queries + Difficulty: Medium + Score: 40 + Link: https://www.hackerrank.com/challenges/frequency-queries/ +*/ + +function freqQuery(arr) { + const result = []; + const hash = {}; + const freq = []; + + for (let i = 0; i < arr.length; i += 1) { + const [action, value] = arr[i]; + const initValue = hash[value] || 0; + + if (action === 1) { + hash[value] = initValue + 1; + freq[initValue] = (freq[initValue] || 0) - 1; + freq[initValue + 1] = (freq[initValue + 1] || 0) + 1; + } + + if (action === 2 && initValue > 0) { + hash[value] = initValue - 1; + freq[initValue - 1] += 1; + freq[initValue] -= 1; + } + + if (action === 3) result.push(freq[value] > 0 ? 1 : 0); + } + + return result; +} + +const input = [ + [1, 5], + [1, 6], + [3, 2], + [1, 10], + [1, 10], + [1, 6], + [2, 5], + [3, 2], +]; +console.log(freqQuery(input)); diff --git a/src/Dictionaries and Hashmaps/game-of-thrones.js b/src/Dictionaries and Hashmaps/game-of-thrones.js new file mode 100644 index 0000000..d516d12 --- /dev/null +++ b/src/Dictionaries and Hashmaps/game-of-thrones.js @@ -0,0 +1,19 @@ +/* + Title: Game Of Thrones - I + Difficulty: Easy + Score: 30 + Link: https://www.hackerrank.com/challenges/game-of-thrones/problem +*/ + +function gameOfThrones(s) { + const frequencies = {}; + for (const char of s) frequencies[char] = frequencies[char] + 1 || 1; + const oddFrequencies = Object.values(frequencies).filter( + (frequency) => frequency % 2, + ); + return oddFrequencies.length <= 1 ? 'YES' : 'NO'; +} + +console.log(gameOfThrones('aaabbbb')); // YES +console.log(gameOfThrones('cdefghmnopqrstuvw')); // NO +console.log(gameOfThrones('cdcdcdcdeeeef')); // YES diff --git a/src/Dictionaries and Hashmaps/ransom-note.js b/src/Dictionaries and Hashmaps/ransom-note.js new file mode 100644 index 0000000..2268c69 --- /dev/null +++ b/src/Dictionaries and Hashmaps/ransom-note.js @@ -0,0 +1,54 @@ +/* + Title: Hash Tables: Ransom Note + Difficulty: Easy + Score: 25 + Link: https://www.hackerrank.com/challenges/ctci-ransom-note/ +*/ + +class ExtendedMap { + constructor() { + this.map = new Map(); + } + + get(x) { + let value = this.map.get(x); + if (value === 1) { + this.map.delete(x); + return true; + } + + return this.map.set(x, --value); + } + + set(x, y) { + if (this.map.get(x)) { + return this.map.set(x, this.map.get(x) + 1); + } + + return this.map.set(x, y); + } +} + +function checkMagazine(magazine, note) { + const m = new ExtendedMap(); + + for (let i = 0; i < magazine.length; i++) { + m.set(magazine[i], 1); + } + + for (let i = 0; i < note.length; i++) { + if (!m.get(note[i])) { + console.log('No'); + return; + } + } + + console.log('Yes'); +} + +const magazine = ['give', 'me', 'one', 'grand', 'today', 'night']; +const note = ['give', 'one', 'grand', 'today']; + +console.time('Time Taken'); +checkMagazine(magazine, note); +console.timeEnd('Time Taken'); diff --git a/src/Dictionaries and Hashmaps/sherlock-and-anagrams.js b/src/Dictionaries and Hashmaps/sherlock-and-anagrams.js new file mode 100644 index 0000000..8b655d9 --- /dev/null +++ b/src/Dictionaries and Hashmaps/sherlock-and-anagrams.js @@ -0,0 +1,27 @@ +/* + Title: Sherlock and Anagrams + Difficulty: Medium + Score: 50 + Link: https://www.hackerrank.com/challenges/sherlock-and-anagrams/ +*/ +function sherlockAndAnagrams(s) { + let count = 0; + const strLength = s.length; // get length + for (let i = 1; i < strLength; i++) { + const found = {}; + for (let j = 0; j + i <= strLength; j++) { + let substr = s.substr(j, i); // device string into substr + substr = substr.split('').sort().join(''); // sort it + if (found[substr]) { + count += found[substr]; // count total + found[substr]++; + } else { + found[substr] = 1; + } + } + } + + return count; +} + +console.log(sherlockAndAnagrams('cdcd')); diff --git a/src/Dictionaries and Hashmaps/two-strings.js b/src/Dictionaries and Hashmaps/two-strings.js new file mode 100644 index 0000000..cc1845d --- /dev/null +++ b/src/Dictionaries and Hashmaps/two-strings.js @@ -0,0 +1,26 @@ +/* + Title: Two Strings + Difficulty: Easy + Score: 25 + Link: https://www.hackerrank.com/challenges/two-strings/ +*/ + +function twoStrings(s1, s2) { + const m = new Map(); + for (let i = 0; i < s1.length; i++) { + m.set(s1[i], 1); + } + + for (let i = 0; i < s2.length; i++) { + if (m.get(s2[i])) { + return 'YES'; + } + } + + return 'NO'; +} + +console.time('find'); +console.log(twoStrings('hello', 'world')); +console.log(twoStrings('hi', 'world')); +console.timeEnd('find'); diff --git a/src/Numbers/grading.js b/src/Numbers/grading.js new file mode 100644 index 0000000..e03efc3 --- /dev/null +++ b/src/Numbers/grading.js @@ -0,0 +1,24 @@ +/* + Title: Grading + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/grading +*/ + +module.exports = function gradingStudents(grades) { + const newGrades = grades.map((grade) => { + if (grade < 38) { + return grade; + } + + const roundRemainder = grade % 5; + + if (roundRemainder === 0 || roundRemainder <= 2) { + return grade; + } + + return grade + (5 - roundRemainder); + }); + + return newGrades; +}; diff --git a/src/Numbers/kangaroo.js b/src/Numbers/kangaroo.js new file mode 100644 index 0000000..780d34a --- /dev/null +++ b/src/Numbers/kangaroo.js @@ -0,0 +1,55 @@ +/* + Title: Kangaroo + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/kangaroo +*/ + +// Solution 1 +function kangaroo(x1, v1, x2, v2) { + if ((x2 > x1 && v2 > v1) || v1 === v2) { + return 'NO'; + } + + let x1Distance = x1; + let x2Distance = x2; + + while (x1Distance !== x2Distance) { + x1Distance += v1; + x2Distance += v2; + + if (x1Distance > x2Distance) { + return 'NO'; + } + } + + return 'YES'; +} + +// Solution 2 + +/* + +(x1 - x2) % (v2 - v1) will solve the problem + +based on requirements + +x1 + (y * v1) = x2 + (y * v2) +x1 + (y * v1) - x2 = y * v2 +x1 - x2 = (y * v2) - (y * v1) +x1 - x2 = y * (v2 - v1) +x1 - x2 / v2 - v1 = y + +so we can find y with all 4 inputs if it is integer and greater than 0 then both will meet +*/ + +function kangarooSolution2(x1, v1, x2, v2) { + if (x2 > x1 && v2 > v1) { + return 'NO'; + } + + const steps = (x1 - x2) % (v2 - v1); + return steps >= 0 && Number.isInteger(steps) ? 'YES' : 'NO'; +} + +module.exports = [kangaroo, kangarooSolution2]; diff --git a/src/Numbers/plus-minus.js b/src/Numbers/plus-minus.js new file mode 100644 index 0000000..164be6c --- /dev/null +++ b/src/Numbers/plus-minus.js @@ -0,0 +1,27 @@ +/* + Title: Plus Minus + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/plus-minus/problem +*/ + +module.exports = function plusMinus(arr) { + const result = { + positive: 0, + negative: 0, + zero: 0, + }; + + const arrLength = arr.length; + for (let index = 0; index < arrLength; index++) { + if (arr[index] === 0) { + ++result.zero; + } else if (arr[index] > 0) { + ++result.positive; + } else { + ++result.negative; + } + } + + return result; +}; diff --git a/Numbers/staircase.js b/src/Numbers/staircase.js similarity index 53% rename from Numbers/staircase.js rename to src/Numbers/staircase.js index f09ff35..6a05c07 100644 --- a/Numbers/staircase.js +++ b/src/Numbers/staircase.js @@ -6,10 +6,10 @@ */ function staircase(n) { - let tmp = n; - for (let index = 0; index < n; index++){ - console.log(' '.repeat(--tmp) + '#'.repeat(index + 1 )); - } + let tmp = n; + for (let index = 0; index < n; index++) { + console.log(' '.repeat(--tmp) + '#'.repeat(index + 1)); + } } staircase(6); diff --git a/src/Sorting/bubble-sort.js b/src/Sorting/bubble-sort.js new file mode 100644 index 0000000..794c249 --- /dev/null +++ b/src/Sorting/bubble-sort.js @@ -0,0 +1,27 @@ +/* + Title: Bubble Sort + Difficulty: Easy + Score: 30 + Link: https://www.hackerrank.com/challenges/ctci-bubble-sort/problem +*/ + +function countSwaps(a) { + let isSorted = false; + let counter = 0; + while (!isSorted) { + isSorted = true; + for (let i = 0; i < a.length - 1; i++) { + if (a[i] > a[i + 1]) { + [a[i], a[i + 1]] = [a[i + 1], a[i]]; + counter++; + isSorted = false; + } + } + } + + console.log(`Array is sorted in ${counter} swaps.`); + console.log(`First Element: ${a[0]}`); + console.log(`Last Element: ${a[a.length - 1]}`); +} + +countSwaps([5, 2, 1]); diff --git a/src/Sorting/mark-and-toys.js b/src/Sorting/mark-and-toys.js new file mode 100644 index 0000000..80dcd98 --- /dev/null +++ b/src/Sorting/mark-and-toys.js @@ -0,0 +1,28 @@ +/* + Title: Mark and Toys + Difficulty: Easy + Score: 35 + Link: https://www.hackerrank.com/challenges/mark-and-toys/problem +*/ + +function maximumToys(prices, balance) { + prices = prices.sort((a, b) => a - b); + let toysBought = 0; + console.log(prices); + for (let i = 0; i < prices.length; i++) { + if (balance >= prices[i]) { + toysBought++; + balance -= prices[i]; + } else { + break; + } + } + return toysBought; +} + +const arr = [ + 33324560, 77661073, 31948330, 21522343, 97176507, 5724692, 24699815, 12079402, + 6479353, 28430129, 42427721, 57127004, 26256001, 29446837, 65107604, 9809008, + 65846182, 8470661, 13597655, 360, +]; +console.log(maximumToys(arr, 100000)); diff --git a/src/Sorting/mini-max-sum.js b/src/Sorting/mini-max-sum.js new file mode 100644 index 0000000..79d6224 --- /dev/null +++ b/src/Sorting/mini-max-sum.js @@ -0,0 +1,19 @@ +/* + Title:Mini-Max Sum + Difficulty: Easy + Score: 10 + Link: https://www.hackerrank.com/challenges/mini-max-sum/problem +*/ + +function miniMaxSum(arr) { + const sum = [0, 0]; + arr = arr.sort((a, b) => a - b); + for (let i = 0; i < 4; i++) { + sum[0] += arr[i]; + sum[1] += arr[arr.length - (i + 1)]; + } + + console.log(sum[0], sum[1]); +} + +miniMaxSum(1, 2, 3, 4, 5); diff --git a/src/Strings/alternating-characters.js b/src/Strings/alternating-characters.js new file mode 100644 index 0000000..3fbcb1a --- /dev/null +++ b/src/Strings/alternating-characters.js @@ -0,0 +1,27 @@ +/* + Title: Alternating Characters + Difficulty: Easy + Score: 20 + Link: https://www.hackerrank.com/challenges/alternating-characters/problem +*/ + +function alternatingCharacters(string) { + let prev = ''; + let counter = 0; + for (let index = 0; index < string.length; index++) { + if (!prev) { + prev = string[index]; + continue; + } + + if (prev === string[index]) { + counter++; + } + + prev = string[index]; + } + + return counter; +} + +console.log(alternatingCharacters('BBBBB')); diff --git a/src/Strings/counting-valleys.js b/src/Strings/counting-valleys.js new file mode 100644 index 0000000..b8c21eb --- /dev/null +++ b/src/Strings/counting-valleys.js @@ -0,0 +1,25 @@ +/* + Title: Counting Valleys + Difficulty: Easy + Score: 15 + Link: https://www.hackerrank.com/challenges/counting-valleys +*/ + +module.exports = function countingValleys(n, s) { + let currentValley = 0; + let count = 0; + for (let i = 0; i < n; i++) { + if (s[i] === 'U') { + currentValley++; + } + if (s[i] === 'D') { + currentValley--; + } + + if (currentValley === 0 && s[i] === 'U') { + count++; + } + } + + return count; +}; diff --git a/src/Strings/hackerrank-in-string.js b/src/Strings/hackerrank-in-string.js new file mode 100644 index 0000000..2564590 --- /dev/null +++ b/src/Strings/hackerrank-in-string.js @@ -0,0 +1,17 @@ +/* + Title: HackerRank in a String! + Difficulty: Easy + Score: 20 + Link: https://www.hackerrank.com/challenges/hackerrank-in-a-string +*/ + +module.exports = function hackerrankInString(s) { + const mask = Array.from('hackerrank'); + for (let i = 0; i < s.length; i++) { + const currentChar = s.charAt(i); + if (mask[0] === currentChar) { + mask.shift(); + } + } + return mask.length === 0 ? 'YES' : 'NO'; +}; diff --git a/src/Strings/making-anagrams.js b/src/Strings/making-anagrams.js new file mode 100644 index 0000000..3fb1d88 --- /dev/null +++ b/src/Strings/making-anagrams.js @@ -0,0 +1,47 @@ +/* + Title: Making Anagrams + Difficulty: Easy + Score: 25/ + Link: https://www.hackerrank.com/challenges/ctci-making-anagrams/problem?h_r=internal-search +*/ +function count(a) { + const arr = a.split('').sort(); + const counter = {}; + arr.forEach((value) => { + if (counter[value] !== undefined) { + counter[value] += 1; + } else { + counter[value] = 1; + } + }); + + return counter; +} + +function findDiff(arr1, arr2) { + let removeCount = 0; + + for (const key in arr1) { + if (!arr2[key]) { + removeCount += arr1[key]; + } else { + removeCount += Math.abs(arr2[key] - arr1[key]); + } + } + + for (const key in arr2) { + if (!arr1[key]) { + removeCount += arr2[key]; + } + } + return removeCount; +} + +function makeAnagram(string1, string2) { + string1 = count(string1); + string2 = count(string2); + return findDiff(string1, string2); +} +const a = 'fcrxzwscanmligyxyvym'; +const b = 'jxwtrhvujlmrpdoqbisbwhmgpmeoke'; +console.log(makeAnagram(a, b)); diff --git a/src/Strings/repeated-string.js b/src/Strings/repeated-string.js new file mode 100644 index 0000000..6e71159 --- /dev/null +++ b/src/Strings/repeated-string.js @@ -0,0 +1,16 @@ +/* + Title: Repeated String + Difficulty: Easy + Score: 20 + Link: https://www.hackerrank.com/challenges/repeated-string +*/ + +module.exports = function repeatedString(s, n) { + const initialCount = s.split('a').length - 1; + const cocent = Math.floor(n / s.length); + let total = cocent * initialCount; + const remainder = n % s.length; + const remaindLetter = s.slice(0, remainder); + total += remaindLetter.split('a').length - 1; + return total; +}; diff --git a/src/Strings/sherlock-and-the-valid-string.js b/src/Strings/sherlock-and-the-valid-string.js new file mode 100644 index 0000000..6b4a1cc --- /dev/null +++ b/src/Strings/sherlock-and-the-valid-string.js @@ -0,0 +1,40 @@ +/* + Title: Sherlock and Valid String + Difficulty: Medium + Score: 35 + Link: https://www.hackerrank.com/challenges/sherlock-and-valid-string +*/ + +function isValid(string) { + const counter = {}; + + const arr = string.split('').sort(); + let smallValue = 0; + let allowedTimes = 0; + arr.forEach((element) => { + counter[element] = counter[element] === undefined ? 1 : counter[element] + 1; + }); + + for (const key in counter) { + if (smallValue === 0) { + smallValue = counter[key]; + continue; + } + + if (smallValue !== counter[key]) { + if (counter[key] === 1) { + allowedTimes += 1; + } else { + allowedTimes += Math.abs(smallValue - counter[key]); + } + } + + if (allowedTimes > 1) { + break; + } + } + + return !(allowedTimes > 1); +} + +console.log(isValid('abcdefghhgfedecba')); diff --git a/src/Strings/strong-password.js b/src/Strings/strong-password.js new file mode 100644 index 0000000..fa54fb6 --- /dev/null +++ b/src/Strings/strong-password.js @@ -0,0 +1,26 @@ +/* + Title: Strong Password + Difficulty: Easy + Score: 15 + Link: https://www.hackerrank.com/challenges/strong-password +*/ + +module.exports = function passwordStrengthMinimumNumber(n, password) { + const STRONG_LENGTH = 6; + const missingChars = STRONG_LENGTH - n; + let score = 0; + + const passwordAsArray = Array.from(password); + + const hasNumber = passwordAsArray.some((char) => '0123456789'.includes(char)); + const hasLowerCase = passwordAsArray.some((char) => 'abcdefghijklmnopqrstuvwxyz'.includes(char)); + const hasUpperCase = passwordAsArray.some((char) => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.includes(char)); + const hasSpecialChar = passwordAsArray.some((char) => '!@#$%^&*()-+'.includes(char)); + + if (!hasNumber) score++; + if (!hasLowerCase) score++; + if (!hasUpperCase) score++; + if (!hasSpecialChar) score++; + + return Math.max(score, missingChars); +}; diff --git a/src/Strings/time-conversion.js b/src/Strings/time-conversion.js new file mode 100644 index 0000000..dc86243 --- /dev/null +++ b/src/Strings/time-conversion.js @@ -0,0 +1,27 @@ +/* + Title: Time Conversion + Difficulty: Easy + Score: 15 + Link: https://www.hackerrank.com/challenges/time-conversion/ +*/ + +module.exports = function timeConversion(time) { + const cycle = time.substring(time.length - 2, time.length); + const timeWithoutCycle = time.substring(0, time.length - 2); + const timeArr = timeWithoutCycle.split(':'); + + if (cycle === 'AM') { + if (timeArr[0] !== '12') { + return timeWithoutCycle; + } + + timeArr[0] = '00'; + return timeArr.join(':'); + } + + if (timeArr[0] !== '12') { + timeArr[0] = parseInt(timeArr[0], 10) + 12; + } + + return timeArr.join(':'); +}; diff --git a/test/Arrays/birthday-cake-candles.test.js b/test/Arrays/birthday-cake-candles.test.js index ba2fb31..506cc68 100644 --- a/test/Arrays/birthday-cake-candles.test.js +++ b/test/Arrays/birthday-cake-candles.test.js @@ -1,28 +1,28 @@ -const birthdayCakeCandles = require("../../Arrays/birthday-cake-candles"); -const { describe, it } = require("mocha"); -const { expect } = require("chai"); +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const birthdayCakeCandles = require('../../src/Arrays/birthday-cake-candles'); -describe("Birthday Cake Candles", () => { - it("returns 0 for empty array", () => { - expect(birthdayCakeCandles([])).to.equal(0); - }); +describe('Birthday Cake Candles', () => { + it('returns 0 for empty array', () => { + expect(birthdayCakeCandles([])).to.equal(0); + }); - it("returns 1 if there is only one candle", () => { - expect(birthdayCakeCandles([1])).to.equal(1); - expect(birthdayCakeCandles([2])).to.equal(1); - expect(birthdayCakeCandles([3])).to.equal(1); - }); + it('returns 1 if there is only one candle', () => { + expect(birthdayCakeCandles([1])).to.equal(1); + expect(birthdayCakeCandles([2])).to.equal(1); + expect(birthdayCakeCandles([3])).to.equal(1); + }); - it("returns 2 if there are two candles with the same height", () => { - expect(birthdayCakeCandles([1, 1])).to.equal(2); - expect(birthdayCakeCandles([2, 2])).to.equal(2); - expect(birthdayCakeCandles([3, 3])).to.equal(2); - }); + it('returns 2 if there are two candles with the same height', () => { + expect(birthdayCakeCandles([1, 1])).to.equal(2); + expect(birthdayCakeCandles([2, 2])).to.equal(2); + expect(birthdayCakeCandles([3, 3])).to.equal(2); + }); - it("returns correct amount for array with different sized candles", () => { - expect(birthdayCakeCandles([3, 2, 1, 3])).to.equal(2); - expect(birthdayCakeCandles([3, 2, 1, 3, 4, 5, 4])).to.equal(1); - expect(birthdayCakeCandles([3, 2, 1, 1, 2, 2, 4, 4, 4])).to.equal(3); - expect(birthdayCakeCandles([3, 2, 1])).to.equal(1); - }); + it('returns correct amount for array with different sized candles', () => { + expect(birthdayCakeCandles([3, 2, 1, 3])).to.equal(2); + expect(birthdayCakeCandles([3, 2, 1, 3, 4, 5, 4])).to.equal(1); + expect(birthdayCakeCandles([3, 2, 1, 1, 2, 2, 4, 4, 4])).to.equal(3); + expect(birthdayCakeCandles([3, 2, 1])).to.equal(1); + }); }); diff --git a/test/Arrays/left-rotation.test.js b/test/Arrays/left-rotation.test.js index f1dd855..0965ab2 100644 --- a/test/Arrays/left-rotation.test.js +++ b/test/Arrays/left-rotation.test.js @@ -1,22 +1,20 @@ -const leftRotation = require('../../Arrays/left-rotation') -const { describe, it } = require('mocha') -const { expect } = require('chai') +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const leftRotation = require('../../src/Arrays/left-rotation'); describe('Left Rotation', () => { + it('should work for any empty array', () => { + expect(leftRotation([], 10)).to.deep.equal([]); + }); - it('should work for any empty array', () => { - expect(leftRotation([], 10)).to.deep.equal([]) - }) + it('should work for a 1 element array', () => { + const original = [1]; + expect(leftRotation(original, 10)).to.deep.equal(original); + }); - it('should work for a 1 element array', () => { - const original = [1] - expect(leftRotation(original, 10)).to.deep.equal(original) - }) - - it('should work for multiple elements', () => { - const original = "ABCDEF".split('') - const expected = "DEFABC".split('') - expect(leftRotation(original, 3)).to.deep.equal(expected) - }) - -}) + it('should work for multiple elements', () => { + const original = 'ABCDEF'.split(''); + const expected = 'DEFABC'.split(''); + expect(leftRotation(original, 3)).to.deep.equal(expected); + }); +}); diff --git a/test/Data Structures/cycle-detection.test.js b/test/Data Structures/cycle-detection.test.js index a465729..d6dd036 100644 --- a/test/Data Structures/cycle-detection.test.js +++ b/test/Data Structures/cycle-detection.test.js @@ -1,54 +1,52 @@ -const { hasCycle, Node } = require('../../Data Structures/cycle-detection'); const { describe, it } = require('mocha'); const { expect } = require('chai'); +const { hasCycle, Node } = require('../../src/Data Structures/cycle-detection'); describe('Cycle Detection', () => { - it('should detect a cycle in a regular linked list', () => { - const test1 = new Node(1); - const tail1 = test1.insert(2) // tail1 === Node #8 - .insert(3) - .insert(4) - .insert(5) - .insert(6) - .insert(7) - .insert(8); - const connection1 = test1.next.next.next; // connection1 === Node #4 - tail1.next = connection1; - expect(hasCycle(test1)).to.equal(true); - }); + it('should detect a cycle in a regular linked list', () => { + const test1 = new Node(1); + const tail1 = test1 + .insert(2) // tail1 === Node #8 + .insert(3) + .insert(4) + .insert(5) + .insert(6) + .insert(7) + .insert(8); + const connection1 = test1.next.next.next; // connection1 === Node #4 + tail1.next = connection1; + expect(hasCycle(test1)).to.equal(true); + }); - it('should return false when the end of the list points to null', () => { - const test2 = new Node(1); - test2.insert(2) - .insert(3) - .insert(4) - .insert(5) - .insert(6) - .insert(7) - .insert(8); - expect(hasCycle(test2)).to.equal(false); - }); + it('should return false when the end of the list points to null', () => { + const test2 = new Node(1); + test2.insert(2).insert(3).insert(4).insert(5) + .insert(6) + .insert(7) + .insert(8); + expect(hasCycle(test2)).to.equal(false); + }); - it('should return false when head is null instead of a node', () => { - const test3 = null; - expect(hasCycle(test3)).to.equal(false); - }); + it('should return false when head is null instead of a node', () => { + const test3 = null; + expect(hasCycle(test3)).to.equal(false); + }); - it('should return false when the linked list only has one node that points to null', () => { - const test4 = new Node(1); - expect(hasCycle(test4)).to.equal(false); - }); + it('should return false when the linked list only has one node that points to null', () => { + const test4 = new Node(1); + expect(hasCycle(test4)).to.equal(false); + }); - it('should return false when the linked list only has two nodes', () => { - const test5 = new Node(1); - test5.insert(2); - expect(hasCycle(test5)).to.equal(false); - }); + it('should return false when the linked list only has two nodes', () => { + const test5 = new Node(1); + test5.insert(2); + expect(hasCycle(test5)).to.equal(false); + }); - it('should return false when the linked list only has two nodes but the tail links to head', () => { - const test6 = new Node(1); - const tail6 = test6.insert(2); // tail6 === Node #2 - tail6.next = test6; - expect(hasCycle(test6)).to.equal(true); - }); -}) + it('should return false when the linked list only has two nodes but the tail links to head', () => { + const test6 = new Node(1); + const tail6 = test6.insert(2); // tail6 === Node #2 + tail6.next = test6; + expect(hasCycle(test6)).to.equal(true); + }); +}); diff --git a/test/Data Structures/reverse-doubly-linked-list.test.js b/test/Data Structures/reverse-doubly-linked-list.test.js index f23567b..01f5fdb 100644 --- a/test/Data Structures/reverse-doubly-linked-list.test.js +++ b/test/Data Structures/reverse-doubly-linked-list.test.js @@ -1,29 +1,32 @@ -const { reverse, DoublyLinkedListNode, DoublyLinkedList } = require('../../Data Structures/reverse-doubly-linked-list'); -const { describe, it } = require('mocha') -const { expect } = require('chai') - +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const { + reverse, + DoublyLinkedListNode, + DoublyLinkedList, +} = require('../../src/Data Structures/reverse-doubly-linked-list'); describe('Reverse Doubly Linked List', () => { - it('should reverse an empty list', () => { - let node = new DoublyLinkedListNode(1) - expect(reverse(node)).to.deep.equal(node) - }) + it('should reverse an empty list', () => { + const node = new DoublyLinkedListNode(1); + expect(reverse(node)).to.deep.equal(node); + }); - it('should reverse a chain of linked nodes', () => { - let list = new DoublyLinkedList(); - list.insertNode(1) - list.insertNode(2) - list.insertNode(3) - list.insertNode(4) - list.insertNode(5) + it('should reverse a chain of linked nodes', () => { + const list = new DoublyLinkedList(); + list.insertNode(1); + list.insertNode(2); + list.insertNode(3); + list.insertNode(4); + list.insertNode(5); - let reversedList = new DoublyLinkedList(); - reversedList.insertNode(5) - reversedList.insertNode(4) - reversedList.insertNode(3) - reversedList.insertNode(2) - reversedList.insertNode(1) + const reversedList = new DoublyLinkedList(); + reversedList.insertNode(5); + reversedList.insertNode(4); + reversedList.insertNode(3); + reversedList.insertNode(2); + reversedList.insertNode(1); - expect(reverse(list.head)).to.deep.equal(reversedList.head) - }) -}) \ No newline at end of file + expect(reverse(list.head)).to.deep.equal(reversedList.head); + }); +}); diff --git a/test/Numbers/plus-minus.test.js b/test/Numbers/plus-minus.test.js index aa7ab29..32a5b28 100644 --- a/test/Numbers/plus-minus.test.js +++ b/test/Numbers/plus-minus.test.js @@ -1,17 +1,29 @@ -const plusMinus = require('../../Numbers/plus-minus') -const { describe, it } = require('mocha') -const { expect } = require('chai') +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const plusMinus = require('../../src/Numbers/plus-minus'); describe('Plus Minus Problem', () => { - it('It should Return 2 Positive 2 Negative and 1 Zero', () => { - expect(plusMinus([1, 1, -1, -1, 0])).to.deep.equal({positive: 2, negative: 2, zero: 1 }); - }); + it('It should Return 2 Positive 2 Negative and 1 Zero', () => { + expect(plusMinus([1, 1, -1, -1, 0])).to.deep.equal({ + positive: 2, + negative: 2, + zero: 1, + }); + }); - it('It should Return 5 Positive', () => { - expect(plusMinus([1, 1, 1, 5, 6])).to.deep.equal({positive: 5, negative: 0, zero: 0 }); - }); + it('It should Return 5 Positive', () => { + expect(plusMinus([1, 1, 1, 5, 6])).to.deep.equal({ + positive: 5, + negative: 0, + zero: 0, + }); + }); - it('It should Return 5 Negative', () => { - expect(plusMinus([-1, -1, -1, -5, -6])).to.deep.equal({positive: 0, negative: 5, zero: 0 }); - }); -}) + it('It should Return 5 Negative', () => { + expect(plusMinus([-1, -1, -1, -5, -6])).to.deep.equal({ + positive: 0, + negative: 5, + zero: 0, + }); + }); +}); diff --git a/test/Strings/hackerrank-in-string.test.js b/test/Strings/hackerrank-in-string.test.js index 647c9a7..fdbd886 100644 --- a/test/Strings/hackerrank-in-string.test.js +++ b/test/Strings/hackerrank-in-string.test.js @@ -1,28 +1,24 @@ -const hackerrankInString = require("../../Strings/hackerrank-in-string"); -const { describe, it } = require("mocha"); -const { expect } = require("chai"); +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const hackerrankInString = require('../../src/Strings/hackerrank-in-string'); -describe("HackerRank in a String", () => { - it("should return NO for any empty string", () => { - expect(hackerrankInString("")).to.equal("NO"); - }); +describe('HackerRank in a String', () => { + it('should return NO for any empty string', () => { + expect(hackerrankInString('')).to.equal('NO'); + }); - it("should return YES for an exact match", () => { - expect(hackerrankInString("hackerrank")).to.equal("YES"); - }); + it('should return YES for an exact match', () => { + expect(hackerrankInString('hackerrank')).to.equal('YES'); + }); - describe("should return YES inputs containing hackerrank", () => { - it("hereiamstackerrank", () => - expect(hackerrankInString("hereiamstackerrank")).to.equal("YES")); - it("hhaacckkekraraannk", () => - expect(hackerrankInString("hhaacckkekraraannk")).to.equal("YES")); - }); - describe("should return NO for inputs that don't contain hackerrank", () => { - it("hackerworld", () => - expect(hackerrankInString("hackerworld")).to.equal("NO")); - it("rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt", () => - expect( - hackerrankInString("rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt") - ).to.equal("NO")); - }); + describe('should return YES inputs containing hackerrank', () => { + it('hereiamstackerrank', () => expect(hackerrankInString('hereiamstackerrank')).to.equal('YES')); + it('hhaacckkekraraannk', () => expect(hackerrankInString('hhaacckkekraraannk')).to.equal('YES')); + }); + describe("should return NO for inputs that don't contain hackerrank", () => { + it('hackerworld', () => expect(hackerrankInString('hackerworld')).to.equal('NO')); + it('rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt', () => expect( + hackerrankInString('rhbaasdndfsdskgbfefdbrsdfhuyatrjtcrtyytktjjt'), + ).to.equal('NO')); + }); }); diff --git a/test/Strings/repeated-string.test.js b/test/Strings/repeated-string.test.js index a797079..0dd8a47 100644 --- a/test/Strings/repeated-string.test.js +++ b/test/Strings/repeated-string.test.js @@ -1,25 +1,25 @@ -const repeatedString = require("../../Strings/repeated-string"); -const { describe, it } = require("mocha"); -const { expect } = require("chai"); +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const repeatedString = require('../../src/Strings/repeated-string'); -describe("Repeated String", () => { - it("should return 7 with input string 'aba' and a string length of 10 to search in", () => { - expect(repeatedString("aba", 10)).to.eq(7); - }); +describe('Repeated String', () => { + it("should return 7 with input string 'aba' and a string length of 10 to search in", () => { + expect(repeatedString('aba', 10)).to.eq(7); + }); - it("should return 1 with input string 'aaaaaa' and a string length of 1 to search in", () => { - expect(repeatedString("aaaaaa", 1)).to.eq(1); - }); + it("should return 1 with input string 'aaaaaa' and a string length of 1 to search in", () => { + expect(repeatedString('aaaaaa', 1)).to.eq(1); + }); - it("should return n if input string is 'a'", () => { - expect(repeatedString("a", 100000)).to.eq(100000); - expect(repeatedString("a", 300)).to.eq(300); - expect(repeatedString("a", 5)).to.eq(5); - }); + it("should return n if input string is 'a'", () => { + expect(repeatedString('a', 100000)).to.eq(100000); + expect(repeatedString('a', 300)).to.eq(300); + expect(repeatedString('a', 5)).to.eq(5); + }); - it("should return 0 if the input string does not contain 'a'", () => { - expect(repeatedString("test", 20)).to.eq(0); - expect(repeatedString("bbbb", 7)).to.eq(0); - expect(repeatedString("cdcdcd", 5)).to.eq(0); - }); + it("should return 0 if the input string does not contain 'a'", () => { + expect(repeatedString('test', 20)).to.eq(0); + expect(repeatedString('bbbb', 7)).to.eq(0); + expect(repeatedString('cdcdcd', 5)).to.eq(0); + }); }); diff --git a/test/Strings/strong-password.test.js b/test/Strings/strong-password.test.js index 2f44ed7..4e70cfc 100644 --- a/test/Strings/strong-password.test.js +++ b/test/Strings/strong-password.test.js @@ -1,26 +1,19 @@ -const passwordStrengthMinimumNumber = require("../../Strings/strong-password"); -const { describe, it } = require("mocha"); -const { expect } = require("chai"); +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const passwordStrengthMinimumNumber = require('../../src/Strings/strong-password'); -describe("Strong Password", () => { - it("returns 6 for empty string", () => - expect(passwordStrengthMinimumNumber(0, "")).to.equal(6)); +describe('Strong Password', () => { + it('returns 6 for empty string', () => expect(passwordStrengthMinimumNumber(0, '')).to.equal(6)); - it("returns 3 with password length of 3", () => - expect(passwordStrengthMinimumNumber(3, "Ab1")).to.equal(3)); + it('returns 3 with password length of 3', () => expect(passwordStrengthMinimumNumber(3, 'Ab1')).to.equal(3)); - it("returns 3 with missing special character, number and uppercase letter", () => - expect(passwordStrengthMinimumNumber(3, "longenough")).to.equal(3)); + it('returns 3 with missing special character, number and uppercase letter', () => expect(passwordStrengthMinimumNumber(3, 'longenough')).to.equal(3)); - it("returns 1 with missing number", () => - expect(passwordStrengthMinimumNumber(11, "#HackerRank")).to.equal(1)); + it('returns 1 with missing number', () => expect(passwordStrengthMinimumNumber(11, '#HackerRank')).to.equal(1)); - it("returns 1 with missing special character", () => - expect(passwordStrengthMinimumNumber(11, "1HackerRank")).to.equal(1)); + it('returns 1 with missing special character', () => expect(passwordStrengthMinimumNumber(11, '1HackerRank')).to.equal(1)); - it("returns 1 with missing uppercase letter", () => - expect(passwordStrengthMinimumNumber(12, "#1hackerrank")).to.equal(1)); + it('returns 1 with missing uppercase letter', () => expect(passwordStrengthMinimumNumber(12, '#1hackerrank')).to.equal(1)); - it("returns 1 with missing lowercase letter", () => - expect(passwordStrengthMinimumNumber(12, "#1HACKERRANK")).to.equal(1)); + it('returns 1 with missing lowercase letter', () => expect(passwordStrengthMinimumNumber(12, '#1HACKERRANK')).to.equal(1)); }); diff --git a/test/Strings/time-conversion.test.js b/test/Strings/time-conversion.test.js index c89a1d1..5e940bc 100644 --- a/test/Strings/time-conversion.test.js +++ b/test/Strings/time-conversion.test.js @@ -1,21 +1,21 @@ -const timeConversion = require("../../Strings/time-conversion"); -const { describe, it } = require("mocha"); -const { expect } = require("chai"); +const { describe, it } = require('mocha'); +const { expect } = require('chai'); +const timeConversion = require('../../src/Strings/time-conversion'); -describe("Time Conversion", () => { - it("should convert midnight", () => { - expect(timeConversion("12:00:00AM")).to.eq("00:00:00") - }); +describe('Time Conversion', () => { + it('should convert midnight', () => { + expect(timeConversion('12:00:00AM')).to.eq('00:00:00'); + }); - it("should convert noon", () => { - expect(timeConversion("12:00:00PM")).to.eq("12:00:00") - }); + it('should convert noon', () => { + expect(timeConversion('12:00:00PM')).to.eq('12:00:00'); + }); - it("should convert an AM time", () => { - expect(timeConversion("07:05:45AM")).to.eq("07:05:45") - }); + it('should convert an AM time', () => { + expect(timeConversion('07:05:45AM')).to.eq('07:05:45'); + }); - it("should convert a PM time", () => { - expect(timeConversion("07:05:45PM")).to.eq("19:05:45") - }); -}); \ No newline at end of file + it('should convert a PM time', () => { + expect(timeConversion('07:05:45PM')).to.eq('19:05:45'); + }); +});