This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
*/ | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test import TestCase | |
from django.contrib.auth import get_user_model | |
User = get_user_model() | |
class UserModelTest(TestCase): | |
""" | |
Test User model | |
""" |