Skip to content

Instantly share code, notes, and snippets.

@harbirchahal
harbirchahal / quick_sort.py
Created April 28, 2021 10:36
Algo: Quick Sort
def partition (array, start, end):
pivot = array[start]
low = start + 1
high = end
while (low <= high):
while (low <= end and array[low] <= pivot):
low += 1
while (high > start and array[high] > pivot):
high -= 1
@harbirchahal
harbirchahal / merge_sort.py
Last active April 28, 2021 10:33
Algo: Merge Sort
def merge_two(arr1, arr2):
merged = []
arr1_index = 0
arr2_index = 0
arr1_size = len(arr1)
arr2_size = len(arr2)
while (arr1_index < arr1_size and arr2_index < arr2_size):
if (arr1[arr1_index] < arr2[arr2_index]):
@harbirchahal
harbirchahal / heapify.py
Last active April 26, 2021 13:27
Algo: Heap -> Heapify
def heapify(array):
last_index = len(array) - 1
for index in range(last_index, -1, -1):
bubble_down(array, index)
def bubble_down(array, index):
index_limit = len(array) - 1
lchild_index = (index + 1) * 2 - 1
rchild_index = lchild_index + 1
@harbirchahal
harbirchahal / event-bubble.component.html
Last active April 20, 2021 18:20
Event Bubbling in Angular
<div (click)="handleAction($event)">
<div *ngFor="let item of listing">
<div>{{ item.label }}</div>
<div>
<button [attr.data-id]="item.id"
data-action="modify">
Modify
</button>
<button [attr.data-id]="item.id"
data-action="delete">
@harbirchahal
harbirchahal / my_array.js
Last active February 26, 2020 18:19
Naive native array implementation in JavaScript
/**
* as (typeof []) is "object"
*/
function MyArray() {
const array = Object.create(MyArray.prototype);
Object.defineProperty(array, 'length', {
value: 0,
enumerable: false,
writable: true,
});
@harbirchahal
harbirchahal / array_generators_util.js
Last active February 26, 2020 18:18
Implementing array functions as generators
const log = console.log;
function map(fn) {
return function* (arr) {
for (const el of arr) {
yield fn(el);
}
}
}
@harbirchahal
harbirchahal / String.java
Last active February 26, 2020 18:20
Mock implementation of String functions
public class String {
public static boolean contains(String s, String t) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == t.charAt(0)) {
int j = 1;
for (; j < t.length(); j++) {
if (t.charAt(j) != s.charAt(i + j)) {
break;
}
@harbirchahal
harbirchahal / constructor_function.js
Last active November 25, 2019 09:02
Constructor OO pattern in JavaScript
/** Person pseudo class. */
const Person = (function () {
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.alias = function () {
return this.firstName.charAt(0) + this.lastName.charAt(0);
}
@harbirchahal
harbirchahal / array_util.ts
Last active November 19, 2019 08:39
Simplest implementation of array utility functions.
const map = ([head, ...tail], fn) =>
typeof head === 'undefined' ?
[] :
[fn(head), ...map(tail, fn)];
const reduce = ([head, ...tail], fn, value) =>
typeof head === 'undefined' ?
value :
reduce(tail, fn, fn(value, head));
@harbirchahal
harbirchahal / MaxHeap.java
Last active April 28, 2021 10:38
DS: Max Heap
import java.util.Arrays;
/**
* MAX_HEAP: the root will always be the largest value
*/
public class MaxHeap {
private int[] data;
private int size;
public MaxHeap(int size) {