My code below. I am getting an error when trying to create my adjacency matrix. i dont know what i am doing wrong def readMatrix(inputfilename):     ''' Returns a two-dimentional array created from the data in the given file.              Pre:  'inputfilename' is the name of a text file whose first row contains the             number of vertices in a graph and whose subsequent rows contain the rows of             the adjacency matrix of the graph. ''

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

text file

8
0 1 2 3 100 100 100 100
1 0 2 100 3 4 100 100
2 2 0 4 4 100 5 100
3 100 4 0 100 100 4 100
100 3 4 100 0 3 3 3
100 4 100 100 3 0 100 1
100 100 5 4 3 100 0 2
100 100 100 100 3 1 2 0

My code below. I am getting an error when trying to create my adjacency matrix. i dont know what i am doing wrong

def readMatrix(inputfilename):
    ''' Returns a two-dimentional array created from the data in the given file.
    
        Pre:  'inputfilename' is the name of a text file whose first row contains the
            number of vertices in a graph and whose subsequent rows contain the rows of
            the adjacency matrix of the graph. '''
    
    # Open the file
    f = open(inputfilename, 'r')

    # Read the number of vertices from the first line of the file
    n = int(f.readline().strip())

    # Read the rest of the file stripping off the newline characters and splitting it into
    # a list of intger values
    rest = f.read().strip().split()
    

    # Create the adjacency matrix
    adjMat = []
    adjrow=[]
    n=rest.split("\n")
    a=[]
    for i in n:
        a.append(i.split(" "))
    for i in a:
        
        adjrow=[]
        for j in i:
            if j=="100":
                adjrow.append("INF")
            else:
                adjrow.append(int(j))
        adjMat.append(adjrow)      
    
    # Return the matrix
    return adjMat

testFile = input("Enter the name of the input file")

graphMatrix = readMatrix(testFile)
graphMatrix

def readMatrix(inputfilename):
Returns a two-dimentional array created from the data in the given file.
Pre: 'inputfilename' is the name of a text file whose first row contains the
number of vertices in a graph and whose subsequent rows contain the rows of
the adjacency matrix of the graph.
# Open the file
open (inputfilename, 'r')
+ =
# Read the number of vertices from the first line of the file
int(f.readline(().strip())
n =
# Read the rest of the file stripping off the newline characters and splitting it into
# a list of intger values
rest =
f.read().strip(). split()
# Create the adjacency matrix
adjMat = []
adjrow=[]
n=rest.split("\n")
a=[]
for i in n:
a.append(i.split(" "))
for i in a:
adjrow=[]
for j in i:
if j=="100":
adjrow.append ("INF")
else:
adjrow.append (int(j))
adjMat.append (adjrow)
# Return the matrix
return adjMat
Test your function.
testFile =
input("Enter the name of the input file")
Enter the name of the input fileinputfilename.txt
graphMatrix = readMatrix(testFile)
graphMatrix
Transcribed Image Text:def readMatrix(inputfilename): Returns a two-dimentional array created from the data in the given file. Pre: 'inputfilename' is the name of a text file whose first row contains the number of vertices in a graph and whose subsequent rows contain the rows of the adjacency matrix of the graph. # Open the file open (inputfilename, 'r') + = # Read the number of vertices from the first line of the file int(f.readline(().strip()) n = # Read the rest of the file stripping off the newline characters and splitting it into # a list of intger values rest = f.read().strip(). split() # Create the adjacency matrix adjMat = [] adjrow=[] n=rest.split("\n") a=[] for i in n: a.append(i.split(" ")) for i in a: adjrow=[] for j in i: if j=="100": adjrow.append ("INF") else: adjrow.append (int(j)) adjMat.append (adjrow) # Return the matrix return adjMat Test your function. testFile = input("Enter the name of the input file") Enter the name of the input fileinputfilename.txt graphMatrix = readMatrix(testFile) graphMatrix
AttributeError
Traceback (most recent call last)
C: \Users\WAGADE~1\AppData\Local\Temp/ipykernel_11140/1175540795.py in <module>
----> 1 graphMatrix = readMatrix(testFile)
2 graphMatrix
C: \Users\WAGADE~1\AppData\Local\Temp/ipykernel_11140/1370766450.py in readMatrix(inputfilename)
adjMat = []
adjrow=[]
n=rest.split("\n")
a=[]
for i in n:
20
21
---> 22
23
24
AttributeError: 'list' object has no attribute 'split'
Transcribed Image Text:AttributeError Traceback (most recent call last) C: \Users\WAGADE~1\AppData\Local\Temp/ipykernel_11140/1175540795.py in <module> ----> 1 graphMatrix = readMatrix(testFile) 2 graphMatrix C: \Users\WAGADE~1\AppData\Local\Temp/ipykernel_11140/1370766450.py in readMatrix(inputfilename) adjMat = [] adjrow=[] n=rest.split("\n") a=[] for i in n: 20 21 ---> 22 23 24 AttributeError: 'list' object has no attribute 'split'
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY