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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:00:09+00:00 2026-05-27T16:00:09+00:00

I want to use boolean algebra instead of several conditionals, for example def boo(x,y):

  • 0

I want to use boolean algebra instead of several conditionals, for example

def boo(x,y):
    if x==3 and y==1: return 4
    if x==3 and y==4: return 1
    if x==4 and y==1: return 2
    if x==4 and y==2: return 1
    if x==5 and y==1: return 3

what I want to do is

def simple(x,y):
    return x#y

and there are total 12 equations, I just want to directly return (x#y) where # is a boolean operator. I did this to a smaller problem where I luckily found out a relation. I want to do the same in this case also, how do I proceed with it?

Does this have any performance gains, because its not going through several if conditionals?
Is this normal practice?

sample:

 x    y   output
 1    2     3
 1    3     2
 1    4     5
 1    5     4

here a simple bitwise xor gate will do

def(x,y): return x^y
  • 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-27T16:00:10+00:00Added an answer on May 27, 2026 at 4:00 pm

    I don’t know whether trying to find a terser expression of the above logic would lead to more readable code; probably not. But you can rework the logic as-is to a more mathematical formulation:

    def boo(x, y):
        p = (x, y)
        return (1 if p in ((3, 4), (4, 2)) else
                2 if p == (4, 1) else
                3 if p == (5, 1) else
                4 if p == (3, 1) else
                None)
    

    Another option is to use a dictionary:

    def boo(x, y):
        return {(3,4):1, (4,2):1, (4,1):2, (5,1):3, (3,1):4}.get((x, y), None)
    

    If you know that all values will match the specified cases, you can write [(x, y)] instead of .get((x, y), None).

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

Sidebar

Related Questions

I want to use a JOIN to return a boolean result. Here's an example
Is there a better way to do use a use a boolean with visible?
What I want is to use a boolean value from applicationContext.properties (Jboss configuration file)
I just want to know how to use a Boolean in a Spring mvc
I have a boolean property in code-behind ASP.NET, now I want to use it
I would like to use setAlwaysOnTop(boolean) in java. I want to setAlwaysOnTop() when I
Inside MSACCESS I want to use relatively simple bitwise operations in WHERE clause of
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
I have a transaction log file in CSV format that I want use to

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.