Skip to content

Instantly share code, notes, and snippets.

View syphh's full-sized avatar

Syphax Ait oubelli syphh

View GitHub Profile
@syphh
syphh / sumSubDynamic.js
Created December 22, 2019 21:24
Sum of sub-matrices (dynamic programming method)
function dynamicSumSubmatrices(mat){
// n and m represent dimensions of the matrix
let n = mat.length;
let m = mat[0].length;
// declaring a (n*m) matrix
let sumMat = [...new Array(n)].map(row => [...new Array(m)]);
// part 1
sumMat[0][0] = mat[0][0];
@syphh
syphh / sumSubBruteForce.js
Last active December 22, 2019 21:44
Sum of sub-matrices (Brute force method)
function sumSubmatrices(mat){
// n and m represent dimensions of the matrix
let n = mat.length;
let m = mat[0].length;
// declaring a (n*m) matrix
let sumMat = [...new Array(n)].map(row => [...new Array(m)]);
for(let i = 0; i < n; i++){
for(let j = 0; j < m; j++){
@syphh
syphh / randMat.js
Created December 22, 2019 21:44
100*100 random matrix
let randomMat = [...new Array(100)].map(row => [...new Array(100)].map(cell => Math.floor(Math.random() * 10)));
@syphh
syphh / addTimer.js
Created December 22, 2019 21:48
Adding timer
function foo(){
console.time();
// function code
console.timeEnd();
}
function isLowerTriangular(mat){
let matSize = mat.length;
for(let i = 0; i < matSize; i++){
for(let j = i+1; j < matSize; j++){
if(mat[i][j] != 0)
return false;
}
}
return true;
}
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
def dfs(node, node_map):
clone = Node(node.val)
node_map[node.val] = clone
for neighbor in node.neighbors:
def is_safe(board, i, j):
n = len(board)
j_left = j
j_right = j
while i >= 0:
if (j_left >= 0 and board[i][j_left] == 1) or board[i][j] == 1 or (j_right < n and board[i][j_right] == 1):
return False
i -= 1
j_left -= 1
j_right += 1
import numpy as np
def approximate_pi(n):
points = np.random.uniform(-1, 1, (n, 2))
inside = np.sum(points[:,0]**2+points[:,1]**2 <= 1)
k = inside/n
return 4*k
n = 1000000
from typing import List, Any
class Node:
def __init__(self, key, value, nxt=None):
self.key: str = key
self.value: Any = value
self.nxt: Node = nxt
class LinkedList:
import numpy as np
import matplotlib.pyplot as plt
def midpoint(p1, p2):
x1, y1 = p1
x2, y2 = p2
return (x1+x2)/2, (y1+y2)/2