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 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: |
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
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 |
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_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 |
NewerOlder