Skip to content

Fixes LGTM issues #1745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dynamic_programming/max_sum_contiguous_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
30 changes: 20 additions & 10 deletions searches/hill_climbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +64,14 @@ def __hash__(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):
"""
string representation of the current search state.
Expand All @@ -85,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.
Expand Down