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 8535451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:26:03+00:00 2026-06-11T10:26:03+00:00

I am trying to solve a problem on Udacity described as follows: # Find

  • 0

I am trying to solve a problem on Udacity described as follows:

# Find Eulerian Tour
#
# Write a function that takes in a graph
# represented as a list of tuples
# and return a list of nodes that
# you would follow on an Eulerian Tour
#
# For example, if the input graph was
# [(1, 2), (2, 3), (3, 1)]
# A possible Eulerian tour would be [1, 2, 3, 1]

I came up with the following solution, which, while not as elegant as some of the recursive algorithms, does seem to work within my test case.

def find_eulerian_tour(graph):
    tour = []

    start_vertex = graph[0][0]
    tour.append(start_vertex)

    while len(graph) > 0:
        current_vertex = tour[len(tour) - 1]
        for edge in graph:
            if current_vertex in edge:
                if edge[0] == current_vertex:
                    current_vertex = edge[1]
                elif edge[1] == current_vertex:
                    current_vertex = edge[0]
                else:
                    # Edit to account for case no tour is possible
                    return False

                graph.remove(edge)
                tour.append(current_vertex)
                break
    return tour

graph = [(1, 2), (2, 3), (3, 1)]
print find_eulerian_tour(graph)

>> [1, 2, 3, 1]

However, when submitting this, I get rejected by the grader. I am doing something wrong? I can’t see any errors.

  • 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-11T10:26:05+00:00Added an answer on June 11, 2026 at 10:26 am

    Here’s a valid case where your algorithm fails:

    graph = [(1, 2), (2, 3), (3, 1), (3, 4), (4, 3)]
    

    Use the power of print to find out what happens to graph and current_vertex.

    Another hint: Move the else down so that it belongs to the for and is executed when the for loop is not broken. As it is now, it can never be executed. After that correction, the algorithm still fails, of course.

    The algorithm still fails, of course.

    The algorithm still fails, of course.

    Please, don’t comment stating that the code doesn’t work. It doesn’t. The algorithm still fails, even if the code below does what the OP had in mind. The point was to show that the OP’s algorithm is wrong, which the OP couldn’t determine. For that, a correct implementation of OP’s algorithm is needed (see below). A correct implementation of a wrong algorithm is still not a correct solution.

    I’m sorry to make this answer worse by writing all these lengthy explanations, but people continue to complain that the code doesn’t work (of course, the point was to show that it is wrong). They also downvote this answer, probably because they expect to be able to copy the code as a solution. But this is not the point, the point is to show to the OP that there is an error in his algorithm.

    The code below does not find eulerian tours. Look elsewhere to copy code for passing your assingments!

    def find_eulerian_tour(graph):
        tour = []
    
        current_vertex = graph[0][0]
        tour.append(current_vertex)
    
        while len(graph) > 0:
            print(graph, current_vertex)
            for edge in graph:
                if current_vertex in edge:
                    if edge[0] == current_vertex:
                        current_vertex = edge[1]
                    else:
                        current_vertex = edge[0]
    
                    graph.remove(edge)
                    tour.append(current_vertex)
                    break
            else:
                # Edit to account for case no tour is possible
                return False
        return tour
    
    graph = [(1, 2), (2, 3), (3, 1), (3, 4), (4, 3)]
    print(find_eulerian_tour(graph))
    

    Output:

    [(1, 2), (2, 3), (3, 1), (3, 4), (4, 3)] 1
    [(2, 3), (3, 1), (3, 4), (4, 3)] 2
    [(3, 1), (3, 4), (4, 3)] 3
    [(3, 4), (4, 3)] 1
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve a problem that, unfortunately, goes beyond my capacity. I have
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
The problem I am trying to solve is that I want to check all
I am trying to solve a problem that involves basically implementing a logical AND
I am trying to solve this problem in Python. Noting that only the first
I'm trying to solve a problem that I outlined in this question . At
I'm currently trying to solve a problem where I need to find the position
I was trying to solve a problem that required the maximum value of a
Trying to solve this problem: I have the following set of divs that when
Trying to solve that problem, but no luck for hours... I have var screen1

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.