這段代碼是圖實作的一些操作,我想要一些幫助來實作樣式中的輸入請求:
example operations:
IV A insert vertex with id==A into the graph;
IA A B P inserts an edge from the vertex of id==A to the vertex of id==B with weight P, if the vertices exist;
RV A removes the vertex of id==A, if it exists, and all edges related to it;
RA A B remove the edge from the vertex of id==A to the vertex of id==B, if it exists;
其中 N 是決定要執行的運算元量的任意數字,在示例的情況下有 4 個,其余是命令:
INPUT EXAMPLE:
4
IV A
IA B A 1
IV B
IA A B 2
OUTPUT: Present the vertex number, number of edges and total sum of weights information
EXAMPLE:
2 vertex, 1 edge and total weight 2.00.
我想知道如何閱讀條目和呼叫函式的幫助,謝謝!!
def add_node(v):
if v in graph:
print(v, "is already part of the graph")
else:
graph[v] = []
def add_edge(v1,v2,cost):
if v1 and v2 not in graph:
print("not part of the graph")
else:
list1 = [v2,cost]
list2 = [v1,cost]
graph[v1].append(list1)
graph[v2].append(list2)
def delete_node(v):
if v not in graph:
print(v,"not part of the graph")
else:
graph.pop(v)
for i in graph:
list1 = graph[i]
for j in list1:
if v == j[0]:
list1.remove(j)
def delete_edge(v1,v2,cost):
if v1 and v2 not in graph:
print("not part of the graph")
else:
temp = [v1, cost]
temp1 = [v2, cost]
if temp1 in graph[v1]:
graph[v1].remove(temp1)
graph[v2].remove(temp)
uj5u.com熱心網友回復:
我想知道如何閱讀條目和呼叫函式的幫助
為此,您可以在函式下方添加以下代碼:
# main program
method = {
"IV": add_node,
"IA": add_edge,
"RV": delete_node,
"RA": delete_edge
}
numinputlines = int(input())
for _ in range(numinputlines):
instruction, *rest = input().split()
if len(rest) == 3: # There is a cost:
rest[-1] = int(rest[-1])
method[instruction](*rest)
該method詞典能夠幫助到2個字母的代碼轉換到需要被呼叫的方法。由于這些方法的引數都按相同的順序排列,因此您可以將它們捕獲在 list 中rest,并在傳遞引數時“解壓縮”該串列。只有一件特別的事情需要照顧。兩個方法獲取一個cost引數,并且它必須是數字型別。由于輸入被讀取為字串,因此您需要將該成本字串轉換為數字。該if宣告處理了這種情況。
這應該可以回答你的問題,但它并沒有完成練習。您仍然需要除錯您的代碼。例如,目前您的代碼將在您提供的示例輸入上引發例外——頂點“B”在IA B A 1添加之前被參考IV B。
此外,您還需要添加代碼以生成輸出。
但是由于您的問題是關于捕獲輸入和呼叫函式,所以我將其余的留給您解決。
uj5u.com熱心網友回復:
由于幾乎沒有錯誤,我對圖形函式做了很少的修改。雖然它不是圖的最佳實作,但我不想做出重大改變來堅持你自己的實作。
def add_node(v):
if v in graph.keys() :
print(v, "is already part of the graph")
else:
graph[v] = []
def add_edge(v1,v2,cost):
keys = graph.keys()
if v1 not in keys or v2 not in keys:
print("not part of the graph")
else:
list1 = [v2,cost]
list2 = [v1,cost]
graph[v1].append(list1)
graph[v2].append(list2)
def delete_node(v):
if v not in graph.keys():
print(v,"not part of the graph")
else:
graph.pop(v)
for i in graph.keys():
list1 = graph[i]
for j in list1:
if v == j[0]:
list1.remove(j)
def delete_edge(v1,v2):
keys = graph.keys()
if v1 not in keys or v2 not in keys:
print("not part of the graph")
else:
graph[v1] = [x for x in graph[v1] if x[0] != v2]
graph[v2] = [x for x in graph[v2] if x[0] != v1]
graph = {}
no_operations = int(input("Enter number of operations: "))
for opt in range(no_operations):
command = input("Enter command: ").split()
if(command[0] == 'IV'):
A = command[1]
add_node(A)
elif(command[0] == 'IA'):
B = command[1]
A = command[2]
P = int(command[3])
add_edge(A, B, P)
elif(command[0] == 'RV'):
B = command[1]
delete_node(B)
elif(command[0] == 'RA'):
A = command[1]
B = command[2]
delete_edge(A, B)
else:
print("Invalid command")
print(graph)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/327568.html
標籤:Python 算法 python-2.7 蟒蛇请求
上一篇:理解連續子陣列求和問題
