mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-20 12:47:35 +00:00
Typos in comments in hill_climbing.py (#1667)
* Typos in comments in hill_climbing.py * fixup! Format Python code with psf/black push
This commit is contained in:
parent
36d229f82a
commit
1f2b1a88ab
@ -7,31 +7,36 @@ Operations:
|
|||||||
4. remove from the end -> O(1)
|
4. remove from the end -> O(1)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class _DoublyLinkedBase:
|
class _DoublyLinkedBase:
|
||||||
""" A Private class (to be inherited) """
|
""" A Private class (to be inherited) """
|
||||||
|
|
||||||
class _Node:
|
class _Node:
|
||||||
__slots__ = '_prev', '_data', '_next'
|
__slots__ = "_prev", "_data", "_next"
|
||||||
|
|
||||||
def __init__(self, link_p, element, link_n):
|
def __init__(self, link_p, element, link_n):
|
||||||
self._prev = link_p
|
self._prev = link_p
|
||||||
self._data = element
|
self._data = element
|
||||||
self._next = link_n
|
self._next = link_n
|
||||||
|
|
||||||
def has_next_and_prev(self):
|
def has_next_and_prev(self):
|
||||||
return " Prev -> {0}, Next -> {1}".format(self._prev != None, self._next != None)
|
return " Prev -> {0}, Next -> {1}".format(
|
||||||
|
self._prev != None, self._next != None
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._header = self._Node(None, None, None)
|
self._header = self._Node(None, None, None)
|
||||||
self._trailer = self._Node(None, None, None)
|
self._trailer = self._Node(None, None, None)
|
||||||
self._header._next = self._trailer
|
self._header._next = self._trailer
|
||||||
self._trailer._prev = self._header
|
self._trailer._prev = self._header
|
||||||
self._size = 0
|
self._size = 0
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self._size
|
return self._size
|
||||||
|
|
||||||
def is_empty(self):
|
def is_empty(self):
|
||||||
return self.__len__() == 0
|
return self.__len__() == 0
|
||||||
|
|
||||||
def _insert(self, predecessor, e, successor):
|
def _insert(self, predecessor, e, successor):
|
||||||
# Create new_node by setting it's prev.link -> header
|
# Create new_node by setting it's prev.link -> header
|
||||||
# setting it's next.link -> trailer
|
# setting it's next.link -> trailer
|
||||||
@ -40,11 +45,11 @@ class _DoublyLinkedBase:
|
|||||||
successor._prev = new_node
|
successor._prev = new_node
|
||||||
self._size += 1
|
self._size += 1
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _delete(self, node):
|
def _delete(self, node):
|
||||||
predecessor = node._prev
|
predecessor = node._prev
|
||||||
successor = node._next
|
successor = node._next
|
||||||
|
|
||||||
predecessor._next = successor
|
predecessor._next = successor
|
||||||
successor._prev = predecessor
|
successor._prev = predecessor
|
||||||
self._size -= 1
|
self._size -= 1
|
||||||
@ -53,8 +58,8 @@ class _DoublyLinkedBase:
|
|||||||
del node
|
del node
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
|
|
||||||
class LinkedDeque(_DoublyLinkedBase):
|
class LinkedDeque(_DoublyLinkedBase):
|
||||||
|
|
||||||
def first(self):
|
def first(self):
|
||||||
""" return first element
|
""" return first element
|
||||||
>>> d = LinkedDeque()
|
>>> d = LinkedDeque()
|
||||||
@ -62,11 +67,11 @@ class LinkedDeque(_DoublyLinkedBase):
|
|||||||
'A'
|
'A'
|
||||||
>>> d.add_first('B').first()
|
>>> d.add_first('B').first()
|
||||||
'B'
|
'B'
|
||||||
"""
|
"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise Exception('List is empty')
|
raise Exception("List is empty")
|
||||||
return self._header._next._data
|
return self._header._next._data
|
||||||
|
|
||||||
def last(self):
|
def last(self):
|
||||||
""" return last element
|
""" return last element
|
||||||
>>> d = LinkedDeque()
|
>>> d = LinkedDeque()
|
||||||
@ -76,27 +81,27 @@ class LinkedDeque(_DoublyLinkedBase):
|
|||||||
'B'
|
'B'
|
||||||
"""
|
"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise Exception('List is empty')
|
raise Exception("List is empty")
|
||||||
return self._trailer._prev._data
|
return self._trailer._prev._data
|
||||||
|
|
||||||
### DEque Insert Operations (At the front, At the end) ###
|
### DEque Insert Operations (At the front, At the end) ###
|
||||||
|
|
||||||
def add_first(self, element):
|
def add_first(self, element):
|
||||||
""" insertion in the front
|
""" insertion in the front
|
||||||
>>> LinkedDeque().add_first('AV').first()
|
>>> LinkedDeque().add_first('AV').first()
|
||||||
'AV'
|
'AV'
|
||||||
"""
|
"""
|
||||||
return self._insert(self._header, element, self._header._next)
|
return self._insert(self._header, element, self._header._next)
|
||||||
|
|
||||||
def add_last(self, element):
|
def add_last(self, element):
|
||||||
""" insertion in the end
|
""" insertion in the end
|
||||||
>>> LinkedDeque().add_last('B').last()
|
>>> LinkedDeque().add_last('B').last()
|
||||||
'B'
|
'B'
|
||||||
"""
|
"""
|
||||||
return self._insert(self._trailer._prev, element, self._trailer)
|
return self._insert(self._trailer._prev, element, self._trailer)
|
||||||
|
|
||||||
### DEqueu Remove Operations (At the front, At the end) ###
|
### DEqueu Remove Operations (At the front, At the end) ###
|
||||||
|
|
||||||
def remove_first(self):
|
def remove_first(self):
|
||||||
""" removal from the front
|
""" removal from the front
|
||||||
>>> d = LinkedDeque()
|
>>> d = LinkedDeque()
|
||||||
@ -114,9 +119,9 @@ class LinkedDeque(_DoublyLinkedBase):
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError('remove_first from empty list')
|
raise IndexError("remove_first from empty list")
|
||||||
return self._delete(self._header._next)
|
return self._delete(self._header._next)
|
||||||
|
|
||||||
def remove_last(self):
|
def remove_last(self):
|
||||||
""" removal in the end
|
""" removal in the end
|
||||||
>>> d = LinkedDeque()
|
>>> d = LinkedDeque()
|
||||||
@ -134,5 +139,5 @@ class LinkedDeque(_DoublyLinkedBase):
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if self.is_empty():
|
if self.is_empty():
|
||||||
raise IndexError('remove_first from empty list')
|
raise IndexError("remove_first from empty list")
|
||||||
return self._delete(self._trailer._prev)
|
return self._delete(self._trailer._prev)
|
||||||
|
@ -23,7 +23,7 @@ class SearchProblem:
|
|||||||
|
|
||||||
def score(self) -> int:
|
def score(self) -> int:
|
||||||
"""
|
"""
|
||||||
Returns the output for the function called with current x and y coordinates.
|
Returns the output of the function called with current x and y coordinates.
|
||||||
>>> def test_function(x, y):
|
>>> def test_function(x, y):
|
||||||
... return x + y
|
... return x + y
|
||||||
>>> SearchProblem(0, 0, 1, test_function).score() # 0 + 0 = 0
|
>>> SearchProblem(0, 0, 1, test_function).score() # 0 + 0 = 0
|
||||||
@ -91,7 +91,7 @@ def hill_climbing(
|
|||||||
have any neighbors which can improve the solution.
|
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 minimum else the minimum.
|
find_max: If True, the algorithm should find the maximum else the minimum.
|
||||||
max_x, min_x, max_y, min_y: the maximum and minimum bounds of x and y.
|
max_x, min_x, max_y, min_y: the maximum and minimum bounds of x and y.
|
||||||
visualization: If True, a matplotlib graph is displayed.
|
visualization: If True, a matplotlib graph is displayed.
|
||||||
max_iter: number of times to run the iteration.
|
max_iter: number of times to run the iteration.
|
||||||
|
@ -24,17 +24,18 @@ def bubble_sort(list1):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for i, num in enumerate(list1):
|
for i, num in enumerate(list1):
|
||||||
try:
|
try:
|
||||||
if list1[i+1] < num:
|
if list1[i + 1] < num:
|
||||||
list1[i] = list1[i+1]
|
list1[i] = list1[i + 1]
|
||||||
list1[i+1] = num
|
list1[i + 1] = num
|
||||||
bubble_sort(list1)
|
bubble_sort(list1)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
return list1
|
return list1
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
list1 = [33,99,22,11,66]
|
if __name__ == "__main__":
|
||||||
bubble_sort(list1)
|
list1 = [33, 99, 22, 11, 66]
|
||||||
|
bubble_sort(list1)
|
||||||
print(list1)
|
print(list1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user