Skip to content

Instantly share code, notes, and snippets.

View DiegoGallegos4's full-sized avatar
🏠
Working from home

Diego Gallegos DiegoGallegos4

🏠
Working from home
View GitHub Profile
def linear_search(A, k):
"""
Args:
A array: array with n elements
Returns
index i where A[i] = k, if there is not such i, then NOT_FOUND
"""
for (index, item) in enumerate(A):
if item == k:
@DiegoGallegos4
DiegoGallegos4 / fractional_knapsack.py
Created April 9, 2019 17:36
Greedy implementation of Fractional Knapsack
items = [(20, 4), (18, 3), (14, 2)]
W = 7
def fractional_knapsack_naive(items, bag_weight):
"""
Maximize the value of items that fit into knapsack
constrained to bag_weight
Args:
items: list of tuples containing (value_i, weight_i) pairs
@DiegoGallegos4
DiegoGallegos4 / min_refills.py
Last active April 6, 2019 16:55
Greedy implementation of Max Refills Problem
def min_refills(stations, full_tank_limit):
"""
Find min refills for gas tank in a trip between point A and B
Args:
stations (array): possible gas stations to refill including start(A) and end(B).
full_tank_limit (int): gas tank maximum limit.
Returns:
(int) number of refills needed to make the trip between A -> B