Compare commits

...

4 Commits

Author SHA1 Message Date
miltonbhowmick
eee84fcd04 Changed doctest for ruff failed 2023-08-08 14:39:05 +06:00
miltonbhowmick
129176a4f3 Changed doctest examples 2023-08-08 14:16:27 +06:00
miltonbhowmick
85d33bea6c Moved mutable class attributes as per concern from repository test merge 2023-08-08 11:51:06 +06:00
miltonbhowmick
60a8d009eb Fixed some unsed var error and long line issue from github merge test 2023-08-08 11:46:24 +06:00

View File

@ -1,82 +1,79 @@
import heapq
import doctest
# diagonal clockwise
dxy1 = [
(1, 1),
(1, 0),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
(0, 1),
]
# diagonal anti-clockwise
dxy2 = [
(-1, -1),
(-1, 0),
(0, -1),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(-1, 1),
]
# start point and end point on same row and column right side
dxy3 = [
(0, -1),
(-1, -1),
(-1, 0),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(-1, 1),
]
# start point and end point on same row and column left side
dxy4 = [
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
# start point and end point on same column and row down side
dxy5 = [
(1, 0),
(0, 1),
(1, 1),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
# start point and end point on same column and row up side
dxy6 = [
(0, -1),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
class UniformCostSearch:
def __init__(
self, current: list[int], final: list[int], grid: list[list[int]]
) -> None:
def __init__(self, grid: list[list[int]]) -> None:
self.m = len(grid[0])
self.n = len(grid)
# diagonal clockwise
dxy1 = [
(1, 1),
(1, 0),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
(0, 1),
]
# diagonal anti-clockwise
dxy2 = [
(-1, -1),
(-1, 0),
(0, -1),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(-1, 1),
]
# start point and end point on same row and column right side
dxy3 = [
(0, -1),
(-1, -1),
(-1, 0),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(-1, 1),
]
# start point and end point on same row and column left side
dxy4 = [
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
# start point and end point on same column and row down side
dxy5 = [
(1, 0),
(0, 1),
(1, 1),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
# start point and end point on same column and row up side
dxy6 = [
(0, -1),
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
def get_shortest_path(
self,
start: list[int],
@ -86,8 +83,12 @@ class UniformCostSearch:
) -> 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]]
>>> start = [0,0]
>>> end = [1, 2]
>>> dist = [[1,0],[0,2]]
>>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
>>> get_shortest_path(start,end,dist,dxy)
[[0,0],[1,1]]
"""
shortest_path = []
curr_node = end
@ -120,8 +121,14 @@ class UniformCostSearch:
) -> 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]]
>>> current = [0, 0]
>>> final = [[1, 1]]
>>> grid = [[0,0],[0,0]]
>>> prev = [[None, None],[None, None]]
>>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
>>> goal_answer = [1000000, 100000]
>>> ucs(current,final,grid,dxy,prev,goal_answer)
[[0,2],[1,1]]
"""
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
@ -175,25 +182,28 @@ class UniformCostSearch:
) -> 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]]
>>> start_point = [0, 0]
>>> end_point = [1, 1]
>>> grid = [[0,0],[0,0]]
>>> your_algorithm(start_point, end_point, grid)
[[0,0],[1,1]]
"""
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:
dxy = self.dxy5
dxy = dxy5
elif start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] > 0:
dxy = self.dxy6
dxy = dxy6
elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] < 0:
dxy = self.dxy4
dxy = dxy4
elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] > 0:
dxy = self.dxy3
dxy = dxy3
elif start_point[0] - end_point[0] > 0:
dxy = self.dxy2
dxy = dxy2
elif start_point[0] - end_point[0] < 0:
dxy = self.dxy1
dxy = dxy1
goal_answer = []
for cell in end_point:
for _ in range(0, len(end_point)):
goal_answer.append(10**8)
path = self.ucs(start_point, [end_point], grid, prev, dxy, goal_answer)
return path
@ -202,12 +212,10 @@ class UniformCostSearch:
def run() -> None:
"""
Return None. Its just running the UCS algorithm class.
>>> run(2, 2)
>>> run()
None
"""
executed_object = UniformCostSearch(
[0, 7],
[19, 17],
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],