Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:18:15+00:00 2026-06-15T18:18:15+00:00

Hey I am writing a function that takes a matrix input such as the

  • 0

Hey I am writing a function that takes a matrix input such as the one below and returns its inverse, where all the 1s are changed to 0s and all the 0s changed to 1s, while keeping the diagonal from top left to bottom right 0s.

An example input:

g1 = [[0, 1, 1, 0],
     [1, 0, 0, 1],
     [1, 0, 0, 1],
     [0, 1, 1, 0]]

the function should output this:

g1 = [[0, 0, 0, 1],
     [0, 0, 1, 0],
     [0, 1, 0, 0],
     [1, 0, 0, 0]]

When I run the program, it throws a “list index out of range” error. I’m sure this is because the loops I have set up are trying to access values that do not exist, but how do I allow an input of unknown row and column size? I only know how to do this with a single list, but a list of lists? Here is the function, not including the test function that calls it:

def inverse_graph(graph):
    # take in graph
    # change all zeros to ones and ones to zeros

    r, c = 0, 0 # row, column equal zero

    while (graph[r][c] == 0 or graph[r][c] == 1): # while the current row has a value.
        while (graph[r][c] == 0 or graph[r][c] == 1): # while the current column has a value
            if (graph[r][c] == 0):
                graph[r][c] = 1
            elif (graph[r][c] == 1):
                graph[r][c] = 0
            c+=1
        c=0
        r+=1

    c=0
    r=0

    # sets diagonal to zeros

    while (g1[r][c] == 0 or g1[r][c] == 1):
        g1[r][c]=0
        c+=1
        r+=1

    return graph
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:18:16+00:00Added an answer on June 15, 2026 at 6:18 pm

    This doesn’t directly answer your question, but I want to point out that in Python you can often reduce and sometimes eliminate the need to use indexing by using a
        for <element> in <container>:
    statement. By use it along with the built-in enumerate() function, it’s possible to get both the index and the corresponding element
        for <index>,<element> in enumerate(<container>):

    Applying them to your problem would allow something like this:

    g1 = [[0, 1, 1, 0],
          [1, 0, 0, 1],
          [1, 0, 0, 1],
          [0, 1, 1, 0]]
    
    def inverse_graph(graph):
        """ invert zeroes and ones in a square graph
            but force diagonal elements to be zero
        """
        for i,row in enumerate(graph):
            for j,cell in enumerate(row):
                row[j] = 0 if cell or i == j else 1
        return graph
    
    print(g1)
    print(inverse_graph(g1))
    

    Output:

    [[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]]
    [[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]
    

    Which is simpler and clearly works. Another point is that since you’re applying the function to a mutable (changeable) container, a list-of-lists, there’s really no need to return the container because it is being changed in-place. It’s not wrong to do so because it can make using the function easier, but it’s something you may not have realized.

    You could shorten the function a little bit more and eliminate indexing altogether by using something called a list comprehension:

    def inverse_graph(graph):
        return [[0 if cell or i == j else 1
                    for j,cell in enumerate(row)]
                        for i,row in enumerate(graph)]
    

    Because of the way they work, this version doesn’t change the graph in-place, but instead creates and returns a new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I'm writing a program that takes in a string representation of a
Hey I am writing a function that handles favorites within my app, as well
Hey I thought about writing a function that prompts me in certain situations (perl
Hey guys, I'm writing a word wrap function to format console text in C++.
Hey--I'm writing a basic Rails app that uses the digg API. I'm trying to
Hey guys, I am writing an application that retrieves the Group Names and access
Hey guys, Im working with wpf 3d, im currently writing a program that will
Hey guys, I'm writing a Firefox plugin and using the following function: var obj
Hey all, I'm writing an app to handle registration for athletic events. Some of
Hey there. I am writing some PHP that needs to check to see if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.