Fixes LGTM issues (#1745)

* Fixes redefinition of a variable

* Fixes implementing __eq__

* Updates docstring
This commit is contained in:
onlinejudge95 2020-02-11 02:53:19 +05:30 committed by GitHub
parent 80718bd880
commit 6fdd53c676
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View File

@ -6,7 +6,7 @@ def max_subarray_sum(nums: list) -> int:
if not nums: if not nums:
return 0 return 0
n = len(nums) n = len(nums)
s = [0] * n
res, s, s_pre = nums[0], nums[0], nums[0] res, s, s_pre = nums[0], nums[0], nums[0]
for i in range(1, n): for i in range(1, n):
s = max(nums[i], s_pre + nums[i]) s = max(nums[i], s_pre + nums[i])

View File

@ -4,17 +4,18 @@ import math
class SearchProblem: class SearchProblem:
""" """
A interface to define search problems. The interface will be illustrated using An interface to define search problems.
the example of mathematical function. The interface will be illustrated using the example of mathematical function.
""" """
def __init__(self, x: int, y: int, step_size: int, function_to_optimize): def __init__(self, x: int, y: int, step_size: int, function_to_optimize):
""" """
The constructor of the search problem. The constructor of the search problem.
x: the x coordinate of the current search state.
y: the y coordinate of the current search state. x: the x coordinate of the current search state.
step_size: size of the step to take when looking for neighbors. y: the y coordinate of the current search state.
function_to_optimize: a function to optimize having the signature f(x, y). 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.x = x
self.y = y self.y = y
@ -63,6 +64,14 @@ class SearchProblem:
""" """
return hash(str(self)) return hash(str(self))
def __eq__(self, obj):
"""
Check if the 2 objects are equal.
"""
if isinstance(obj, SearchProblem):
return hash(str(self)) == hash(str(obj))
return False
def __str__(self): def __str__(self):
""" """
string representation of the current search state. string representation of the current search state.
@ -85,10 +94,11 @@ def hill_climbing(
max_iter: int = 10000, max_iter: int = 10000,
) -> SearchProblem: ) -> SearchProblem:
""" """
implementation of the hill climbling algorithm. We start with a given state, find Implementation of the hill climbling algorithm.
all its neighbors, move towards the neighbor which provides the maximum (or We start with a given state, find all its neighbors,
minimum) change. We keep doing this until we are at a state where we do not move towards the neighbor which provides the maximum (or minimum) change.
have any neighbors which can improve the solution. We keep doing this until we are at a state where we do not have any
neighbors which can improve the solution.
Args: Args:
search_prob: The search state at the start. search_prob: The search state at the start.
find_max: If True, the algorithm should find the maximum else the minimum. find_max: If True, the algorithm should find the maximum else the minimum.