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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:04:25+00:00 2026-05-14T03:04:25+00:00

I’ve been working on this for a couple of days now without success. Basically,

  • 0

I’ve been working on this for a couple of days now without success. Basically, I have a bunch of nodes arranged in a 2D matrix. Every node has four neighbors, except for the nodes on the sides and corners of the matrix, which have 3 and 2 neighbors, respectively. Imagine a bunch of square cards laid out side by side in a rectangular area–the project is actually simulating a sort of card/board game.

Each node may or may not be connected to the nodes around it. Each node has a function (get_connections()), that returns the nodes immediately around it that it is connected to (so anywhere from 0 to 4 nodes are returned). Each node also has an “index” property, that contains it’s position on the board matrix (eg ‘1, 4’ -> row 1, col 4). What I am trying to do is find the longest non-repeating path of connected nodes given a particular “start” node.

I’ve uploaded a couple of images that should give a good idea of what I’m trying to do:

www.necessarygames.com/junk/10-days-problem-01.jpg
(source: necessarygames.com)

www.necessarygames.com/junk/10-days-problem-02.jpg
(source: necessarygames.com)

In both images, the highlighted red cards are supposedly the longest path of connected cards containing the most upper-left card. However, you can see in both images that a couple of cards that should be in the path have been left out (Romania and Maldova in the first image, Greece and Turkey in the second)

Here’s the recursive function that I am using currently to find the longest path, given a starting node/card:

def get_longest_trip(self, board, processed_connections = list(), 
                     processed_countries = list()):
    #Append this country to the processed countries list,
    #so we don't re-double over it
    processed_countries.append(self)
    possible_trips = dict()
    if self.get_connections(board):
        for i, card in enumerate(self.get_connections(board)):
            if card not in processed_countries:
                processed_connections.append((self, card))
                possible_trips[i] = card.get_longest_trip(board, 
                                                          processed_connections, 
                                                          processed_countries)
        if possible_trips:       
            longest_trip = []
            for i, trip in possible_trips.iteritems(): 
                trip_length = len(trip)
                if trip_length > len(longest_trip):
                    longest_trip = trip
            longest_trip.append(self)         
            return longest_trip
        else:
            print
            card_list = []
            card_list.append(self)
            return card_list
    else:
        #If no connections from start_card, just return the start card 
        #as the longest trip
        card_list = []
        card_list.append(board.start_card)
        return card_list

The problem here has to do with the processed_countries list: if you look at my first screenshot, you can see that what has happened is that when Ukraine came around, it looked at its two possible choices for longest path (Maldova-Romania, or Turkey, Bulgaria), saw that they were both equal, and chose one indiscriminantly. Now when Hungary comes around, it can’t attempt to make a path through Romania (where the longest path would actually be), because Romania has been added to the processed_countries list by Ukraine.

Any help on this is EXTREMELY appreciated. If you can find me a solution to this, recursive or not, I’d be happy to donate some $$ to you.

I’ve uploaded my full source code (Python 2.6, Pygame 1.9 required) to:

http://www.necessarygames.com/junk/planes_trains.zip

The relevant code is in src/main.py, which is all set to run.

  • 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-05-14T03:04:25+00:00Added an answer on May 14, 2026 at 3:04 am

    …Romania has been added to the processed_countries list by Ukraine.

    Use separate processed_countries lists for each graph path. They say one code example is worth thousand words, so I’ve changed your code a little (untested):

    def get_longest_trip(self, board, processed_countries = list()):
        # see https://stackoverflow.com/questions/576988/python-specific-antipatterns-and-bad-practices/577198#577198
        processed_countries = list(processed_countries)
        processed_countries.append(self)
    
        longest_trip = list()
        if self.get_connections(board):
            possible_trips = list()
            for card in self.get_connections(board):
                if card not in processed_countries:
                    possible_trips.append(card.get_longest_trip(board, 
                                                                processed_countries))
            if possible_trips:
                longest_trip = max(possible_trips, key=len)
                longest_trip.append(self)
    
        if not longest_trip:
            longest_trip.append(self)
        return longest_trip
    

    Unrelated matters:

    Traceback (most recent call last):
      File "main.py", line 1171, in <module>
        main()
      File "main.py", line 1162, in main
        interface = Interface(continent, screen, ev_manager)    
      File "main.py", line 72, in __init__
        self.deck = Deck(ev_manager, continent)
      File "main.py", line 125, in __init__
        self.rebuild(continent)  
      File "main.py", line 148, in rebuild
        self.stack.append(CountryCard(country, self.ev_manager))
      File "main.py", line 1093, in __init__
        Card.__init__(self, COUNTRY, country.name, country.image, country.color, ev_manager)  
      File "main.py", line 693, in __init__
        self.set_text(text)
      File "main.py", line 721, in set_text
        self.rendered_text = self.render_text_rec(text)  
      File "main.py", line 817, in render_text_rec
        return render_textrect(text, self.font, text_rect, self.text_color, self.text_bgcolor, 1)       
      File "/home/vasi/Desktop/Planes and Trains/src/textrect.py", line 47, in render_textrect
        raise TextRectException, "The word " + word + " is too long to fit in the rect passed."
    textrect.TextRectException: The word Montenegro is too long to fit in the rect passed.
    

    There are 16 different bak files in your source package. Sixteen. Sixteeeen. Think about it and start to use version control.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

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.