Skip to content

Instantly share code, notes, and snippets.

View cagataycali's full-sized avatar
🧬

./c² cagataycali

🧬
View GitHub Profile
@cagataycali
cagataycali / single-cycle-check.js
Created November 8, 2021 21:40
[JavaScript] Graph single cycle check
const assert = require('assert');
function hasSingleCycle(array) {
let numberOfVisitedNodes = 0;
let currentIndex = 0;
while (numberOfVisitedNodes < array.length) {
// If we go back to the first element,
// That shows, we have an infinite loop before our loop ends.
if (currentIndex === 0 && numberOfVisitedNodes > 0) return false;
currentIndex = jump(currentIndex, array);
@cagataycali
cagataycali / depth-first-search.js
Created November 8, 2021 21:07
[JavaScript] Graph depth first search
const assert = require("assert");
class Node {
constructor(name) {
this.name = name;
this.children = [];
}
addChild(name) {
this.children.push(new Node(name));
@cagataycali
cagataycali / merge-sorted-linked-lists.js
Last active November 6, 2021 19:32
[JavaScript] Merge two sorted linked lists
function mergeLinkedLists(headOne, headTwo) {
let p1 = headOne;
let p1Prev = null;
let p2 = headTwo;
while (p1 !== null && p2 !== null) {
// If our second linkedlist's pointer value is bigger
if (p1.value < p2.value) {
// Store the previous
p1Prev = p1;
// Move the pointer to next item.
@cagataycali
cagataycali / word-ladder.js
Created November 5, 2021 17:39
[JavaScript] Word ladder
const assert = require("assert");
// Leetcode: https://leetcode.com/problems/word-ladder/
// Leetcode: https://leetcode.com/problems/word-ladder-ii/
// Inputs: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
// Output: 5
/*
A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
@cagataycali
cagataycali / max-value.js
Created November 4, 2021 17:42
[JavaScript] Maximum value after insertion
// https://leetcode.com/problems/maximum-value-after-insertion
const assert = require("assert");
// n very large string represent integer,
// x [1-9]
// return string
function maxValue(n, x) {
// If the input is empty.
if (!n || n.length === 0) {
return null;
}
@cagataycali
cagataycali / readme.md
Last active November 14, 2021 01:57
[Bash Script] Package and Package-lock version bump with bash script

Usage

# Give executable permission to script;
chmod +x update.sh;
# Execute with mode major;
./update.sh major;
# Execute with mode minor;
./update.sh minor;
# Execute with mode patch;
@cagataycali
cagataycali / bst-validation.js
Created November 2, 2021 20:54
[JavaScript] Binary search tree validation
class BST {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function validateBst(tree) {
return validate(tree, -Infinity, Infinity)
@cagataycali
cagataycali / bst-construction.js
Created November 2, 2021 20:53
[JavaScript] Binary Search Tree Construction
class BST {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
insert(value) {
let currentNode = this;
while (true) {
@cagataycali
cagataycali / brackets-validator.js
Created October 31, 2021 19:02
[JavaScript] Brackets validator
const assert = require('assert');
// '(', '{', '[' are called "openers."
// ')', '}', ']' are called "closers."
function validateBrackets(input) {
const brackets = {
openings: {
'{': '}',
'(': ')',
@cagataycali
cagataycali / sum.js
Created October 31, 2021 15:12
[JavaScript] Simple currying with sum
function sum(a) {
return function (b) {
if (b !== undefined) {
return sum(a + b)
}
return a;
}
}