I would like help just to know how to read the entries and call the functions
For that purpose you can add the following code below your functions:
# 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)
The method
dictionary helps to translate a 2-letter code to a method that needs to be called. As the arguments to those methods are all in the same order, you can just capture those in a list rest
, and "unpack" that list when passing the arguments. There is just one special thing to take care of. Two methods get a cost
argument, and it must be of number type. Since input is read as string, you need to convert that cost string to a number. The if
statement takes care of this case.
This should answer your question, but it does not finish the exercise. You will still have to debug your code. For instance, currently your code will raise an exception on the example input you have given -- vertex "B" is referenced in IA B A 1
before it is added with IV B
.
Also, you'll need to add the code to produce the output.
But since your question was about capturing the input and calling the functions, I have left the rest for you to tackle.