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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:36:42+00:00 2026-06-10T10:36:42+00:00

If i mention the word ‘state machine’ , most people would go for state

  • 0

If i mention the word ‘state machine’ , most people would go for state machine design. So i’d like to simplify the case to focus on the point.

Suggested here is a text flow, it’s very long or it’s frequently brought to current stage.

Each time I process a single character from the sequence and decide what the next state is. Here are two solutions:

textflow = iter(text)
endFlag = False
while True:
   process()
   if endFlag:break

then the process method would be like this

def process0():
    for x in textflow:
        do something
        if condition1:
           process = process1
           break
        elif cond2:
           process = process2
           break


def process1():
    for x in textflow:
        do something
        if cond0:
           process = process0
           break
        elif cond2:
           process = process2
           break
    ...

or

for x in text:
   process(x)

then the process method would be like this

def process0(x):
    do something
    if cond1:
      process = process1
    elif cond2:
      process = process2

def process1(x):
    do something
    if cond0:
       process = process0
    elif cond2:
       process = process2
....

In the first solution, each process method does the iteration by its own way and ends the process until the state is changed. In the second, the main loop does the iterations and each time process would be called.(map() could be use, but since the process would be changed everytime, it doesn’t help.)

The state change would be like this:

0 1 2 1 0 2 1 0 1 2 0 1 2 0 ....

or like this:

0 0 0 0 0 ...many 0 .. 0 1 ...many 1 ... 1 2......2 1 ..... 1

A more common case would be:

0 1 1 1 1 1 0 0 2 1 1 1 1 2 0 2 3 1 1 1 1 3 1 1 1 2 0 ....

I need an advisor to tell me which solution is more efficient.

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

    It will mostly depend on how many items you want to process, as function calls are expensive. If you have a large number of items, then embedding the loop inside the function is the better option. If it’ll only iterate over a few items, I usually go with whichever is clearest in expressing intent.

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

Sidebar

Related Questions

I am pulling profiles which mention the word fitness from a database like so:
Using XPath, I would like to Match whole word (option for user, just like
I always see people mention that Python binding and C Sharp binding etc. when
I am programmatically generating Office Documents (in my case Word or Excel 2007) using
When I read books about WPF, I saw the authors mention XPS like it
Python docs mention this word a lot and I want to know what it
I would like to know the better way to arrange the list items for
Whilst a few questions mention using Lua in conjunction with some other language, I
First let me mention that I've gone through many suggested questions and found no
I had someone mention to me that catching all exceptions is not necessarily good

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.