Question

Python programming question Using an adjacency matrix: Take a txt file and have the program read...

Python programming question

Using an adjacency matrix:

Take a txt file and have the program read it and output the contents into an adjacency matrix.

- The txt file would have the size of the matrix and a list of names.

- Print the filled matrix of the names

- After each iteration, the program will ask, 'continue'?

- Show how to swap names for the next iteration(Program would take an input of two names and swap them)

Example:

input: 2, jerry, joe, bob, ian

The matrix would be 2x2 with those names. For the next iteration, input would be jerry, ian

- jerry and ian would be swapped when the new matrix is printed.

- After each iteration, the program will ask, 'continue'?

- no means program is done, yes means another swap.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

def getIndex(matrix, name):
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            if name == matrix[i][j]:
                return (i,j)
def printMatrix(matrix):
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            print(matrix[i][j],end=" ")
        print()
def main():
    # open file in read mode
    file = open("names.txt","r")
    # it reads file as string
    data = file.read()
    # split data by ,
    data = data.strip('\n').split(",")
    n = int(data[0])
    matrix = []
    index = 1
    # fill matrix
    for i in range(n):
        matrix.append([])
        for j in range(n):
            matrix[i].append(data[index])
            index += 1
    print("Before swapping: ")
    printMatrix(matrix)
    while(True):
        # ask user to enter names
        names = input("\nEnter names to swap: ")
        names = names.split(",")
        # get indices of both names
        ind1 = getIndex(matrix,names[0])
        ind2 = getIndex(matrix,names[1])
        # swap names
        temp = matrix[ind1[0]][ind1[1]]
        matrix[ind1[0]][ind1[1]] = matrix[ind2[0]][ind2[1]]
        matrix[ind2[0]][ind2[1]] = temp
        # print modified matrix
        print("\nAfter swapping: ")
        printMatrix(matrix)
        # ask user to continue
        if(input("\nContinue?(yes/no) ").lower() == 'no'):
            print("Thanks")
            break
main()

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

OUTPUT:-

CODE SCREENSHOT:-

           THANK YOU!!!

Add a comment
Know the answer?
Add Answer to:
Python programming question Using an adjacency matrix: Take a txt file and have the program read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • in csis 152 style python programming language Read the initial employee information from a text file...

    in csis 152 style python programming language Read the initial employee information from a text file and store the employee information into a list of sublists called empRoster. Each sublist should contain [empID, name, payrate,hours] Here's an example of how empRoster will look: 111, "Sally Smith", 10, 401, 1222, "Bob Brown", 10, 42]1 The text file will be structured as shown below, where each line contains the employee id, employee name, payrate and hours. Each entry on the line is...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • IN PYTHON PLEASE...... Process bowlers and their 3 bowling scores. Use my data file below: bowlers2.txt...

    IN PYTHON PLEASE...... Process bowlers and their 3 bowling scores. Use my data file below: bowlers2.txt ( Showed below) And you can also use a while loop to read your file if you prefer. How to average down an array, which you don’t have to do. In this case that would be the game 1 average for all bowlers for example. Find the low average and high average. Start with read the data into arrays/lists and printed out the arrays/lists...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • This is subject of C++ Your program should read the file answers.dat. This file contains the...

    This is subject of C++ Your program should read the file answers.dat. This file contains the name of the student who took the quiz, and his answers for each question. The answers file has the following format: The student name is always the first line. A student name may or may not have last names. For example, both Elvis Presley and Cher took this class. A quiz always has 11 questions. There is one answer per line. Each answer is...

  • CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

    CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The Assignment ? Specifications General Structure your file name and class name on the following pattern: The first three letters of your last name (begin with upper case.). Then the first two letters of your first name (begin with upper case.). Follow this with the name of the program: TryCatch. For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT