Skip to content

Instantly share code, notes, and snippets.

@tianmingzuo
tianmingzuo / gist:4f4fd4841307efb807ef7d711e8c8d50
Created October 11, 2018 15:39
/* You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments. */
/*
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
*/
function destroyer(arr) {
let len =arguments.length;
let indexArr = [];
let arrCopy = arr.concat([]); //do not change the input array
if(len > 1){
for(let i=0; i<arrCopy.length; i++){
@tianmingzuo
tianmingzuo / symmetricDifference.js
Created October 24, 2018 05:58
Find the Symmetric Difference Create a function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays. Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term "symmetric difference" of two sets is the set of elements which are in either of the two sets, …
function sym(args) {
let len = arguments.length;
let outArr = [];
//delete the repeated element in each argument
for(let i=0; i<len; i++){
if(arguments[i].length <= 1){
arguments[i] = arguments[i];
}
for(let j=arguments[i].length-1; j>=1; j--){
for(let k=j-1; k>=0; k--){
function moveDisk(n, source, buffer, destination){
if(n>=1){
moveDisk(n-1, source, destination, buffer);
console.log("Move one disk from " + source + " to " + destination);
moveDisk(n-1, buffer, source, destination);
}else{
return;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>JS Calculator</title>
<!--<link rel="stylesheet" type="text/css" href="calculator.css"> -->
<style>
div table{
@tianmingzuo
tianmingzuo / createHashTable.js
Created September 16, 2019 03:07
A Hash table is used to implement associative arrays, or mappings of key-value pairs, like the objects and Maps we have just been studying. A JavaScript object could be implemented as a hash table, for instance (its actual implementation will depend on the environment it's running in). The way a hash table works is that it takes a key input and …
var called = 0;
var hash = (string) => {
called++;
var hash = 0;
for (var i = 0; i < string.length; i++) { hash += string.charCodeAt(i); }
return hash;
};
var HashTable = function() {
this.collection = {};
// change code below this line
@tianmingzuo
tianmingzuo / createLinkedListClass.js
Created September 16, 2019 16:49
Create a Linked List Class: if our list already has one or more members? How do we add an element to the list? Recall that each node in a linked list has a next property. To add a node to the list, find the last node in the list, and point that last node's next property at our new node. (Hint: you know you've reached the end of a linked list whe…
function LinkedList() {
var length = 0;
var head = null;
var Node = function(element){
this.element = element;
this.next = null;
};
this.head = function(){
@tianmingzuo
tianmingzuo / createDoublyLinkedList.js
Last active September 18, 2019 02:49
Create a Doubly Linked List: Nodes in a doubly linked list have references to the next and previous node in the list. Let's add two methods to our doubly linked list called add and remove. The add method should add the given element to the list while the remove method should remove all occurrences of a given element in the list. Be careful to ha…
var Node = function(data, prev) {
this.data = data;
this.prev = prev;
this.next = null;
};
var DoublyLinkedList = function() {
this.head = null;
this.tail = null;
// change code below this line
length = 0;
@tianmingzuo
tianmingzuo / addNodeToBinarySearchTree.js
Created September 21, 2019 01:32
Data Structures: Add a New Element to a Binary Search Tree: The method should be called addand it should accept an integer value to add to the tree. Take care to maintain the invariant of a binary search tree: the value in each left child should be less than or equal to the parent value, and the value in each right child should be greater than o…
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
this.add = function(val) {
@tianmingzuo
tianmingzuo / maxPalindromeProduct.js
Created September 27, 2019 16:52
Largest palindrome product: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two n-digit numbers.
function largestPalindromeProduct(n) {
// Good luck!
let i = 10**n-1;
let k = 10**(n-1);
while (i >= k){
let j = 10**n-1;
while(j >= k){
console.log("attempt", i, j, i*j);
@tianmingzuo
tianmingzuo / newLlargestPalindromeProduct.js
Created September 27, 2019 17:09
Largest palindrome product: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two n-digit numbers.
function isPalindrome(s) {
let len = s.length;
for (let i = 0; i < Math.floor(len / 2); i++) {
if (s.charAt(i) !== s.charAt(len - i - 1)) {
return false;
}
}
return true;
}