Skip to content

Instantly share code, notes, and snippets.

View p8ul's full-sized avatar
💭
Globally Distributed Technologist.

Paul K. p8ul

💭
Globally Distributed Technologist.
  • Nairobi
  • 02:20 (UTC +03:00)
View GitHub Profile
@p8ul
p8ul / javascript-cartesian-product.js
Created October 2, 2019 17:39
Cartesian Product
/*
https://en.wikipedia.org/wiki/Cartesian_product
In set theory (and, usually, in other parts of mathematics), a Cartesian product is a mathematical
operation that returns a set (or product set or simply product) from multiple sets. That is, for sets A and B,
the Cartesian product A × B is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B. Products can
be specified using set-builder notation, e.g.
*/
let A = ['x', 'y' , 'z']
@p8ul
p8ul / bubble-sort.js
Created October 1, 2019 17:50
Bubble sort
// https://en.wikipedia.org/wiki/Bubble_sort
const sortList = (arrInput) => {
// Clone to prevent modification or original input array.
let arr = [...arrInput];
let len = arr.length;
// Boolean that determins whether the swap has occur or not.
let swapped;
do {
swapped = false;
for (let i = 0; i < len; i ++) {
@p8ul
p8ul / sliding window algorithm search string.js
Created June 26, 2019 08:25
Search through a string using sliding window algorithm.
/*
Sliding window involves creating a window which can either be an array or number from one
position to another.
Depending on a certain condition, the window either increases or closes (and new window is created)
Very uselful for keeping track of subset of data in an array/string etc.
*/
/**
@p8ul
p8ul / Prefix sum algorithm.js
Created June 23, 2019 06:33
Prefix sum algorithm and its usage.
/* We will write 2 prefix sum algorithm */
// https://www.youtube.com/watch?v=pVS3yhlzrlQ
const array = [6, 3, -2, 4, -1, 0, -5];
// Simple algorithm
const prefixSum = (array) => {
let result = [arr[0]]; // The first digit in the array don't change
array.reduce((accumulator, current) => {
result.push(accumulator + current); // next will be previous sum plus current digit
@p8ul
p8ul / javascript-array-sort-average.js
Created June 19, 2019 11:41
Two functions to 1. Sort a javascript array 2. Return average ranking
const arr = [
{ name: "John", rank: 2 },
{ name: "Mathew", rank: 4 },
{ name: "Alex", rank: 1 },
]
/**
* @func sortArray
* @desc Sort an array of object by rank
* @params {Array} arr an array of objects
@p8ul
p8ul / test_user_model.py
Last active October 18, 2018 10:09
This gist test django model. The scripts creates a new users then verifies whether a new user is created.
from django.test import TestCase
from django.contrib.auth import get_user_model
User = get_user_model()
class UserModelTest(TestCase):
"""
Test User model
"""