mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
grid = [[0, 1, 0, 0, 0, 0],
|
||||
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
|
||||
[0, 1, 0, 0, 0, 0],
|
||||
@@ -14,13 +12,13 @@ heuristic = [[9, 8, 7, 6, 5, 4],
|
||||
[5, 4, 3, 2, 1, 0]]'''
|
||||
|
||||
init = [0, 0]
|
||||
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
|
||||
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
|
||||
cost = 1
|
||||
|
||||
#the cost map which pushes the path closer to the goal
|
||||
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
|
||||
for i in range(len(grid)):
|
||||
for j in range(len(grid[0])):
|
||||
for i in range(len(grid)):
|
||||
for j in range(len(grid[0])):
|
||||
heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
|
||||
if grid[i][j] == 1:
|
||||
heuristic[i][j] = 99 #added extra penalty in the heuristic map
|
||||
@@ -62,7 +60,7 @@ def search(grid,init,goal,cost,heuristic):
|
||||
g = next[1]
|
||||
f = next[0]
|
||||
|
||||
|
||||
|
||||
if x == goal[0] and y == goal[1]:
|
||||
found = True
|
||||
else:
|
||||
@@ -93,10 +91,10 @@ def search(grid,init,goal,cost,heuristic):
|
||||
print("ACTION MAP")
|
||||
for i in range(len(action)):
|
||||
print(action[i])
|
||||
|
||||
|
||||
return path
|
||||
|
||||
|
||||
a = search(grid,init,goal,cost,heuristic)
|
||||
for i in range(len(a)):
|
||||
print(a[i])
|
||||
print(a[i])
|
||||
|
||||
|
||||
@@ -1,23 +1,10 @@
|
||||
from __future__ import print_function
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Accept No. of Nodes and edges
|
||||
n, m = map(int, raw_input().split(" "))
|
||||
n, m = map(int, input().split(" "))
|
||||
|
||||
# Initialising Dictionary of edges
|
||||
g = {}
|
||||
for i in xrange(n):
|
||||
for i in range(n):
|
||||
g[i + 1] = []
|
||||
|
||||
"""
|
||||
@@ -25,8 +12,8 @@ if __name__ == "__main__":
|
||||
Accepting edges of Unweighted Directed Graphs
|
||||
----------------------------------------------------------------------------
|
||||
"""
|
||||
for _ in xrange(m):
|
||||
x, y = map(int, raw_input().strip().split(" "))
|
||||
for _ in range(m):
|
||||
x, y = map(int, input().strip().split(" "))
|
||||
g[x].append(y)
|
||||
|
||||
"""
|
||||
@@ -34,8 +21,8 @@ if __name__ == "__main__":
|
||||
Accepting edges of Unweighted Undirected Graphs
|
||||
----------------------------------------------------------------------------
|
||||
"""
|
||||
for _ in xrange(m):
|
||||
x, y = map(int, raw_input().strip().split(" "))
|
||||
for _ in range(m):
|
||||
x, y = map(int, input().strip().split(" "))
|
||||
g[x].append(y)
|
||||
g[y].append(x)
|
||||
|
||||
@@ -44,8 +31,8 @@ if __name__ == "__main__":
|
||||
Accepting edges of Weighted Undirected Graphs
|
||||
----------------------------------------------------------------------------
|
||||
"""
|
||||
for _ in xrange(m):
|
||||
x, y, r = map(int, raw_input().strip().split(" "))
|
||||
for _ in range(m):
|
||||
x, y, r = map(int, input().strip().split(" "))
|
||||
g[x].append([y, r])
|
||||
g[y].append([x, r])
|
||||
|
||||
@@ -170,10 +157,10 @@ def topo(G, ind=None, Q=[1]):
|
||||
|
||||
|
||||
def adjm():
|
||||
n = raw_input().strip()
|
||||
n = input().strip()
|
||||
a = []
|
||||
for i in xrange(n):
|
||||
a.append(map(int, raw_input().strip().split()))
|
||||
for i in range(n):
|
||||
a.append(map(int, input().strip().split()))
|
||||
return a, n
|
||||
|
||||
|
||||
@@ -193,10 +180,10 @@ def adjm():
|
||||
def floy(A_and_n):
|
||||
(A, n) = A_and_n
|
||||
dist = list(A)
|
||||
path = [[0] * n for i in xrange(n)]
|
||||
for k in xrange(n):
|
||||
for i in xrange(n):
|
||||
for j in xrange(n):
|
||||
path = [[0] * n for i in range(n)]
|
||||
for k in range(n):
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
if dist[i][j] > dist[i][k] + dist[k][j]:
|
||||
dist[i][j] = dist[i][k] + dist[k][j]
|
||||
path[i][k] = k
|
||||
@@ -245,10 +232,10 @@ def prim(G, s):
|
||||
|
||||
|
||||
def edglist():
|
||||
n, m = map(int, raw_input().split(" "))
|
||||
n, m = map(int, input().split(" "))
|
||||
l = []
|
||||
for i in xrange(m):
|
||||
l.append(map(int, raw_input().split(' ')))
|
||||
for i in range(m):
|
||||
l.append(map(int, input().split(' ')))
|
||||
return l, n
|
||||
|
||||
|
||||
@@ -272,10 +259,10 @@ def krusk(E_and_n):
|
||||
break
|
||||
print(s)
|
||||
x = E.pop()
|
||||
for i in xrange(len(s)):
|
||||
for i in range(len(s)):
|
||||
if x[0] in s[i]:
|
||||
break
|
||||
for j in xrange(len(s)):
|
||||
for j in range(len(s)):
|
||||
if x[1] in s[j]:
|
||||
if i == j:
|
||||
break
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
def printDist(dist, V):
|
||||
print("\nVertex Distance")
|
||||
for i in range(V):
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
""" Author: OMKAR PATHAK """
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
class Graph():
|
||||
def __init__(self):
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
# encoding=utf8
|
||||
|
||||
""" Author: OMKAR PATHAK """
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
class Graph():
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
def printDist(dist, V):
|
||||
print("\nVertex Distance")
|
||||
for i in range(V):
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
# Author: Shubham Malik
|
||||
# References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
||||
|
||||
from __future__ import print_function
|
||||
import math
|
||||
import sys
|
||||
# For storing the vertex set to retreive node with the lowest distance
|
||||
|
||||
@@ -12,7 +12,6 @@ Constraints
|
||||
Note: The tree input will be such that it can always be decomposed into
|
||||
components containing an even number of nodes.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
# pylint: disable=invalid-name
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
# encoding=utf8
|
||||
|
||||
from __future__ import print_function
|
||||
# Author: OMKAR PATHAK
|
||||
|
||||
# We can use Python's dictionary for constructing the graph.
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
class Graph:
|
||||
|
||||
def __init__(self, vertex):
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
have negative edge weights.
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def _print_dist(dist, v):
|
||||
print("\nThe shortest path matrix using Floyd Warshall algorithm\n")
|
||||
@@ -34,9 +32,9 @@ def floyd_warshall(graph, v):
|
||||
4. The above is repeated for each vertex k in the graph.
|
||||
5. Whenever distance[i][j] is given a new minimum value, next vertex[i][j] is updated to the next vertex[i][k].
|
||||
"""
|
||||
|
||||
|
||||
dist=[[float('inf') for _ in range(v)] for _ in range(v)]
|
||||
|
||||
|
||||
for i in range(v):
|
||||
for j in range(v):
|
||||
dist[i][j] = graph[i][j]
|
||||
@@ -53,7 +51,7 @@ def floyd_warshall(graph, v):
|
||||
_print_dist(dist, v)
|
||||
return dist, v
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__== '__main__':
|
||||
v = int(input("Enter number of vertices: "))
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
if __name__ == "__main__":
|
||||
num_nodes, num_edges = list(map(int, input().strip().split()))
|
||||
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
from __future__ import print_function
|
||||
import heapq
|
||||
import numpy as np
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
|
||||
|
||||
class PriorityQueue:
|
||||
def __init__(self):
|
||||
@@ -96,7 +90,7 @@ def do_something(back_pointer, goal, start):
|
||||
grid[(n-1)][0] = "-"
|
||||
|
||||
|
||||
for i in xrange(n):
|
||||
for i in range(n):
|
||||
for j in range(n):
|
||||
if (i, j) == (0, n-1):
|
||||
print(grid[i][j], end=' ')
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def dfs(u):
|
||||
global g, r, scc, component, visit, stack
|
||||
if visit[u]: return
|
||||
|
||||
Reference in New Issue
Block a user