How to check if one or two integers are -1, among four integer variables in total, Python3?












0














I have 4 integer variables



r1, r2, c1, c2


They can be any integer values. I want to find out which of them is/are -1.



The rule is r1 and r2 cannot be -1 at the same time; c1 and c2 cannot be -1 at the same time.



I can use if statement, but it can be cumbersome:



if r1 == -1:        
if c1 == -1:
# do action a
elif c2 == -1:
# do action b
else: # c1 and c2 are both not -1
# do action c
elif r2 == -1:
if c1 == -1:
# do action d
elif c2 == -1:
# do action e
else: # c1 and c2 are both not -1
# do action f
else: # none of the four integers is -1.
# do action g


Could you please show me some more elegant and efficient algorithm?










share|improve this question




















  • 3




    This logic seems suspect: the comment # none of the four integers is -1. is incorrect.
    – wim
    Nov 20 '18 at 16:16






  • 3




    Do you just need to check that r1, r2 are both not -1 and c1, c2 are not -1 ?
    – Austin
    Nov 20 '18 at 16:23








  • 6




    If you want to perform specific operations depending on value of each variable then if else might be the only way!
    – Sandesh34
    Nov 20 '18 at 16:26










  • if r1 == r2 == -1 or c1 == c2 == -1: print("rule violation")
    – jasonharper
    Nov 20 '18 at 17:12










  • @Sandesh.Patil I can actually think of a few ways that doesn't involve if... else... but none of them would be as readable and as elegant as the original implementation. That said, if OP confirms it's just a check against -1 and no separate actions needed, it'll be much simpler.
    – Idlehands
    Nov 20 '18 at 17:33
















0














I have 4 integer variables



r1, r2, c1, c2


They can be any integer values. I want to find out which of them is/are -1.



The rule is r1 and r2 cannot be -1 at the same time; c1 and c2 cannot be -1 at the same time.



I can use if statement, but it can be cumbersome:



if r1 == -1:        
if c1 == -1:
# do action a
elif c2 == -1:
# do action b
else: # c1 and c2 are both not -1
# do action c
elif r2 == -1:
if c1 == -1:
# do action d
elif c2 == -1:
# do action e
else: # c1 and c2 are both not -1
# do action f
else: # none of the four integers is -1.
# do action g


Could you please show me some more elegant and efficient algorithm?










share|improve this question




















  • 3




    This logic seems suspect: the comment # none of the four integers is -1. is incorrect.
    – wim
    Nov 20 '18 at 16:16






  • 3




    Do you just need to check that r1, r2 are both not -1 and c1, c2 are not -1 ?
    – Austin
    Nov 20 '18 at 16:23








  • 6




    If you want to perform specific operations depending on value of each variable then if else might be the only way!
    – Sandesh34
    Nov 20 '18 at 16:26










  • if r1 == r2 == -1 or c1 == c2 == -1: print("rule violation")
    – jasonharper
    Nov 20 '18 at 17:12










  • @Sandesh.Patil I can actually think of a few ways that doesn't involve if... else... but none of them would be as readable and as elegant as the original implementation. That said, if OP confirms it's just a check against -1 and no separate actions needed, it'll be much simpler.
    – Idlehands
    Nov 20 '18 at 17:33














0












0








0


0





I have 4 integer variables



r1, r2, c1, c2


They can be any integer values. I want to find out which of them is/are -1.



The rule is r1 and r2 cannot be -1 at the same time; c1 and c2 cannot be -1 at the same time.



I can use if statement, but it can be cumbersome:



if r1 == -1:        
if c1 == -1:
# do action a
elif c2 == -1:
# do action b
else: # c1 and c2 are both not -1
# do action c
elif r2 == -1:
if c1 == -1:
# do action d
elif c2 == -1:
# do action e
else: # c1 and c2 are both not -1
# do action f
else: # none of the four integers is -1.
# do action g


Could you please show me some more elegant and efficient algorithm?










share|improve this question















I have 4 integer variables



r1, r2, c1, c2


They can be any integer values. I want to find out which of them is/are -1.



The rule is r1 and r2 cannot be -1 at the same time; c1 and c2 cannot be -1 at the same time.



