This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :g/^/m0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function system(command) | |
| local file = assert(io.popen(command, "r")) | |
| local output = file:read("*all"):gsub("%s+", "") | |
| file:close() | |
| return output | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Checks if a date is before another date. | |
| // - Use the less than operator (`<`) to check if the first date comes before the second one. | |
| const isBeforeDate = (dateA, dateB) => dateA < dateB; | |
| isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Finds the index of a given element in a sorted array using the binary search algorithm. | |
| // | |
| // - Declare the left and right search boundaries, `l` and `r`, initialized to `0` and the `length` of the array respectively. | |
| // - Use a `while` loop to repeatedly narrow down the search subarray, using `Math.floor()` to cut it in half. | |
| // - Return the index of the element if found, otherwise return `-1`. | |
| // - **Note:** Does not account for duplicate values in the array. | |
| const binarySearch = (arr, item) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Checks if a date is after another date. | |
| // Use the greater than operator (`>`) to check if the first date comes after the second one. | |
| const isAfterDate = (dateA, dateB) => dateA > dateB; | |
| isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Returns the elements that exist in both arrays, filtering duplicate values. | |
| // Create a `Set` from `b`, then use `Array.prototype.filter()` on `a` to only keep values contained in `b`. | |
| const intersection = (a, b) => { | |
| const s = new Set(b); | |
| return [...new Set(a)].filter(x => s.has(x)); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Finds all unique values of an array, based on a provided comparator function, starting from the right. | |
| // - Use `Array.prototype.reduceRight()` and `Array.prototype.some()` to create an array containing only the last unique occurrence of each value, based on the comparator function, `fn`. | |
| // - The comparator function takes two arguments: the values of the two elements being compared. | |
| const uniqueElementsByRight = (arr, fn) => | |
| arr.reduceRight((acc, v) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Checks if all the elements in `values` are included in `arr`. | |
| // - Use `Array.prototype.every()` and `Array.prototype.includes()` to check if all elements of `values` are included in `arr`. | |
| const includesAll = (arr, values) => values.every(v => arr.includes(v)); | |
| // includesAll([1, 2, 3, 4], [1, 4]); // true | |
| // includesAll([1, 2, 3, 4], [1, 5]); // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Interval: [start, end]. | |
| // Merges two overlapping intervals into one. | |
| function intervalsMerge(a, b) { | |
| return [Math.min(a[0], b[0]), Math.max(a[1], b[1])]; | |
| } | |
| const deepEqual = require('./deepEqual'); | |
| console.log(deepEqual(intervalsMerge([1, 2], [1, 4]), [1, 4])); | |
| console.log(deepEqual(intervalsMerge([1, 2], [0, 4]), [0, 4])); |
NewerOlder