mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Added unit test and parameters with type
This commit is contained in:
parent
1e5066a4b9
commit
7bd35f5178
@ -1,9 +1,11 @@
|
||||
from typing import List
|
||||
import heapq
|
||||
import doctest
|
||||
|
||||
|
||||
class UniformCostSearch:
|
||||
def __init__(self, current: list, final: list, grid: list[list]) -> None:
|
||||
def __init__(
|
||||
self, current: list[int], final: list[int], grid: list[list[int]]
|
||||
) -> None:
|
||||
self.m = len(grid[0])
|
||||
self.n = len(grid)
|
||||
|
||||
@ -76,8 +78,17 @@ class UniformCostSearch:
|
||||
]
|
||||
|
||||
def get_shortest_path(
|
||||
self, start: list, end: list, dist: list[list], dxy: list[tuple]
|
||||
) -> list:
|
||||
self,
|
||||
start: list[int],
|
||||
end: list[int],
|
||||
dist: list[list[int]],
|
||||
dxy: list[tuple],
|
||||
) -> list[list[int]]:
|
||||
"""
|
||||
Return 2D list where optimal path is stored.
|
||||
>>> get_shortest_path([0, 2],[2, 2], [['inf','inf',1],['inf,2,2],['inf',0,3]], [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)])
|
||||
[[0,2],[1,2],[2,2]]
|
||||
"""
|
||||
shortest_path = []
|
||||
curr_node = end
|
||||
while curr_node != start:
|
||||
@ -100,13 +111,19 @@ class UniformCostSearch:
|
||||
|
||||
def ucs(
|
||||
self,
|
||||
current: list,
|
||||
final: list[list],
|
||||
grid: list[list],
|
||||
prev: list[list],
|
||||
current: list[int],
|
||||
final: list[list[int]],
|
||||
grid: list[list[int]],
|
||||
prev: list[list[int]],
|
||||
dxy: list[tuple],
|
||||
goal_answer: list,
|
||||
) -> list:
|
||||
) -> list[list[int]]:
|
||||
"""
|
||||
Return 2D list where optimal path is stored.
|
||||
>>> ucs([0, 2],[[1,2],[2, 2]], [[0,0,0],[0,0,0],[0,0,0]], [[None, None, None], [None, None, None],[None, None, None]], [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)], [1000000, 100000])
|
||||
[[0,2],[1,2],[2,2]]
|
||||
"""
|
||||
|
||||
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
||||
visited = [[0 for _ in range(self.m)] for _ in range(self.n)]
|
||||
|
||||
@ -154,8 +171,13 @@ class UniformCostSearch:
|
||||
heapq.heappush(heap, (new_dist, x + dx, y + dy))
|
||||
|
||||
def your_algorithm(
|
||||
self, start_point: list, end_point: list, grid: list[list]
|
||||
) -> list[int]:
|
||||
self, start_point: list[int], end_point: list[int], grid: list[list[int]]
|
||||
) -> list[list[int]]:
|
||||
"""
|
||||
Return 2D list where optimal path is stored.
|
||||
>>> your_algorithm([0, 2],[2, 2], [[0,0,0],[0,0,0],[0,0,0]])
|
||||
[[0,2],[1,2],[2,2]]
|
||||
"""
|
||||
prev = [[None for _ in range(self.m)] for _ in range(self.n)]
|
||||
dxy = []
|
||||
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
||||
@ -178,6 +200,11 @@ class UniformCostSearch:
|
||||
|
||||
|
||||
def run() -> None:
|
||||
"""
|
||||
Return None. Its just running the UCS algorithm class.
|
||||
>>> run(2, 2)
|
||||
None
|
||||
"""
|
||||
executed_object = UniformCostSearch(
|
||||
[0, 7],
|
||||
[19, 17],
|
||||
|
Loading…
x
Reference in New Issue
Block a user