Skip to content

Instantly share code, notes, and snippets.

View misterpoloy's full-sized avatar
👽
Software Engineer

Juan P. Ortiz misterpoloy

👽
Software Engineer
View GitHub Profile
@misterpoloy
misterpoloy / Proto.js
Created November 27, 2020 06:36
Prototype Pattern
var car = {
drive(){
console.log("Started Driving")
},
brake(){
console.log("Stopping the car")
},
numofWheels : 4
}
@misterpoloy
misterpoloy / BuilderPattern.js
Created November 27, 2020 06:04
Builder Pattern in JS
function Meal() {
this.make = function(builder){
builder.step1();
builder.step2();
builder.step3();
builder.step4();
return builder.get();
}
}
@misterpoloy
misterpoloy / SingletonPattern.js
Created November 27, 2020 01:57
Singleton pattern
let instance = null;
class Printer {
constructor(pages) {
this.display= function(){
console.log(`You are connected to the printer. You want to print ${pages} pages.`)
}
}
static getInstance(numOfpages){
if(!instance){
@misterpoloy
misterpoloy / Constructor.js
Created November 27, 2020 01:52
Constructor Pattern
function Human(name, age, occupation){ //ES5 function based constructor
//defining properties inside the constructor function
//constructor initializing the property values upon object creation
this.name = name;
this.age = age;
this.occupation = occupation;
//defining a method inside the constructor function
this.describe = function(){
console.log(`${this.name} is a ${this.age}-year-old ${this.occupation}`);
}
@misterpoloy
misterpoloy / CountInversionsOfArray.cpp
Last active October 6, 2021 05:44
Divide and Conquer count inversions of an array
// Explanation https://www.youtube.com/watch?v=owZhw-A0yWE
#include <vector>
#include <iostream>
using namespace std;
int inv_count = 0; // NAIV, since I didn't want to modify my inital merge sort impelemntation LOL
vector<int> merge(vector<int> left, vector<int> right) {
vector<int> sortedArray(left.size() + right.size(), 0);
@misterpoloy
misterpoloy / Merge.cpp
Created October 5, 2020 06:04
Merge sort implementation c++
#include <vector>
using namespace std;
vector<int> merge(vector<int> left, vector<int> right) {
vector<int> sortedArray(left.size() + right.size(), 0);
int k = 0;
int i = 0;
int j = 0;
while (i < left.size() && j < right.size()) {
if (left[i] <= right[j]) {
@misterpoloy
misterpoloy / FactoryPattern.js
Created September 13, 2020 08:55
Factory Pattern Example
class IceCreamFactory {
constructor() {
this.createIcecream = function(flavor) {
let iceCream;
if (flavor === 'chocolate'){
iceCream = new Chocolate();
}
else if (flavor === 'mint'){
iceCream = new Mint();
}
@misterpoloy
misterpoloy / main.cpp
Last active October 6, 2021 05:44
Huffman Encoding
// https://www.youtube.com/watch?v=co4_ahEDCho
#include <iostream>
#include <unordered_map>
#include "minHeap.hpp"
using namespace std;
// used to store frequency of each caracter
unordered_map<char, int> freq;
#include <vector>
using namespace std;
// Do not edit the class below except for the buildHeap,
// siftDown, siftUp, peek, remove, and insert methods.
// Feel free to add new properties and methods to the class.
class MinHeap {
public:
vector<int> heap;
@misterpoloy
misterpoloy / GraphDFS.cpp
Created August 20, 2020 20:45
Depth First Search Graph
#include <iostream>
#include <list>
using namespace std;
//graph class for DFS travesal
class DFSGraph {
int V; // No. of vertices
list<int> *adjList; // adjacency list
void DFS_util(int v, bool visited[]) { // heper used by DFS