I can use if statement, but it can be cumbersome:



if r1 == -1:        
if c1 == -1:
# do action a
elif c2 == -1:
# do action b
else: # c1 and c2 are both not -1
# do action c
elif r2 == -1:
if c1 == -1:
# do action d
elif c2 == -1:
# do action e
else: # c1 and c2 are both not -1
# do action f
else: # none of the four integers is -1.
# do action g


Could you please show me some more elegant and efficient algorithm?







python python-3.x if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 16:16









gsamaras

50.6k2399186




50.6k2399186










asked Nov 20 '18 at 16:08









aura

1679




1679








  • 3




    This logic seems suspect: the comment # none of the four integers is -1. is incorrect.
    – wim
    Nov 20 '18 at 16:16






  • 3




    Do you just need to check that r1, r2 are both not -1 and c1, c2 are not -1 ?
    – Austin
    Nov 20 '18 at 16:23








  • 6




    If you want to perform specific operations depending on value of each variable then if else might be the only way!
    – Sandesh34
    Nov 20 '18 at 16:26










  • if r1 == r2 == -1 or c1 == c2 == -1: print("rule violation")
    – jasonharper
    Nov 20 '18 at 17:12










  • @Sandesh.Patil I can actually think of a few ways that doesn't involve if... else... but none of them would be as readable and as elegant as the original implementation. That said, if OP confirms it's just a check against -1 and no separate actions needed, it'll be much simpler.
    – Idlehands
    Nov 20 '18 at 17:33














  • 3




    This logic seems suspect: the comment # none of the four integers is -1. is incorrect.
    – wim
    Nov 20 '18 at 16:16






  • 3




    Do you just need to check that r1, r2 are both not -1 and c1, c2 are not -1 ?
    – Austin
    Nov 20 '18 at 16:23








  • 6




    If you want to perform specific operations depending on value of each variable then if else might be the only way!
    – Sandesh34
    Nov 20 '18 at 16:26










  • if r1 == r2 == -1 or c1 == c2 == -1: print("rule violation")
    – jasonharper
    Nov 20 '18 at 17:12










  • @Sandesh.Patil I can actually think of a few ways that doesn't involve if... else... but none of them would be as readable and as elegant as the original implementation. That said, if OP confirms it's just a check against -1 and no separate actions needed, it'll be much simpler.
    – Idlehands
    Nov 20 '18 at 17:33








3




3




This logic seems suspect: the comment # none of the four integers is -1. is incorrect.
– wim
Nov 20 '18 at 16:16




This logic seems suspect: the comment # none of the four integers is -1. is incorrect.
– wim
Nov 20 '18 at 16:16




3




3




Do you just need to check that r1, r2 are both not -1 and c1, c2 are not -1 ?
– Austin
Nov 20 '18 at 16:23






Do you just need to check that r1, r2 are both not -1 and c1, c2 are not -1 ?
– Austin
Nov 20 '18 at 16:23






6




6




If you want to perform specific operations depending on value of each variable then if else might be the only way!
– Sandesh34
Nov 20 '18 at 16:26




If you want to perform specific operations depending on value of each variable then if else might be the only way!
– Sandesh34
Nov 20 '18 at 16:26












if r1 == r2 == -1 or c1 == c2 == -1: print("rule violation")
– jasonharper
Nov 20 '18 at 17:12




if r1 == r2 == -1 or c1 == c2 == -1: print("rule violation")
– jasonharper
Nov 20 '18 at 17:12












@Sandesh.Patil I can actually think of a few ways that doesn't involve if... else... but none of them would be as readable and as elegant as the original implementation. That said, if OP confirms it's just a check against -1 and no separate actions needed, it'll be much simpler.
– Idlehands
Nov 20 '18 at 17:33




@Sandesh.Patil I can actually think of a few ways that doesn't involve if... else... but none of them would be as readable and as elegant as the original implementation. That said, if OP confirms it's just a check against -1 and no separate actions needed, it'll be much simpler.
– Idlehands
Nov 20 '18 at 17:33












2 Answers
2






active

oldest

votes


















0














