Skip to content

Instantly share code, notes, and snippets.

View oliviagallucci's full-sized avatar
Thinking about free(dom) and open source software

Olivia Gallucci oliviagallucci

Thinking about free(dom) and open source software
View GitHub Profile
@oliviagallucci
oliviagallucci / nested_recursion.py
Last active April 29, 2024 14:14
example of nested recursion
def nested_recursion(n):
if n > 100:
return n - 10
else:
# here is the nesting
return nested_recursion(nested_recursion(n + 11))
result = nested_recursion(95)
print(result)
# prints
@oliviagallucci
oliviagallucci / a_star.py
Created February 4, 2024 12:21
A* implementation in Python
# this code implements the A* algorithm to find the shortest path between nodes in
# a graph, considering the cost of edges and heuristic values, and demonstrates
# its usage with a sample graph and finding the shortest path between specified
# start and goal nodes.
import heapq
class Graph:
def __init__(self):
# initialize the graph with empty sets and dictionaries to store nodes,
@oliviagallucci
oliviagallucci / floyd_warshall.py
Created February 4, 2024 12:02
Floyd Warshall algorithm example in Python
# this program defines the floyd-warshall algorithm to find the shortest paths between all
# pairs of vertices in a weighted graph. it also provides a sample graph represented as an
# adjacency matrix (sample_edges). you can modify sample_edges with your own graph
# structure as needed.
INF = float('inf')
# find the shortest paths between all pairs of vertices
def floyd_warshall(graph):
num_vertices = len(graph)
@oliviagallucci
oliviagallucci / bellman_ford.py
Created February 4, 2024 11:49
Example Bellman Ford algorithm in Python
# this program initializes a graph with a given number of vertices,
# adds edges to it, and then executes the bellman-ford algorithm to
# find the shortest paths from a specified source vertex to all
# other vertices in the graph. it also checks for negative weight
# cycles in the graph.
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
@oliviagallucci
oliviagallucci / bfs.py
Created January 3, 2024 00:27
breadth-first search example (computational finance)
from collections import deque
class financialGraph:
def __init__(self):
self.graph = {}
def add_instrument(self, instrument, dependencies):
# add a financial instrument and its dependencies to the graph
self.graph[instrument] = dependencies
@oliviagallucci
oliviagallucci / weightedIntervalScheduling.py
Created January 3, 2024 00:10
Interval scheduling variant - dynamic programming (not greedy)
def weighted_interval_scheduling(intervals):
# sort intervals by finish times
intervals.sort(key=lambda x: x[1])
n = len(intervals)
dp = [0] * n
for i in range(n):
# find the non-overlapping interval before i
non_overlapping_index = binary_search(intervals, i)
@oliviagallucci
oliviagallucci / minCoinChange.py
Created December 31, 2023 23:44
coin change example (finds minimum amount of coins) - dynamic programming in python
def min_coins(coins, amount):
# create a table to store the minimum number of coins
# needed for each amount
dp = [float('inf')] * (amount + 1)
# there is no need for any coin to make change
# for amount 0
dp[0] = 0
# iterate through each coin denomination
@oliviagallucci
oliviagallucci / coinChange.py
Last active December 31, 2023 22:10
coin change example (finds total number of ways) - dynamic programming in python
def coin_change_ways(coins, amount):
# create a table to store the results of subproblems
dp = [0] * (amount + 1)
# there is one way to make change
# for amount 0 (using no coins)
dp[0] = 1
# iterate through each coin denomination
for coin in coins:
# update the table for each amount from the
@oliviagallucci
oliviagallucci / huffmanCoding.py
Created December 31, 2023 21:58
Simple implementation of Huffman coding in Python
# a simple implementation of Huffman coding in Python.
# Huffman coding is a lossless data compression algorithm that assigns
# variable-length codes to input characters, with shorter codes
# assigned to more frequent characters.
import heapq
from collections import defaultdict
class Node:
def __init__(self, char, freq):
def longestCommonSubsequence(X, Y):
m = len(X)
n = len(Y)
# create a 2D array to store the lengths of LCS
# initialize the array with zeros
dp = [[0] * (n + 1) for _ in range(m + 1)]
# build the LCS array in a bottom-up manner
for i in range(m + 1):