Skip to content

Instantly share code, notes, and snippets.

View petergi's full-sized avatar
💭
Just Busy Living On The Side Of A Square

Peter Giannopoulos petergi

💭
Just Busy Living On The Side Of A Square
View GitHub Profile
function binToInt(binary) {
let res = 0;
for (let i = 0; i < binary.length; i++) {
res = res * 2 + (+binary[i]);
}
return res;
}
// console.log(binToInt('0') === parseInt('0', 2) && parseInt('0', 2) === 0);
// Calculates the distance between two points in any number of dimensions.
// - Use `Object.keys()` and `Array.prototype.map()` to map each coordinate to its difference between the two points.
// - Use `Math.hypot()` to calculate the Euclidean distance between the two points.
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));
euclideanDistance([1, 1], [2, 3]); // ~2.2361
// Generates an array, containing the Fibonacci sequence, up until the nth term.
// - Use `Array.from()` to create an empty array of the specific length, initializing the first two values (`0` and `1`).
// - Use `Array.prototype.reduce()` and `Array.prototype.concat()` to add values into the array, using the sum of the last two values, except for the first two.
const fibonacci = n =>
Array.from({ length: n }).reduce(
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
[]
);
// Encrypts or decrypts a given string using the Caesar cipher.
// - Use the modulo (`%`) operator and the ternary operator (`?`) to calculate the correct encryption/decryption key.
// - Use the spread operator (`...`) and `Array.prototype.map()` to iterate over the letters of the given string.
// - Use `String.prototype.charCodeAt()` and `String.fromCharCode()` to convert each letter appropriately, ignoring special characters, spaces etc.
// - Use `Array.prototype.join()` to combine all the letters into a string.
// - Pass `true` to the last parameter, `decrypt`, to decrypt an encrypted string.
const caesarCipher = (str, shift, decrypt = false) => {
const s = decrypt ? (26 - shift) % 26 : shift;
// Checks if a date is the same as another date.
// - Use `Date.prototype.toISOString()` and strict equality checking (`===`) to check if the first date is the same as the second one.
const isSameDate = (dateA, dateB) =>
dateA.toISOString() === dateB.toISOString();
isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true
function traverse(matrix) {
const DIRECTIONS = [[0, 1], [0, -1], [1, 0], [-1, 0]];
const rows = matrix.length, cols = matrix[0].length;
const visited = matrix.map(row => Array(row.length).fill(false));
function dfs(i, j) {
if (visited[i][j]) {
return;
}
visited[i][j] = true;
// Finds the highest index at which a value should be inserted into an array in order to maintain its sort order, based on a provided iterator function.
//
// - Loosely check if the array is sorted in descending order.
// - Use `Array.prototype.map()` to apply the iterator function to all elements of the array.
// - Use `Array.prototype.reverse()` and `Array.prototype.findIndex()` to find the appropriate last index where the element should be inserted, based on the provided iterator function.
const sortedLastIndexBy = (arr, n, fn) => {
const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
const val = fn(n);
const index = arr
open System
open System.IO
open System.Threading.Tasks
let private readFileAsync (file:string) (f:byte[] -> 'a) =
async {
// Open stream
use stream = File.OpenRead(file)
// Async read, so we don't block on the thread pool
let rec insertions x = function
| [] -> [[x]]
| (y :: ys) as l -> (x::l)::(List.map (fun x -> y::x) (insertions x ys))
let rec permutations = function
| [] -> seq [ [] ]
| x :: xs -> Seq.concat (Seq.map (insertions x) (permutations xs))
open System
let getAge (d : DateTime) =
let d' = DateTime.Now
match d' > d with
| true ->
let months = 12 * (d'.Year - d.Year) + (d'.Month - d.Month)
match d'.Day < d.Day with
| true -> let days = DateTime.DaysInMonth(d.Year, d.Month) - d.Day + d'.Day