Skip to content

Instantly share code, notes, and snippets.

View flyptkarsh's full-sized avatar

Patrick Karsh flyptkarsh

View GitHub Profile
@flyptkarsh
flyptkarsh / dijkstra.js
Created May 19, 2024 16:15
Dijkstra's Shortest Path Algorithm in JavaScript
// Define a graph using an adjacency list
const graph = {
A: { B: 1, C: 4 }, // Node A is connected to Node B with a weight of 1 and Node C with a weight of 4
B: { A: 1, C: 2, D: 5 }, // ... and so on for other nodes
C: { A: 4, B: 2, D: 1 },
D: { B: 5, C: 1 }
};
function dijkstra(graph, start) {
// Create an object to store the shortest distance from the start node to every other node
@flyptkarsh
flyptkarsh / max_heap.rb
Created March 8, 2024 16:16
MaxHeap Ruby implementation
class MaxHeap
def initialize
@heap = []
end
def push(val)
@heap << val
sift_up(@heap.size - 1)
end
@flyptkarsh
flyptkarsh / min_heap.rb
Created March 8, 2024 16:09
MinHeap in Ruby
class MinHeap
def initialize
@heap = []
end
def push(val)
@heap << val
sift_up(@heap.size - 1)
end
@flyptkarsh
flyptkarsh / SQLWindowFunctions.md
Last active March 8, 2024 15:30
SQL Window Functions

SQL Window Functions Examples

This gist provides practical examples of SQL window functions to enhance your data analysis skills. Through these examples, you'll learn how to use various window functions to perform complex calculations like ranking, running totals, moving averages, and more.

1. Assign Row Numbers

SELECT id, salesperson, amount,
       ROW_NUMBER() OVER(ORDER BY amount DESC) AS row_num
FROM sales;
@flyptkarsh
flyptkarsh / dijkstra_with_comments.rb
Created March 3, 2024 20:45
Dijkstra's Algorithm (with comments)
# Define a graph using an adjacency list
graph = {
'A' => { 'B' => 1, 'C' => 4 },
'B' => { 'A' => 1, 'C' => 2, 'D' => 5 },
'C' => { 'A' => 4, 'B' => 2, 'D' => 1 },
'D' => { 'B' => 5, 'C' => 1 }
}
# Define the dijkstra method to find the shortest path from a start node
def dijkstra(graph, start)
@flyptkarsh
flyptkarsh / dijkstra_without_comments.rb
Last active March 3, 2024 20:42
Dijkstra’s Shortest Path Algorithm (without comments)
graph = {
'A' => { 'B' => 1, 'C' => 4 },
'B' => { 'A' => 1, 'C' => 2, 'D' => 5 },
'C' => { 'A' => 4, 'B' => 2, 'D' => 1 },
'D' => { 'B' => 5, 'C' => 1 }
}
def dijkstra(graph, start)
distances = {}
visited = {}
@flyptkarsh
flyptkarsh / .zshrc
Created February 16, 2024 21:03
zshrc Settings
# Function to parse the current Git branch
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
# Color definitions
COLOR_DEF=$'%f'
COLOR_USR=$'%F{243}'
COLOR_DIR=$'%F{197}'
COLOR_GIT=$'%F{39}'
@flyptkarsh
flyptkarsh / closure.js
Created February 16, 2024 16:11
Counter Closure
function counter() {
// This variable is private to the counter function
let count = 0;
// The function returned is a closure that
// has access to the count variable
return function() {
count += 1; // Increment the count
return count; // Return the updated count
};
@flyptkarsh
flyptkarsh / closure.js
Last active February 16, 2024 16:03
Example of a Closure
function outerFunction() {
let outerVariable = 'I am outside!';
function innerFunction() {
// Accesses outerVariable from the outer scope
console.log(outerVariable);
}
return innerFunction;
}
@flyptkarsh
flyptkarsh / greedyKnapsack.js
Created February 15, 2024 17:58
A greedy algorithm for solving the 0/1 Knapsack problem
function greedyKnapsack(values, weights, capacity) {
let n = values.length;
let ratio = [];
let totalValue = 0;
// Step 1: Calculate value-to-weight ratio for each item
for (let i = 0; i < n; i++) {
ratio.push({ index: i, value: values[i], weight: weights[i], ratio: values[i] / weights[i] });
}