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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:36:25+00:00 2026-05-16T04:36:25+00:00

I wrote a simple script to solve a logic puzzle, the type of puzzle

  • 0

I wrote a simple script to solve a “logic puzzle”, the type of puzzle from school where you are given a number of rules and then must be able to find the solution for problems like “There are five musicians named A, B, C, D, and E playing in a concert, each plays one after the other… if A goes before B, and D is not last … what is the order of who plays when?” etc.

To evaluate possible solutions, I wrote each “rule” as a separate function which would evaluate if a possible solution (represented simply as a list of strings) is valid, for example

#Fifth slot must be B or D
def rule1(solution):
    return solution[4] == 'B' or solution[4] == 'D'

#There must be at least two spots between A and B
def rule2(solution):
    returns abs(solution.index('A') - solution.index('B')) >= 2

#etc...

I’m interested in finding the Pythonic way to test if a possible solution passes all such rules, with the ability to stop evaluating rules after the first has failed.

At first I wrote the simplest possible thing:

def is_valid(solution):
    return rule1(solution) and rule2(solution) and rule3(solution) and ...

But this seemed rather ugly. I thought perhaps I could make this read a bit more elegant with something like a list comprehension…

def is_valid(solution)
    rules = [rule1, rule2, rule3, rule4, ... ]
    return all([r(solution) for f in rules])

… but then I realized that since the list comprehension is generated before the all() function is evaluated, that this has the side effect of not being short-circuited at all – every rule will be evaluated even if the first returns False.

So my question is: is there a more Pythonic/functional way to be able to evaluate a list of True/False expressions, with short-circuiting, without the need to write out a long list of return f1(s) and f2(s) and f3(s) ... ?

  • 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-16T04:36:25+00:00Added an answer on May 16, 2026 at 4:36 am

    Use a generator expression:

    rules = [ rule1, rule2, rule3, rule4, ... ]
    rules_generator = ( r( solution ) for r in rules )
    return all( rules_generator )
    

    Syntactic sugar: you can omit the extra parentheses:

    rules = [ rule1, rule2, rule3, rule4, ... ]
    return all( r( solution ) for r in rules )
    

    A generator is (basically) an object with a .next() method, which returns the next item in some iterable. This means they can do useful things like read a file in chunks without loading it all into memory, or iterate up to huge integers. You can iterate over them with for loops transparently; Python handles it behind-the-scenes. For instance, range is a generator in Py3k.

    You can roll your own custom generator expressions by using the yield statement instead of return in a function definition:

    def integers():
        i = 0
        while True:
            yield i
    

    and Python will handle saving the function’s state and so on. They’re awesome!

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

Sidebar

Related Questions

I wrote a simple script for validating login form as follows: <script type=text/javascript> function
I wrote a simple script that is intended to do hierarchical clustering on a
A couple of weeks ago, I wrote a simple Ruby script to test a
I just wrote a very simple perl tk script to read a huge text
I want to write simple script to copy/backup directory then remove on server startup.
I wrote simple script test echo hello #<-- inside test if I press one
on my way to help a fellow I wrote a simple F-Script without even
I wrote a simple python script using the SocketServer, it works well on Windows,
I wrote this simple script for validating non-empty input fields. When, for instance, the
I wrote a simple script for validating forms in jQuery. These are the submit

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.