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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:16:03+00:00 2026-06-15T03:16:03+00:00

I was working with students creating a simple demo of a finite state machine:

  • 0

I was working with students creating a simple demo of a finite state machine:

s2 = {}
s1 = {}
s1["0"] = s2
s1["1"] = s1
s2["0"] = s1
s2["1"] = s2
machine = {"start": s1, "accepting": [s1]}

def recognize(fsm, in_string):
    return do_recognize(fsm, fsm["start"], in_string)

def do_recognize(fsm, current, in_string):
    if len(in_string) == 0:
        return current in fsm["accepting"]

   return do_recognize(fsm, current[in_string[0]] ,
                 in_string[1:])

print (recognize(machine, "0"))

This machine recognizes strings with an even number of 0s, and it works fine on “good” strings (such as “1” or “010”). But on a “bad” string such as the one above, it gets
into an infinite loop and then stack overflow at return current in fsm[“accepting”].

I was able to determine that the problem is the comparison of the two states. In fact I can generate the exact same bug by just writing s1 == s2. But s1 == s1 (a good state) works fine.

My best guess of what’s happening is that it’s doing a deep compare and trying to follow all of the references in s2, which are circular. But why is it asymmetrical (i.e. why doesn’t s1 == s1 have the same problem)? And how can I avoid it?

  • 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-15T03:16:04+00:00Added an answer on June 15, 2026 at 3:16 am

    When you compare dictionaries, each item (key/value pair) in the dictionary is compared as well, so if you have circular references between dictionaries where the circular references involve the same keys, you will get this maximum recursion depth exceeded error when comparing them:

    For example, if you have s1 == {'0': s2} and s2 == {'0': s1}, then attempting s1 == s2 will result in the following comparisons, which illustrates how the recursion occurs:

    s1 == s2 --> s1['0'] == s2['0'] --> s2 == s1 --> s2['0'] == s1['0'] --> s1 == s2 --> ...
    

    A containment test like s1 in [s2] or s2 in [s1] will result in this equality comparison as well, which is why it happens in your code at current in fsm["accepting"].

    You can work around this recursion issue by using an identity comparison instead of an equality comparison, just replace current in fsm["accepting"] with the following:

    any(s is current for s in fsm["accepting"])
    

    A better solution might be to not use circular references by having the states refer to an identifier instead of the object itself, for example you could have a structure like the following:

    states = {"s1": {"0": "s2", "1": "s1"},
              "s2": {"0": "s1", "1": "s2"}}
    machine = {"start": "s1", "accepting": ["s1"]}
    
    def recognize(fsm, in_string):
        return do_recognize(fsm, fsm["start"], in_string)
    
    def do_recognize(fsm, current, in_string):
        if len(in_string) == 0:
            return current in fsm["accepting"]
        return do_recognize(fsm, states[current][in_string[0]], in_string[1:])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on creating a SQL query where the result will return a
I'm currently working on an app that will tell students when their classes start
I am working on creating a searchable database of all the students and associated
Ok, I'm working on a php college project, and I have to let students
The site I'm working on involves teachers creating student objects. The teacher can choose
I'm working on an application for a Nursing students; It is a program where
I'm working as a TA in an introductory programming class, and the students tend
Here is a link to the website I'm working on: http://students.bcitwebdev.com/jlandon/geordieirving/index.php . I'm having
I am working on this site: http://gitastudents.com/~clarkb/group1/Students.html For some reason it does not display
Assume: There are hundred students and each on of them are working on a

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.