Skip to content

Instantly share code, notes, and snippets.

View porteron's full-sized avatar
🌱

Nicholas Porter porteron

🌱
  • Brave Software
  • California
View GitHub Profile
@porteron
porteron / scevent.min.js
Last active September 1, 2019 15:07
Snapchat's Horrible Tracking Script - Initially found at https://sc-static.net/scevent.min.js on Aug-31-2019
try {
! function(u, s, e) {
"use strict";
var n = function() {
function n(e) {
return e && e.performance ? e.performance : null
}
var t = !1,
r = null;
try {
@porteron
porteron / flattenJson.js
Last active July 31, 2019 01:58
Flatten json to a structure where the keys are paths to the values
const d = {
'a': 5,
'b': 6,
'c': {
'f': 9,
'g': {
'm': 17,
'n': 3,
'o': {
@porteron
porteron / shiftedBinarySearch.js
Last active July 30, 2019 05:28
A sorted array has been rotated so that the elements might appear in the order: [3 4 5 6 7 1 2]. How would we find the minimum element?
function search(arr, start, end){
var mid = getMidpoint(start, end);
if(arr[mid-1] > arr[mid] && arr[mid+1] > arr[mid]){
console.log("Found Min: "+ arr[mid], mid)
return arr[mid];
}
if(arr[start] < arr[mid]){
// is sorted
@porteron
porteron / checkSubstring.js
Created July 30, 2019 05:16
Check if a string is substring of another
function checkSubstring(substr, word){
if(substr.length > word) return false;
if(substr.length && !word.length) return false;
for(let i = 0; i < word.length; i++){
if(word[i] === substr[0]){
if(compareStrings(word.substr(i, substr.length), substr)){
return true;
}
}
@porteron
porteron / permutations.js
Created July 30, 2019 05:15
Find all permutations of a string.
function permutations(word){
if(word.length <=1) return [word];
if(typeof word === 'string'){
word = word.split('')
}
if(typeof word === 'number'){
word = String(word).split('')
}
let permutationCollection = [];
@porteron
porteron / mergeSort.js
Created July 30, 2019 04:34
Merge Sort in JavaScript. Divide and Conquer. Recursively call mergeSort function on the subsets of the array, sort them, and merge back together.
function mergeSort(arr){
if(arr.length < 2){
return arr;
}
// Get mid point
var mid = Math.floor(arr.length / 2);
var left = mergeSort(arr.slice(0, mid));
@porteron
porteron / islandCount.js
Created July 30, 2019 04:31
Count the number of islands in the matrix graph. Uses Depth First Search in JavaScript.
// Find number of islands
let area = [
[0,1,0,1,1,0],
[0,1,0,1,0,1],
[1,0,0,0,1,0],
[1,0,0,0,1,0]
];
function countIslands(area){
console.log("Count Islands")
@porteron
porteron / binarySearchTree.js
Created July 30, 2019 04:29
Binary Search Tree in JavaScript. Finds path between two nodes in Binary Search Tree.
// Node structure
const Node = function(value){
return {
left: null,
right: null,
value,
}
}
// Binary Search Tree Class
.
├── actions
├── stores
├── views
│   ├── Anonymous
│   │   ├── __tests__
│   │   ├── views
│   │   │   ├── Home
│   │   │   │   ├── __tests__
│   │   │   │   └── Handler.js