diff --git a/tests/binary_knapsack.py b/tests/binary_knapsack.py new file mode 100644 index 0000000..8df83f7 --- /dev/null +++ b/tests/binary_knapsack.py @@ -0,0 +1,50 @@ +""" +Author: Omkar Pathak +Created At: 25th August 2017 +""" +import inspect + + +def knapsack(w, value, weight): + """ + The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of + items, each with a weight and a value, determine the number of each item to include in a collection + so that the total weight is less than or equal to a given limit and the total value is as large as + possible. It derives its name from the problem faced by someone who is constrained by a fixed-size + knapsack and must fill it with the most valuable items. + + :param w: maximum weight capacity + :param value: an array of values of items in the knapsack + :param weight: an array of weights of items in the knapsack + """ + if type(value) is not list: + raise TypeError("binary knapsack only accepts lists, not {}".format(str(type(value)))) + if type(weight) is not list: + raise TypeError("binary knapsack only accepts lists, not {}".format(str(type(weight)))) + + if len(value) != len(weight): + raise ValueError("both the lists must be of same length") + + # n = number of items + n = len(value) + + knap_sack = [[0 for _ in range(w+1)] for _ in range(n+1)] + + for j in range(w + 1): + knap_sack[0][j] = 0 + + for i in range(n + 1): + for w in range(w + 1): + if weight[i - 1] <= w: + knap_sack[i][w] = max(value[i - 1] + knap_sack[i - 1][w - weight[i - 1]], knap_sack[i - 1][w]) + else: + knap_sack[i][w] = knap_sack[i - 1][w] + + return knap_sack[n][w] + + +def get_code(): + """ + returns the code for the knapsack function + """ + return inspect.getsource(knapsack) diff --git a/tests/lis.py b/tests/lis.py new file mode 100644 index 0000000..3a111a5 --- /dev/null +++ b/tests/lis.py @@ -0,0 +1,45 @@ +""" +Author: Omkar Pathak +Created At: 25th August 2017 +""" +import inspect + +def longest_increasing_subsequence(_list): + """ + The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a + given sequence such that all elements of the subsequence are sorted in increasing order. For example, + the length of LIS for [10, 22, 9, 33, 21, 50, 41, 60, 80] is 6 and LIS is [10, 22, 33, 50, 60, 80]. + + :param _list: an array of elements + :return: returns a tuple of maximum length of lis and an array of the elements of lis + """ + # Initialize list with some value + lis = [1] * len(_list) + # list for storing the elements in an lis + elements = [0] * len(_list) + + # Compute optimized LIS values in bottom up manner + for i in range(1, len(_list)): + for j in range(0, i): + if _list[i] > _list[j] and lis[i] < lis[j] + 1: + lis[i] = lis[j]+1 + elements[i] = j + + # find the maximum of the whole list and get its index in idx + maximum = max(lis) + idx = lis.index(maximum) + + # for printing the elements later + seq = [_list[idx]] + while idx != elements[idx]: + idx = elements[idx] + seq.append(_list[idx]) + + return (maximum, seq[::-1]) + + +def get_code(): + """ + returns the code for the longest_increasing_subsequence function + """ + return inspect.getsource(longest_increasing_subsequence) diff --git a/tests/min_cost_path.py b/tests/min_cost_path.py new file mode 100644 index 0000000..ad01edf --- /dev/null +++ b/tests/min_cost_path.py @@ -0,0 +1,51 @@ +""" +Author: MrDupin +Created At: 25th August 2017 +""" +import inspect + +#Path(i, j) = min(Path(i-1, j), Path(i, j-1) + Matrix(i, j) + + +def calculate_path(i, j, matrix, s): + if(s[i][j] > 0): + #We have already calculated solution for i,j; return it. + return s[i][j] + + m1 = calculate_path(i-1, j, matrix, s) + matrix[i][j] #Optimal solution for i-1, j (top) + m2 = calculate_path(i, j-1, matrix, s) + matrix[i][j] #Optimal solution for i, j-1 (left) + + #Store and return the optimal (minimum) solution + if(m1 < m2): + s[i][j] = m1 + return m1 + else: + s[i][j] = m2 + return m2 + + +def find_path(matrix): + l = len(matrix); + #Initialize solution array. + #A node of i, j in solution has an equivalent node of i, j in matrix + s = [[0 for i in range(l)] for j in range(l)]; + + #Initialize first node as its matrix equivalent + s[0][0] = matrix[0][0] + + #Initialize first column as the matrix equivalent + the above solution + for i in range(1, l): + s[i][0] = matrix[i][0] + s[i-1][0] + + #Initialize first row as the matrix equivalent + the left solution + for j in range(1, l): + s[0][j] = matrix[0][j] + s[0][j-1] + + return calculate_path(l-1, l-1, matrix, s) + + +def get_code(): + """ + returns the code for the min cost path function + """ + return inspect.getsource(calculate_path)