This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
NewerOlder