From cffeb36e56ccc2ed0742e8f3852755cf77a7216c Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Mon, 10 Feb 2020 17:06:19 +0530 Subject: [PATCH 1/3] Fixes redefinition of a variable --- dynamic_programming/max_sum_contiguous_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/max_sum_contiguous_subsequence.py b/dynamic_programming/max_sum_contiguous_subsequence.py index 2cbdb97a1759..bac592370c5d 100644 --- a/dynamic_programming/max_sum_contiguous_subsequence.py +++ b/dynamic_programming/max_sum_contiguous_subsequence.py @@ -6,7 +6,7 @@ def max_subarray_sum(nums: list) -> int: if not nums: return 0 n = len(nums) - s = [0] * n + res, s, s_pre = nums[0], nums[0], nums[0] for i in range(1, n): s = max(nums[i], s_pre + nums[i]) From 8f1ba952b8e99e66add4233fa6b2275de8a7f605 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Mon, 10 Feb 2020 23:52:17 +0530 Subject: [PATCH 2/3] Fixes implementing __eq__ --- searches/hill_climbing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index c1129514c04f..fee5dbf380e4 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -63,6 +63,14 @@ def __hash__(self): """ return hash(str(self)) + def __eq__(self, obj): + """ + hash the string represetation of the current search state. + """ + if isinstance(obj, SearchProblem): + return hash(str(self)) == hash(str(obj)) + return False + def __str__(self): """ string representation of the current search state. From 54697425cb9adee7b8daf1a07fbb229ccd78fa16 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 11 Feb 2020 00:26:42 +0530 Subject: [PATCH 3/3] Updates docstring --- searches/hill_climbing.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index fee5dbf380e4..324097ef5a24 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -4,17 +4,18 @@ class SearchProblem: """ - A interface to define search problems. The interface will be illustrated using - the example of mathematical function. + An interface to define search problems. + The interface will be illustrated using the example of mathematical function. """ def __init__(self, x: int, y: int, step_size: int, function_to_optimize): """ The constructor of the search problem. - x: the x coordinate of the current search state. - y: the y coordinate of the current search state. - step_size: size of the step to take when looking for neighbors. - function_to_optimize: a function to optimize having the signature f(x, y). + + x: the x coordinate of the current search state. + y: the y coordinate of the current search state. + step_size: size of the step to take when looking for neighbors. + function_to_optimize: a function to optimize having the signature f(x, y). """ self.x = x self.y = y @@ -65,7 +66,7 @@ def __hash__(self): def __eq__(self, obj): """ - hash the string represetation of the current search state. + Check if the 2 objects are equal. """ if isinstance(obj, SearchProblem): return hash(str(self)) == hash(str(obj)) @@ -93,10 +94,11 @@ def hill_climbing( max_iter: int = 10000, ) -> SearchProblem: """ - implementation of the hill climbling algorithm. We start with a given state, find - all its neighbors, move towards the neighbor which provides the maximum (or - minimum) change. We keep doing this until we are at a state where we do not - have any neighbors which can improve the solution. + Implementation of the hill climbling algorithm. + We start with a given state, find all its neighbors, + move towards the neighbor which provides the maximum (or minimum) change. + We keep doing this until we are at a state where we do not have any + neighbors which can improve the solution. Args: search_prob: The search state at the start. find_max: If True, the algorithm should find the maximum else the minimum.