From 98dc4fb38fa5c0dda8a2a8606bf44fc2f134fe29 Mon Sep 17 00:00:00 2001 From: devcer Date: Mon, 18 Jan 2021 20:09:00 +0530 Subject: [PATCH] Initial commit with algorithms list --- OddOccurrencesInArray.js | 21 ++++++++ TapeEquilibrium.js | 21 ++++++++ AliveFishes.js | 24 +++++++++ Brackets.js | 23 ++++++++ Crossover.js | 0 DistinctValues.js | 13 +++++ Dominator.js | 19 +++++++ FrogJump.js | 34 ++++++++++++ FrogRiverOne.js | 24 +++++++++ Hackerearth.js | 27 ++++++++++ Hoisting.js | 19 +++++++ Inheritance.js | 0 MaxCounters.js | 16 ++++++ MaxProductOfThree.js | 29 ++++++++++ MaxSliceSum.js | 17 ++++++ MaximumProfit.js | 14 +++++ MergeLL.js | 75 ++++++++++++++++++++++++++ MissingElement.js | 38 +++++++++++++ MissingInteger.js | 14 +++++ NestedString.js | 25 +++++++++ NoOfFactors.js | 16 ++++++ Passingcars.js | 18 +++++++ Permutation.js | 15 ++++++ SumCurry.js | 22 ++++++++ Task1.js | 38 +++++++++++++ Task2.js | 45 ++++++++++++++++ Task3.js | 28 ++++++++++ Triangle.js | 13 +++++ Westwing.js | 111 ++++++++++++++++++++++++++++++++++++++ array-rotate.js | 13 +++++ binary-gap.js | 17 ++++++ codeSnippets.js | 10 ++++ dresslife.js | 11 ++++ fetch.js | 0 minPerimeter.js | 16 ++++++ 35 files changed, 826 insertions(+) create mode 100644 OddOccurrencesInArray.js create mode 100644 TapeEquilibrium.js create mode 100644 AliveFishes.js create mode 100644 Brackets.js create mode 100644 Crossover.js create mode 100644 DistinctValues.js create mode 100644 Dominator.js create mode 100644 FrogJump.js create mode 100644 FrogRiverOne.js create mode 100644 Hackerearth.js create mode 100644 Hoisting.js create mode 100644 Inheritance.js create mode 100644 MaxCounters.js create mode 100644 MaxProductOfThree.js create mode 100644 MaxSliceSum.js create mode 100644 MaximumProfit.js create mode 100644 MergeLL.js create mode 100644 MissingElement.js create mode 100644 MissingInteger.js create mode 100644 NestedString.js create mode 100644 NoOfFactors.js create mode 100644 Passingcars.js create mode 100644 Permutation.js create mode 100644 SumCurry.js create mode 100644 Task1.js create mode 100644 Task2.js create mode 100644 Task3.js create mode 100644 Triangle.js create mode 100644 Westwing.js create mode 100644 array-rotate.js create mode 100644 binary-gap.js create mode 100644 codeSnippets.js create mode 100644 dresslife.js create mode 100644 fetch.js create mode 100644 minPerimeter.js diff --git a/ OddOccurrencesInArray.js b/ OddOccurrencesInArray.js new file mode 100644 index 0000000..dbe1c6c --- /dev/null +++ b/ OddOccurrencesInArray.js @@ -0,0 +1,21 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let map = {}; + A.forEach(element => { + if (map[element] === 1) { + map[element] = 0; + } else if(typeof map[element] === "undefined" || map[element] === 0) { + map[element] = 1; + } + }); + + for (const key in map) { + if (map.hasOwnProperty(key)) { + const element = map[key]; + if(element !== 0) { + return key; + } + } + } +} +console.log(solution([9, 3, 9, 3, 9, 7, 9])); \ No newline at end of file diff --git a/ TapeEquilibrium.js b/ TapeEquilibrium.js new file mode 100644 index 0000000..1aa8c2e --- /dev/null +++ b/ TapeEquilibrium.js @@ -0,0 +1,21 @@ +/* +A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. +Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. +The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| +In other words, it is the absolute difference between the sum of the first part and the sum of the second part. +*/ + +function solution(A) { + var sumRight = A.reduce((pv, cv, idx) => (idx > 0) ? (pv + cv) : (0), 0); + var sumLeft = 0; + var substractions = []; + var maxI = A.length - 1; + + for(var i = 0; i < maxI; i += 1){ + sumLeft += A[i]; + substractions.push(Math.abs(sumLeft - sumRight)); + if (i + 1 <= maxI) sumRight -= A[i + 1]; + } + + return substractions.reduce((pv, cv, idx) => (idx > 0) ? ((pv < cv)? pv : cv) : (cv), 0); +} \ No newline at end of file diff --git a/AliveFishes.js b/AliveFishes.js new file mode 100644 index 0000000..1fed03f --- /dev/null +++ b/AliveFishes.js @@ -0,0 +1,24 @@ +function solution(A, B) { + // write your code in JavaScript (Node.js 8.9.4) + let numFishes = A.length; + if (numFishes === 0) return 0; + let fishScore = []; + for (let i = 0; i < A.length; i++) { + if (B[i] === 1) { + fishScore.push(A[i]); + } else { + while (fishScore.length !== 0) { + if (A[i] < fishScore[fishScore.length - 1]) { + numFishes--; + break; + } else if (A[i] > fishScore[fishScore.length - 1]) { + numFishes--; + fishScore.pop(); + } + } + } + } + return numFishes; +} +console.log(solution([4, 3, 2, 1, 5], [0, 1, 0, 0, 0])); +console.log(solution([4, 6, 2, 1,7], [0, 1, 0, 0,1])); diff --git a/Brackets.js b/Brackets.js new file mode 100644 index 0000000..9f9248d --- /dev/null +++ b/Brackets.js @@ -0,0 +1,23 @@ +function solution(S) { + // write your code in JavaScript (Node.js 8.9.4) + let stack = [], + pairs = { + "{": "}", + "[": "]", + "(": ")", + }; + for (let index = 0; index < S.length; index++) { + const element = S[index]; + if (element === "{" || element === "(" || element === "[") { + stack.push(element); + continue; + } + if(stack.length === 0) return 0; + + if (element !== pairs[stack.pop()]) { + return 0; + } + } + return stack.length === 0 ? 1: 0; +} +console.log(solution("{[()()]}")); diff --git a/Crossover.js b/Crossover.js new file mode 100644 index 0000000..e69de29 diff --git a/DistinctValues.js b/DistinctValues.js new file mode 100644 index 0000000..cb9d99b --- /dev/null +++ b/DistinctValues.js @@ -0,0 +1,13 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let map = {}, + uniqueCount = 0; + A.forEach((element) => { + if (map[element] === undefined) { + map[element] = true; + uniqueCount++; + } + }); + return uniqueCount; +} +console.log(solution([2, 1, 1, 2, 3, 1])); diff --git a/Dominator.js b/Dominator.js new file mode 100644 index 0000000..44d02d1 --- /dev/null +++ b/Dominator.js @@ -0,0 +1,19 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let map = {}, + requiredLength = Math.floor(A.length / 2) + 1; + for (let index = 0; index < A.length; index++) { + const element = A[index]; + if (map[element] === undefined) { + map[element] = [index]; + } else { + map[element].push(index); + } + if (map[element].length >= requiredLength) { + return index; + } + } + return -1; +} + +console.log(solution([3, 4, 3, 2, 3, -1, 3, 3, 2])); diff --git a/FrogJump.js b/FrogJump.js new file mode 100644 index 0000000..7c5d45d --- /dev/null +++ b/FrogJump.js @@ -0,0 +1,34 @@ + +function solution(X, Y, D) { + // write your code in JavaScript (Node.js 8.9.4) + return Math.ceil((Y-X) / D); +} + + + +// A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. + +// Count the minimal number of jumps that the small frog must perform to reach its target. + +// Write a function: + +// function solution(X, Y, D); + +// that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y. + +// For example, given: +// X = 10 +// Y = 85 +// D = 30 + +// the function should return 3, because the frog will be positioned as follows: + +// after the first jump, at position 10 + 30 = 40 +// after the second jump, at position 10 + 30 + 30 = 70 +// after the third jump, at position 10 + 30 + 30 + 30 = 100 + +// Write an efficient algorithm for the following assumptions: + +// X, Y and D are integers within the range [1..1,000,000,000]; +// X ≤ Y. + diff --git a/FrogRiverOne.js b/FrogRiverOne.js new file mode 100644 index 0000000..c8993a4 --- /dev/null +++ b/FrogRiverOne.js @@ -0,0 +1,24 @@ +function solution(X, A) { + // write your code in JavaScript (Node.js 8.9.4) + let map = {}, + len = A.length; + for (let second = 0; second < len; second++) { + let val = A[second]; + if (val > X) continue; + if (map[val] === undefined || second < map[val]) { + map[val] = second; + } + } + console.log(map); + let maxSeconds = 0; + for (let index = 1; index < X + 1; index++) { + const element = map[index]; + if(element === undefined) { + return -1; + } else { + maxSeconds = Math.max(maxSeconds, element); + } + } + return maxSeconds; +} +console.log(solution(5, [1, 3, 1, 4, 2, 3, 5, 4])); diff --git a/Hackerearth.js b/Hackerearth.js new file mode 100644 index 0000000..954c715 --- /dev/null +++ b/Hackerearth.js @@ -0,0 +1,27 @@ +// console.log([[[[[[[22]]]]]]] == 22); +// console.log(["1", "2", "3"].map(parseInt)); + +// fetch("http://starlord.hackerearth.com/kickstarter") +// .then((response) => { +// console.log(response); +// }) +// .then((data) => console.log(data)); + +//Insert code below + +// console.log("Hello World!"); +fetch("http://starlord.hackerearth.com/kickstarter") + .then((response) => response.json()) + .then((data) => { + let body = ` + S. No. + Percentage Funded + Amount Pledged + `; + for (let i = 0; i < data.length; i++) { + let row = `${data[i]["s.no"]}${data[i]["percentage.funded"]}${data[i]["amt.pledged"]}`; + body = body + row; + } + console.log(body); + document.getElementById("table").innerHTML = body; + }); diff --git a/Hoisting.js b/Hoisting.js new file mode 100644 index 0000000..13e00c3 --- /dev/null +++ b/Hoisting.js @@ -0,0 +1,19 @@ +function solution() { + console.log(variableA); // undefined + console.log(letA); // Cannot access 'letA' before initialization + var variableA = 20; + let letA = 30; +} + +// solution(); + +function solutionWithTimeout() { + setTimeout(() => { + console.log(variableA); // 20 + console.log(letA); // 30 + }, 0); + var variableA = 20; + let letA = 30; +} + +solutionWithTimeout(); \ No newline at end of file diff --git a/Inheritance.js b/Inheritance.js new file mode 100644 index 0000000..e69de29 diff --git a/MaxCounters.js b/MaxCounters.js new file mode 100644 index 0000000..f180588 --- /dev/null +++ b/MaxCounters.js @@ -0,0 +1,16 @@ +function solution(N, A) { + // write your code in JavaScript (Node.js 8.9.4) + let counters = Array(N).fill(0), + max = 0; + for (let index = 0; index < A.length; index++) { + const element = A[index]; + if (element <= N) { + counters[element - 1] += 1; + max = Math.max(max, counters[element - 1]); + } else { + counters.fill(max); + } + } + return counters; +} +console.log(solution(5, [3, 4, 4, 6, 1, 4, 4])); diff --git a/MaxProductOfThree.js b/MaxProductOfThree.js new file mode 100644 index 0000000..4f52041 --- /dev/null +++ b/MaxProductOfThree.js @@ -0,0 +1,29 @@ + function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + // if size is less than 3, no triplet exists + let n = A.length; + if (n < 3) return -1; + let max1 = A[0], max2 = -1, max3 = -1, min1 = A[0], min2 = -1; + for(let i = 1; i < n; i++) { + if(A[i] > max1) { + max3 = max2; + max2 = max1; + max1 = A[i]; + } else if(max2 === -1 || A[i] > max2) { + max3 = max2; + max2 = A[i]; + } else if( max3 === -1 || A[i] > max3) { + max3 = A[i]; + } + if(A[i] < min1) { + min2 = min1; + min1 = A[i]; + } else if(min2 === -1 || A[i] < min2) { + min2 = A[i]; + } + } + console.log(max1, max2, max3, min1, min2); + let prod1 = max1 * max2 * max3, prod2 = max1 * min1 * min2; + return Math.max(prod1, prod2); + } +console.log(solution([-3, -2, -16, -20, -25])); diff --git a/MaxSliceSum.js b/MaxSliceSum.js new file mode 100644 index 0000000..d1fc3c7 --- /dev/null +++ b/MaxSliceSum.js @@ -0,0 +1,17 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let maxSum = A[0], currentSum = 0; + for (let index = 0; index < A.length; index++) { + const element = A[index]; + currentSum += element; + maxSum = Math.max(maxSum, currentSum); + if(currentSum < 0) currentSum = 0; + } + return maxSum; +} + +console.log(solution([3, 2, -6, 4, 0])); +console.log(solution([-10])); +console.log(solution([-10, -1])); + + diff --git a/MaximumProfit.js b/MaximumProfit.js new file mode 100644 index 0000000..b77a227 --- /dev/null +++ b/MaximumProfit.js @@ -0,0 +1,14 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let min = A[0], + maxProfit = 0; + for (let index = 1; index < A.length; index++) { + const element = A[index]; + maxProfit = Math.max(element - min, maxProfit); + min = Math.min(element, min); + } + return maxProfit; +} +console.log(solution([23171, 21011, 21123, 21366, 21013, 21367])); +console.log(solution([2, 3, 10, 6, 4, 8, 1])); +console.log(solution([7, 9, 5, 6, 3, 2])); diff --git a/MergeLL.js b/MergeLL.js new file mode 100644 index 0000000..ff83d9f --- /dev/null +++ b/MergeLL.js @@ -0,0 +1,75 @@ +let merge_sorted = function(head1, head2) { + // if both lists are empty then merged list is also empty + // if one of the lists is empty then other is the merged list + if (!head1) { + return head2; + } else if (!head2) { + return head1; + } + + let mergedHead = null; + if (head1.data <= head2.data) { + mergedHead = head1; + head1 = head1.next; + } else { + mergedHead = head2; + head2 = head2.next; + } + + let mergedTail = mergedHead; + + while (head1 && head2) { + let temp = null; + if (head1.data <= head2.data) { + temp = head1; + head1 = head1.next; + } else { + temp = head2; + head2 = head2.next; + } + + mergedTail.next = temp; + mergedTail = temp; + } + + if (head1) { + mergedTail.next = head1; + } else if (head2) { + mergedTail.next = head2; + } + + return mergedHead; +}; + +console.log(""); +console.log(""); +console.log("+++++++++++++++++++++++++++++++++++++++"); +console.log("Insertion Sort"); +console.log("---------------------------------------"); + +let merge_sort_node_1 = create_linked_list([1, 3, 5, 6]); +let merge_sort_node_2 = create_linked_list([2, 4, 6, 20, 34]); +let merged_sort = create_linked_list([1, 2, 3, 4, 5, 6, 6, 20, 34]); + + +let temp_head = merge_sort_node_1; +console.log("1st Linked List"); +while (temp_head) { + console.log(temp_head.data); + temp_head = temp_head.next; +} + +temp_head = merge_sort_node_2; +console.log("2nd Linked List"); +while (temp_head) { + console.log(temp_head.data); + temp_head = temp_head.next; +} +let result = merge_sorted(merge_sort_node_1, merge_sort_node_2); + +temp_head = result; +console.log("Result Merge Sorted List"); +while (temp_head) { + console.log(temp_head.data); + temp_head = temp_head.next; +} \ No newline at end of file diff --git a/MissingElement.js b/MissingElement.js new file mode 100644 index 0000000..1867c9e --- /dev/null +++ b/MissingElement.js @@ -0,0 +1,38 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let n = A.length; + let totalSum = ((n+1)*(n+2))/2; + let sum = 0; + A.forEach(element => { + sum += element; + }); + return (totalSum - sum); +} +console.log(solution([2,3,1,5])); + + + +// An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. + +// Your goal is to find that missing element. + +// Write a function: + +// class Solution { public int solution(int[] A); } + +// that, given an array A, returns the value of the missing element. + +// For example, given array A such that: +// A[0] = 2 +// A[1] = 3 +// A[2] = 1 +// A[3] = 5 + +// the function should return 4, as it is the missing element. + +// Write an efficient algorithm for the following assumptions: + +// N is an integer within the range [0..100,000]; +// the elements of A are all distinct; +// each element of array A is an integer within the range [1..(N + 1)]. + diff --git a/MissingInteger.js b/MissingInteger.js new file mode 100644 index 0000000..36a96be --- /dev/null +++ b/MissingInteger.js @@ -0,0 +1,14 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4); + let map = {}; + for (let index = 0; index < A.length; index++) { + const element = A[index]; + map[element] = true; + } + for (let index = 1; index < Number.POSITIVE_INFINITY; index++) { + if(map[index] === undefined) + return index; + } +} +console.log(solution([1, 2, 3])); +console.log(solution([1, 3, 6, 4, 1, 2])); diff --git a/NestedString.js b/NestedString.js new file mode 100644 index 0000000..c805c98 --- /dev/null +++ b/NestedString.js @@ -0,0 +1,25 @@ +function solution(S) { + // write your code in JavaScript (Node.js 8.9.4) + let stack = []; + + for (let index = 0; index < S.length; index++) { + const char = S[index]; + if(char === '(') { + stack.push(char); + } else { + if(stack[stack.length-1] === '(') + stack.pop(); + else + return 0; + } + } + if(stack.length === 0) + return 1; + return 0; +} + +console.log(solution("(()(())())")); +console.log(solution("())")); +console.log(solution(")")); +console.log(solution("(")); + diff --git a/NoOfFactors.js b/NoOfFactors.js new file mode 100644 index 0000000..de0824c --- /dev/null +++ b/NoOfFactors.js @@ -0,0 +1,16 @@ +// you can write to stdout for debugging purposes, e.g. +// console.log('this is a debug message'); + +function solution(N) { + // write your code in JavaScript (Node.js 8.9.4) + let noOfFactors = 0; + for (let i = 1; i <= Math.sqrt(N); i++) { + if (N % i === 0) { + if (N / i === i) noOfFactors++; + else noOfFactors += 2; + } + } + return noOfFactors; +} + +console.log(solution(24)); diff --git a/Passingcars.js b/Passingcars.js new file mode 100644 index 0000000..789e3c0 --- /dev/null +++ b/Passingcars.js @@ -0,0 +1,18 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let carsCount = 0, + passingCars = 0; + for (let index = A.length - 1; index >= 0; index--) { + const element = A[index]; + if (element === 1) { + carsCount++; + } else { + passingCars += carsCount; + if(passingCars > 1000000000) { + return -1; + } + } + } + return passingCars; +} +console.log(solution([0, 1, 0, 1, 1])); diff --git a/Permutation.js b/Permutation.js new file mode 100644 index 0000000..e34701b --- /dev/null +++ b/Permutation.js @@ -0,0 +1,15 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + let map = {}; + A.forEach(element => { + map[element] = 1; + }); + for(let i = 1; i <= A.length; i++) { + if(map[i] === undefined) { + return 0; + } + } + return 1; +} +console.log(solution([4, 1, 3, 2])); +console.log(solution([4, 1, 3])); diff --git a/SumCurry.js b/SumCurry.js new file mode 100644 index 0000000..002b626 --- /dev/null +++ b/SumCurry.js @@ -0,0 +1,22 @@ +function add(x) { + return function (y) { + return function (z) { + return x + y + z; + }; + }; +} +let sum = a => b => c => a+b+c; + +// console.log(add(5)(4)(3)); +// console.log(sum(5)(4)(3)); + +// We can see a clear pattern, will solve it recursively +function sumFn(a) { + return function (b) { + if (b) { + return sumFn(a+b); // it takes an argument and return a function which again can take an argument. + } + return a; // it will keep on adding 1+2+3+4.. + } + }; +console.log(sumFn(5)(4)(3)()); diff --git a/Task1.js b/Task1.js new file mode 100644 index 0000000..19dbf54 --- /dev/null +++ b/Task1.js @@ -0,0 +1,38 @@ +const ep = 0.00001; + +function distance(x, y) { + return Math.sqrt((x * x) + (y * y)); +} + +function isEqualDistance(x1, y1, x2, y2) { + return Math.abs(distance(x1, y1) - distance(x2, y2)) < ep; +} + +function solution(S, X, Y) { + let arr = []; + let map = new Map(); + let answer = 0; + + for (let i = 0; i < X.length; i++) { + arr[i] = i; + } + + arr.sort((a, b) => distance(X[a], Y[a]) - distance(X[b], Y[b])); + + for (let i = 0; i < arr.length; i++) { + const ch = S[arr[i]]; + if (map.has(ch)) { + map.forEach((idx, key) => { + if (isEqualDistance(X[idx], Y[idx], X[arr[i]], Y[arr[i]])) { + answer--; + } + }); + break; + } + map.set(ch, arr[i]); + answer++; + } + return answer; +} +console.log(solution("ABDCA", [2, -1, -4, -3, 3], [2, -2, 4, 1, -3])); +console.log(solution("ABB", [1, -2, -2], [1, -2, 2])); diff --git a/Task2.js b/Task2.js new file mode 100644 index 0000000..d8cd21a --- /dev/null +++ b/Task2.js @@ -0,0 +1,45 @@ +function solution(S, K) { + // write your code in JavaScript (Node.js 8.9.4) + let memLen = parseString(S).length; + for (let i = 0; i < S.length - (K - 1); i++) { + let subString = getSubstring(S, i, i + K - 1); + memLen = Math.min(memLen, parseString(subString).length); + } + return memLen; +} + +function getSubstring( S, begin,end) { + let sb = ""; + for (let i = 0; i < S.length; i++) { + if (i < begin || i > end) { + sb += S[i]; + } + } + return sb; +} + +function parseString(S) { + let prevChar = S[0], newString = "", count = 1; + for (let index = 1; index < S.length; index++) { + const char = S[index]; + if(char === prevChar) { + count++; + } else { + if(count < 2) { + newString += prevChar; + } else { + newString = newString + count + prevChar; + } + count = 1; + prevChar = char; + } + } + if(count < 2) { + newString += prevChar; + } else { + newString = newString + count + prevChar; + } + return newString; +} + +console.log(solution("ABBBCCDDCCC", 3)); \ No newline at end of file diff --git a/Task3.js b/Task3.js new file mode 100644 index 0000000..89efcf3 --- /dev/null +++ b/Task3.js @@ -0,0 +1,28 @@ +function solution(S) { + let totalACount = S.split("").filter((x) => x === "a").length; + let firstgap = 0; + let secondgap = 0; + let count = 0; + + if (totalACount % 3) return 0; + if (totalACount === 0) { + const n = S.length; + return ((n - 2) * (n - 1)) / 2; + } + + const firstMatch = totalACount / 3; + const secondMatch = firstMatch * 2; + + for (let i = 0; i < S.length; i++) { + if (S[i] === "a") count++; + if (count === firstMatch) firstgap++; + if (count === secondMatch) secondgap++; + } + + return firstgap * secondgap; +} + +console.log(solution("babaa")); +console.log(solution("ababa")); +console.log(solution("aba")); +console.log(solution("bbbbb")); diff --git a/Triangle.js b/Triangle.js new file mode 100644 index 0000000..ac1fff4 --- /dev/null +++ b/Triangle.js @@ -0,0 +1,13 @@ +function solution(A) { + // write your code in JavaScript (Node.js 8.9.4) + if (A.length < 3) return 0; + + A.sort((a,b) => a-b); + for (let i = 0; i < A.length - 2; i++) { + if (A[i] + A[i + 1] > A[i + 2]) return 1; + } + return 0; +} +console.log(solution([10, 2, 5, 1, 8, 20])); +console.log(solution([10, 50, 5, 1])); +console.log(solution([10, 50])); diff --git a/Westwing.js b/Westwing.js new file mode 100644 index 0000000..40ff26a --- /dev/null +++ b/Westwing.js @@ -0,0 +1,111 @@ +class Screen { + constructor(width, height) { + this.height = height; + this.width = width; + } + get diagonal() { + return Math.sqrt(Math.pow(this.width, 2) + Math.pow(this.height, 2)); + } + + set dimensions(definition) { + var dimensions = definition.split('x') + this.width = parseInt(dimensions[0]); + this.height = parseInt(dimensions[1]); + } + } + + let width = 500; + let height = 600; + let screen = new Screen(width, height); + screen.width = 800; + console.log(screen.diagonal); // Should print 1000. + screen.dimensions = '400x300'; + console.log(screen.diagonal); // Should print 500. + + + +// function PositiveNumbers() { +// this.current = 0; +// this.next = function() { +// return ++this.current; +// } +// } +// PositiveNumbers(); +// console.log( +// PositiveNumbers.next()); +// console.log( +// PositiveNumbers.next()); + +// console.log( +// PositiveNumbers.next()); + +// console.log( +// PositiveNumbers.next()); + + +// function generateNewFolderName(existingFolders) { +// // Write your code here +// const folderName = "New Folder"; +// let index = 2; +// if(!existingFolders.includes(folderName)) return folderName; +// while(existingFolders.includes(`${folderName} (${index})`)) { +// index = index + 1; +// } +// return `${folderName} (${index})`; +// } + +// console.log(generateNewFolderName(["New Folder"])); + +// class App extends React.Component { +// state = { +// subject: '', +// body: '' +// } + +// onChange = ({name, value}) => { +// this.setState({ +// [name]: value +// }) +// } + +// render() { +// return
+// +// +// +// +// +// +//
+// } +// } +// // function getP(persons) { +// // return persons.map(person => { +// // person.lastname = person.lastname.toUpperCase(); +// // return person; +// // }) +// // } +// // function getCenP(persons) { +// // return persons.map(person => { +// // person.address = '-----'; +// // return person; +// // }) +// // } + +// // const persons = [{ +// // name: 'Bert', +// // lastname: 'Simpson' +// // }, +// // { +// // name: 'Harry', +// // lastname: 'Potter' +// // }, +// // { +// // name: 'Sherlock', +// // lastname: 'Holmes' +// // }] +// // const cPersons = getP(persons); +// // const cenPersons = getCenP(persons); +// // const harry = cPersons.find(({name}) => name === 'Harry'); +// // console.log(harry); +// // // const cPersons = \ No newline at end of file diff --git a/array-rotate.js b/array-rotate.js new file mode 100644 index 0000000..f68201e --- /dev/null +++ b/array-rotate.js @@ -0,0 +1,13 @@ +// you can write to stdout for debugging purposes, e.g. +// console.log('this is a debug message'); + +function solution(A, K) { + // write your code in JavaScript (Node.js 8.9.4) + if (A.length === 0) return A; + while(K > 0) { + A.unshift(A.pop()); + K--; + } + return A; +} +console.log(solution([3, 8, 9, 7, 6], 3)); \ No newline at end of file diff --git a/binary-gap.js b/binary-gap.js new file mode 100644 index 0000000..ba093d7 --- /dev/null +++ b/binary-gap.js @@ -0,0 +1,17 @@ + function solution(N) { + // write your code in JavaScript (Node.js 8.9.4) + const bin = N.toString(2); + console.log(bin); + let zeroCount = 0, maxZeroCount = 0, checkZero = false; + for(let i = 0; i < bin.length; i++) { + zeroCount = 0 + while (bin.charAt(i) === "0") { + ++zeroCount; + ++i; + } + if(bin.charAt(i) === "1") + maxZeroCount = Math.max(zeroCount, maxZeroCount) + } + return maxZeroCount; + } +console.log(solution(1041)); diff --git a/codeSnippets.js b/codeSnippets.js new file mode 100644 index 0000000..2e7bd6c --- /dev/null +++ b/codeSnippets.js @@ -0,0 +1,10 @@ +function stopAtThree(arr) { + arr.forEach((num) => { + if (num === 3) { + return; + } + console.log(num); + }); +} + +stopAtThree([0, 1, 2, 3, 4, 5]); diff --git a/dresslife.js b/dresslife.js new file mode 100644 index 0000000..1ae9e46 --- /dev/null +++ b/dresslife.js @@ -0,0 +1,11 @@ +function solution(x) { + setTimeout(() => { + return new Promise((resolve, reject) => { + resolve(x * x); + }); + }, 3000); +} +function bar() { + return [1, 2, 3, 4].map(solution); +} +console.log(bar()); diff --git a/fetch.js b/fetch.js new file mode 100644 index 0000000..e69de29 diff --git a/minPerimeter.js b/minPerimeter.js new file mode 100644 index 0000000..7fc2ddb --- /dev/null +++ b/minPerimeter.js @@ -0,0 +1,16 @@ +function solution(N) { + // write your code in JavaScript (Node.js 8.9.4) + let noOfFactors = 0, + minPerimeter = Number.MAX_VALUE; + for (let i = 1; i <= Math.sqrt(N); i++) { + if (N % i === 0) { + let perimeter = 2 * (N / i + i); + minPerimeter = Math.min(perimeter, minPerimeter); + } + } + return minPerimeter; +} + +console.log(solution(30)); +console.log(solution(1)); +