Skip to content

Instantly share code, notes, and snippets.

@nicolekc
nicolekc / components.box\.js
Created January 23, 2022 23:34
New Twiddle
import ClassicComponent from '@ember/component';
export default class extends ClassicComponent {
tagName = '';
}
let truths = [
[ 'a', 1 ],
[ 'b', 2 ],
[ 'c', 1, 2 ],
[ 'd', 3 ],
[ 'e', 3, 4 ]
];
function permutations(input, index = 0, prefix = [], stack = []) {
/**
* You have an array of "truths" represented in the form:
*
* [ subjectA, condition1, condition2 ]
*
* which means subjectA satisfies condition1 and condition2.
*
* Create an algorithm which generates all possible truth permutations in the form:
*
* [ [...conditions], [...subjects] ]
/**
* Input: 1, 2, 3, 4
* Output:
* 1
* 1, 2
* 1, 2, 3
* 1, 2, 3, 4
* 1, 2, 4
* 1, 3
* 1, 3, 4
// MY updated solution (merge sort implementation):
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
// https://leetcode.com/problems/clone-graph/
/**
* // Definition for a Node.
* function Node(val, neighbors) {
* this.val = val === undefined ? 0 : val;
* this.neighbors = neighbors === undefined ? [] : neighbors;
* };
*/
// Solution to https://www.interviewcake.com/question/javascript/find-duplicate-optimize-for-space-beast-mode
function findDuplicate(intArray) {
// Find a number that appears more than once ... in O(n) time
let n = 0;
let pos = intArray[intArray.length - 1];
// Get into the cycle
while (n < intArray.length) {
function quickSort(data, left = 0, right = data.length - 1) {
if (right <= left) {
return data;
}
let split = partition(data, left, right);
quickSort(data, left, split - 1);
quickSort(data, split + 1, right);
}
function hash(str) {
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = hash * 33 + str.charCodeAt(i);
hash |= 0;
}
return hash;
}
function HashTable() {
function mergeSort(arr, start = 0, length = arr.length) {
if (length <= 1) {
return length == 1 ? [ arr[start] ] : [];
}
let halfLength = Math.floor(length / 2);
let left = mergeSort(arr, start, halfLength);
let right = mergeSort(arr, start + halfLength, length - halfLength);
let leftIdx = 0;