Skip to content

Instantly share code, notes, and snippets.

View colibie's full-sized avatar
💭
You're awesome!

Chidera colibie

💭
You're awesome!
View GitHub Profile
import java.util.ArrayList;
/**
Given a sum, and a list of non-unique integer, find a combo that sums up to that target, or the closest.
Eg [2, 3, 3, 4], target = 5
O/p = [2, 3]
[2, 3, 3, 4], target = 8
O/p = [2, 3, 3]
[2, 3, 3, 4], target = 15
O/p = [2, 3, 3, 4]
@colibie
colibie / graphCycleDetection.js
Created May 26, 2020 17:42
An extension of topological sort to include checking for cycles in a graph
// ... topological sort content
hasCycle() {
inRecursionStack = [];
marked = [];
for (int i = 0; i < vertices; i++) {
if (cycleDFS(i, inRecursionStack)) return true;
}
return false;
}
@colibie
colibie / topologicalSort.js
Created May 26, 2020 17:39
Code for topological sort
import Digraph from './Digraph'
class topologicalSort {
constructor(graph) {
this.g = graph;
this.vertices = g.V();
this.marked = []; // contains the visited vertices
this.postOrder = [];
}
@colibie
colibie / Digraph.js
Last active May 26, 2020 21:04
the data structure for a directed graph
export class Digraph { // exported so we can use it in topological sort
constructor(v) {
this.v = v; // initialize the number of vertices
this.e = 0; // and edges
this.adj = {}; // this object contains thevertices connected to each vertex.
}
addEdge(v, w) {
if (!adj[v]) adj[v] = []; // check if the vertex already exist, if not, created it
adj[v].push(w); // push the connecting edge vertex to its array of vertices
/* eslint-disable no-console */
require('dotenv').config();
const cluster = require('cluster');
const jwt = require('jsonwebtoken');
const backgroundJobHandler = require('background_job_handler');
const app = require('./appREST');
const rpc = require('./appRPC');
const { DB } = require('./api/models');
hello(5)
hello(4)
hello(3)
hello(2)
hello(1)
hello(0)
hello(-1)
---
console.log(-1) // and other statements left
console.log(0) // and other statements left
@colibie
colibie / compareHello.js
Created April 19, 2020 14:27
A comparison of recursive hello.js and iterative hello.js
function hello(i) {
if (i < 0) return; // base case
console.log(i);
hello(--i); // recursive call
}
hello(5); // output: 5 4 3 2 1 0
function hello(i) {
@colibie
colibie / factorial.js
Last active April 19, 2020 14:24
An analysis of recursive factorial
function fact(n) {
if (n == 1) return n; // base case
return n * fact(n - 1); // recursive call
}
// fact(5) analysis - output = 120
fact(5)
5 * fact(4)
5 * (4 * fact(3))
@colibie
colibie / iterativeHello.js
Created April 19, 2020 14:17
An iterative approach to hello.js
let i = 5; // I've chosen an arbitrary number
while (i > 0) {
console.log(i--);
}
// output: 5 4 3 2 1
@colibie
colibie / hello.js
Created April 19, 2020 14:09
A basic recursive function
function hello(i) {
if (i < 0) return; // base case
console.log(i);
hello(--i); // recursive call
}
hello(5) // output: 5 4 3 2 1 0