I don't think you have an efficiency problem here, but you can improve readability by checking for the two illegal scenarios you mentioned explicitly:



if r1 == -1 and r2 == -1:
raise Exception("r1 & r2 are -1")
elif c1 == -1 and c2 == -1:
raise Exception("c1 & c2 are -1")
else:
print("ok!")


You have some options to avoid the if/else entirely (a dictionary, for instance, with keys as tuples of (r1, r2, c1, c2) and the operations you wish to invoke as values), but I think they would all be a net loss insofar as readability goes.






share|improve this answer























  • I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
    – Sandesh34
    Nov 20 '18 at 17:23





















0














Based upon the rules you mentioned i.e. it cannot happen that r1=r2=-1 and c1=c2=-1, we can assume that there are cases when we do not need to perform all four checks to find out if your variables are -1 or not.



In order to utilize these rules, you need to have some data structure that reflects order of your variables and enables to skip checks that are not required (e.g. r1 = -1 than we do not need to check r2).



While this perhaps is not more efficient CPU time-wise, it follows the logic that it is not necessary to perform all of the variable checks in each scenario:



d = {'r1' : 0, 'r2' : -1, 'c1' : -1, 'c2' : 0}

def isMinusOne(x):
if x == -1:
return x

i = 0
while i < len(d):
if isMinusOne(list(d.values())[i]) is not None:
print(list(d.keys())[i])
if i == 0 or i == 2:
i += 2
else:
i += 1
else:
i += 1





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397047%2fhow-to-check-if-one-or-two-integers-are-1-among-four-integer-variables-in-tota%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I don't think you have an efficiency problem here, but you can improve readability by checking for the two illegal scenarios you mentioned explicitly:



    if r1 == -1 and r2 == -1:
    raise Exception("r1 & r2 are -1")
    elif c1 == -1 and c2 == -1:
    raise Exception("c1 & c2 are -1")
    else:
    print("ok!")


    You have some options to avoid the if/else entirely (a dictionary, for instance, with keys as tuples of (r1, r2, c1, c2) and the operations you wish to invoke as values), but I think they would all be a net loss insofar as readability goes.






    share|improve this answer























    • I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
      – Sandesh34
      Nov 20 '18 at 17:23


















    0














    I don't think you have an efficiency problem here, but you can improve readability by checking for the two illegal scenarios you mentioned explicitly:



    if r1 == -1 and r2 == -1:
    raise Exception("r1 & r2 are -1")
    elif c1 == -1 and c2 == -1:
    raise Exception("c1 & c2 are -1")
    else:
    print("ok!")


    You have some options to avoid the if/else entirely (a dictionary, for instance, with keys as tuples of (r1, r2, c1, c2) and the operations you wish to invoke as values), but I think they would all be a net loss insofar as readability goes.






    share|improve this answer























    • I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
      – Sandesh34
      Nov 20 '18 at 17:23
















    0












    0








    0






    I don't think you have an efficiency problem here, but you can improve readability by checking for the two illegal scenarios you mentioned explicitly:



    if r1 == -1 and r2 == -1:
    raise Exception("r1 & r2 are -1")
    elif c1 == -1 and c2 == -1:
    raise Exception("c1 & c2 are -1")
    else:
    print("ok!")


    You have some options to avoid the if/else entirely (a dictionary, for instance, with keys as tuples of (r1, r2, c1, c2) and the operations you wish to invoke as values), but I think they would all be a net loss insofar as readability goes.






    share|improve this answer














    I don't think you have an efficiency problem here, but you can improve readability by checking for the two illegal scenarios you mentioned explicitly:



    if r1 == -1 and r2 == -1:
    raise Exception("r1 & r2 are -1")
    elif c1 == -1 and c2 == -1:
    raise Exception("c1 & c2 are -1")
    else:
    print("ok!")


    You have some options to avoid the if/else entirely (a dictionary, for instance, with keys as tuples of (r1, r2, c1, c2) and the operations you wish to invoke as values), but I think they would all be a net loss insofar as readability goes.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 17:39

























    answered Nov 20 '18 at 17:15









    chris

    823814




    823814












    • I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
      – Sandesh34
      Nov 20 '18 at 17:23




















    • I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
      – Sandesh34
      Nov 20 '18 at 17:23


















    I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
    – Sandesh34
    Nov 20 '18 at 17:23






    I think you're just adding more statements, since he wants to perform various operations. And he's in need of efficient one
    – Sandesh34
    Nov 20 '18 at 17:23















    0














    Based upon the rules you mentioned i.e. it cannot happen that r1=r2=-1 and c1=c2=-1, we can assume that there are cases when we do not need to perform all four checks to find out if your variables are -1 or not.



    In order to utilize these rules, you need to have some data structure that reflects order of your variables and enables to skip checks that are not required (e.g. r1 = -1 than we do not need to check r2).



    While this perhaps is not more efficient CPU time-wise, it follows the logic that it is not necessary to perform all of the variable checks in each scenario:



    d = {'r1' : 0, 'r2' : -1, 'c1' : -1, 'c2' : 0}

    def isMinusOne(x):
    if x == -1:
    return x

    i = 0
    while i < len(d):
    if isMinusOne(list(d.values())[i]) is not None:
    print(list(d.keys())[i])
    if i == 0 or i == 2:
    i += 2
    else:
    i += 1
    else:
    i += 1





    share|improve this answer




























      0














      Based upon the rules you mentioned i.e. it cannot happen that r1=r2=-1 and c1=c2=-1, we can assume that there are cases when we do not need to perform all four checks to find out if your variables are -1 or not.



      In order to utilize these rules, you need to have some data structure that reflects order of your variables and enables to skip checks that are not required (e.g. r1 = -1 than we do not need to check r2).



      While this perhaps is not more efficient CPU time-wise, it follows the logic that it is not necessary to perform all of the variable checks in each scenario:



      d = {'r1' : 0, 'r2' : -1, 'c1' : -1, 'c2' : 0}

      def isMinusOne(x):
      if x == -1:
      return x

      i = 0
      while i < len(d):
      if isMinusOne(list(d.values())[i]) is not None:
      print(list(d.keys())[i])
      if i == 0 or i == 2:
      i += 2
      else:
      i += 1
      else:
      i += 1





      share|improve this answer


























        0












        0








        0






        Based upon the rules you mentioned i.e. it cannot happen that r1=r2=-1 and c1=c2=-1, we can assume that there are cases when we do not need to perform all four checks to find out if your variables are -1 or not.



        In order to utilize these rules, you need to have some data structure that reflects order of your variables and enables to skip checks that are not required (e.g. r1 = -1 than we do not need to check r2).



        While this perhaps is not more efficient CPU time-wise, it follows the logic that it is not necessary to perform all of the variable checks in each scenario:



        d = {'r1' : 0, 'r2' : -1, 'c1' : -1, 'c2' : 0}

        def isMinusOne(x):
        if x == -1:
        return x

        i = 0
        while i < len(d):
        if isMinusOne(list(d.values())[i]) is not None:
        print(list(d.keys())[i])
        if i == 0 or i == 2:
        i += 2
        else:
        i += 1
        else:
        i += 1





        share|improve this answer














        Based upon the rules you mentioned i.e. it cannot happen that r1=r2=-1 and c1=c2=-1, we can assume that there are cases when we do not need to perform all four checks to find out if your variables are -1 or not.



        In order to utilize these rules, you need to have some data structure that reflects order of your variables and enables to skip checks that are not required (e.g. r1 = -1 than we do not need to check r2).



        While this perhaps is not more efficient CPU time-wise, it follows the logic that it is not necessary to perform all of the variable checks in each scenario:



        d = {'r1' : 0, 'r2' : -1, 'c1' : -1, 'c2' : 0}

        def isMinusOne(x):
        if x == -1:
        return x

        i = 0
        while i < len(d):
        if isMinusOne(list(d.values())[i]) is not None:
        print(list(d.keys())[i])
        if i == 0 or i == 2:
        i += 2
        else:
        i += 1
        else:
        i += 1






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 20:43

























        answered Nov 20 '18 at 17:30









        New2Python

        338113




        338113






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397047%2fhow-to-check-if-one-or-two-integers-are-1-among-four-integer-variables-in-tota%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin