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

  • Home
  • SEARCH
  • 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 8229863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:56:36+00:00 2026-06-07T16:56:36+00:00

I am trying to print the distinct nodes in a graph when given a

  • 0

I am trying to print the distinct nodes in a graph when given a list of edges with the following:

def find_nodes(graph):
    # get the distinct nodes from the edges
    nodes = []
    l = len(graph)
    for i in range(l):
        edge = graph[i]
        n1 = edge[0]
        n2 = edge[1]
        if n1 not in nodes:
            nodes.append(n1)
        if n2 not in nodes:
            nodes.append(n2)
    return nodes

graph = ((1,2),(2,3), (3,1))
print find_nodes(graph)

But I only get (1,2) how am i missing the 3?

  • 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-07T16:56:38+00:00Added an answer on June 7, 2026 at 4:56 pm

    When I look at the text you’ve inserted, it looks like you’re mixing tabs and spaces as left-hand whitespace:

    This can be confirmed by looking at the repr of each line:

    '    def find_nodes(graph):'
    '        # get the distinct nodes from the edges'
    '        nodes = []'
    '        l = len(graph)'
    '        for i in range(l):'
    '        \tedge = graph[i]'
    '        \tn1 = edge[0]'
    '        \tn2 = edge[1]'
    '        \tif n1 not in nodes:'
    '        \t\tnodes.append(n1)'
    '        \tif n2 not in nodes:'
    '        \t\tnodes.append(n2)'
    '    \treturn nodes'
    

    This can result in lines not being indented to the level you think they are. Here’s what I get as a result from copying and pasting your input into a console:

    >>> s = """
    ...     def find_nodes(graph):
    ...         # get the distinct nodes from the edges
    ...         nodes = []
    ...         l = len(graph)
    ...         for i in range(l):
    ...             edge = graph[i]
    ...             n1 = edge[0]
    ...             n2 = edge[1]
    ...             if n1 not in nodes:
    ...                     nodes.append(n1)
    ...             if n2 not in nodes:
    ...                     nodes.append(n2)
    ...             return nodes
    ...     
    ...     graph = ((1,2),(2,3), (3,1))
    ...     print find_nodes(graph)
    ... 
    ... """
    

    It looks to me like the return nodes line will be executed too early. Write the code into a file and use the python -tt option to check for whitespace problems.

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

Sidebar

Related Questions

while trying to print a greek character 'φ' I get the following character printed
I'm trying to print out a list of products on a page. This is
I'm trying to print the list created by the functions in this class- what
I am trying to update a value in a table I get from an
I'd like to iterate over a list of distinct fields in a given document.
So what I'm trying to do is select all the distinct months from my
I'm trying to print a UITableView with two distinct types of UITableViewCells. Empty table
I'm trying to return a set of distinct values from a DataTable using LINQ.
Im trying print Excel file data on a page. To do it i used
Im trying to print the realtime output based on user input for a search.

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.