How iterate condition checking in python?











up vote
1
down vote

favorite
1












The following function gives the correct result for me. But for a large number of q's, it very difficult to handle this program. So, I would like to iterate q's via a loop or some other manner. How can I do this?



def sgf(a): # here a is a list of two numbers
import random
a2=random.randint(1,1068)
p=1069
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
while ((q1>1060) or (q2>1060) or (q3>1060) or (q4>1060) or (q5>1060) or (q6>1060) or (q7>1060) or (q8>1060) or (q9>1060)):
a2=random.randint(1,1068)
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
if ((q1<=1060) and (q2<=1060) and (q3<=1060) and (q4<=1060) and (q5<=1060) and (q6<=1060) and (q7<=1060) and (q8<=1060) and (q9<=1060)):
break
return q1,q2,q3,q4,q5,q6,q7,q8,q9


For simplicity,



f(x)=(a0+a1*x+a2*x**2)%p


where a0,a1 are in [0,1060] and a2 chosen randomly from [0,1068]
if all f(x)<=1060, accept f(x) otherwise regenerate f(x)










share|improve this question




















  • 1




    You should learn how to use lists and loops... In fact, for this particular problem, you should use NumPy.
    – DYZ
    Nov 11 at 3:23










  • Try something like while any(a[0] + a[1] * 1 + a2 * i ** 2) % p > 1060 for i in range(1, 10))
    – Klaus D.
    Nov 11 at 3:27

















up vote
1
down vote

favorite
1












The following function gives the correct result for me. But for a large number of q's, it very difficult to handle this program. So, I would like to iterate q's via a loop or some other manner. How can I do this?



def sgf(a): # here a is a list of two numbers
import random
a2=random.randint(1,1068)
p=1069
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
while ((q1>1060) or (q2>1060) or (q3>1060) or (q4>1060) or (q5>1060) or (q6>1060) or (q7>1060) or (q8>1060) or (q9>1060)):
a2=random.randint(1,1068)
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
if ((q1<=1060) and (q2<=1060) and (q3<=1060) and (q4<=1060) and (q5<=1060) and (q6<=1060) and (q7<=1060) and (q8<=1060) and (q9<=1060)):
break
return q1,q2,q3,q4,q5,q6,q7,q8,q9


For simplicity,



f(x)=(a0+a1*x+a2*x**2)%p


where a0,a1 are in [0,1060] and a2 chosen randomly from [0,1068]
if all f(x)<=1060, accept f(x) otherwise regenerate f(x)










share|improve this question




















  • 1




    You should learn how to use lists and loops... In fact, for this particular problem, you should use NumPy.
    – DYZ
    Nov 11 at 3:23










  • Try something like while any(a[0] + a[1] * 1 + a2 * i ** 2) % p > 1060 for i in range(1, 10))
    – Klaus D.
    Nov 11 at 3:27















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





The following function gives the correct result for me. But for a large number of q's, it very difficult to handle this program. So, I would like to iterate q's via a loop or some other manner. How can I do this?



def sgf(a): # here a is a list of two numbers
import random
a2=random.randint(1,1068)
p=1069
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
while ((q1>1060) or (q2>1060) or (q3>1060) or (q4>1060) or (q5>1060) or (q6>1060) or (q7>1060) or (q8>1060) or (q9>1060)):
a2=random.randint(1,1068)
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
if ((q1<=1060) and (q2<=1060) and (q3<=1060) and (q4<=1060) and (q5<=1060) and (q6<=1060) and (q7<=1060) and (q8<=1060) and (q9<=1060)):
break
return q1,q2,q3,q4,q5,q6,q7,q8,q9


For simplicity,



f(x)=(a0+a1*x+a2*x**2)%p


where a0,a1 are in [0,1060] and a2 chosen randomly from [0,1068]
if all f(x)<=1060, accept f(x) otherwise regenerate f(x)










share|improve this question















The following function gives the correct result for me. But for a large number of q's, it very difficult to handle this program. So, I would like to iterate q's via a loop or some other manner. How can I do this?



def sgf(a): # here a is a list of two numbers
import random
a2=random.randint(1,1068)
p=1069
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
while ((q1>1060) or (q2>1060) or (q3>1060) or (q4>1060) or (q5>1060) or (q6>1060) or (q7>1060) or (q8>1060) or (q9>1060)):
a2=random.randint(1,1068)
q1=(a[0]+a[1]*1+a2*1**2)%p
q2=(a[0]+a[1]*2+a2*2**2)%p
q3=(a[0]+a[1]*3+a2*3**2)%p
q4=(a[0]+a[1]*4+a2*4**2)%p
q5=(a[0]+a[1]*5+a2*5**2)%p
q6=(a[0]+a[1]*6+a2*6**2)%p
q7=(a[0]+a[1]*7+a2*7**2)%p
q8=(a[0]+a[1]*8+a2*8**2)%p
q9=(a[0]+a[1]*9+a2*9**2)%p
if ((q1<=1060) and (q2<=1060) and (q3<=1060) and (q4<=1060) and (q5<=1060) and (q6<=1060) and (q7<=1060) and (q8<=1060) and (q9<=1060)):
break
return q1,q2,q3,q4,q5,q6,q7,q8,q9


For simplicity,



f(x)=(a0+a1*x+a2*x**2)%p


where a0,a1 are in [0,1060] and a2 chosen randomly from [0,1068]
if all f(x)<=1060, accept f(x) otherwise regenerate f(x)







python python-2.7 loops iteration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:28

























asked Nov 11 at 3:18









H.S.

326




326








  • 1




    You should learn how to use lists and loops... In fact, for this particular problem, you should use NumPy.
    – DYZ
    Nov 11 at 3:23










  • Try something like while any(a[0] + a[1] * 1 + a2 * i ** 2) % p > 1060 for i in range(1, 10))
    – Klaus D.
    Nov 11 at 3:27
















  • 1




    You should learn how to use lists and loops... In fact, for this particular problem, you should use NumPy.
    – DYZ
    Nov 11 at 3:23










  • Try something like while any(a[0] + a[1] * 1 + a2 * i ** 2) % p > 1060 for i in range(1, 10))
    – Klaus D.
    Nov 11 at 3:27










1




1




You should learn how to use lists and loops... In fact, for this particular problem, you should use NumPy.
– DYZ
Nov 11 at 3:23




You should learn how to use lists and loops... In fact, for this particular problem, you should use NumPy.
– DYZ
Nov 11 at 3:23












Try something like while any(a[0] + a[1] * 1 + a2 * i ** 2) % p > 1060 for i in range(1, 10))
– Klaus D.
Nov 11 at 3:27






Try something like while any(a[0] + a[1] * 1 + a2 * i ** 2) % p > 1060 for i in range(1, 10))
– Klaus D.
Nov 11 at 3:27














3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










It looks like the only variables in your generation of qs are the integers that you multiple a[1] and a2 by.



You could alter your function to accept the total number of qs as a second parameter (e.g. total_q), then iterate over range(1, total_q+1) and append each new q to a Python list, returning the list at the end.



Try something like:



def sgf(a, total_q):
import random
a2=random.randint(1,1068)
p=1069
q_list =
for i in range(0,total_q+1):
q = (a[0]+a[1]*i+a2*i**2)%p
q_list.append(q)
while any(q > 1060 for q in q_list):
a2=random.randint(1,1068)
q_list =
for i in range(0,total_q+1):
q =(a[0]+a[1]*i+a2*i**2)%p
q_list.append(q)

if all(q <= 1060 for q in q_list):
break
return q_list





share|improve this answer




























    up vote
    3
    down vote













    Your q should probably be a list. That way you can use a for loop with range to perform your calculation:



    for ind in range(len(q)):
    q[ind]=(a[0]+a[1]*(ind + 1)+a2*(ind + 1)**2)%p


    You can also represent your condition as an iteration, but the simplest way would probably be to use the any function and write your condition as a generator:



    while any(qx > 1060 for qx in q):





    share|improve this answer




























      up vote
      1
      down vote













      def sgf(a):
      import random
      a2=random.randint(1,1068)
      p=1069
      items =
      # if items is empty, or any element is greater than 1060, keep looping
      while not items or any(item > 1060 for item in items):
      items = [(a[0]+a[1]*i+a2*i**2)%p for i in range(1,10)]
      return items





      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',
        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%2f53245559%2fhow-iterate-condition-checking-in-python%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        It looks like the only variables in your generation of qs are the integers that you multiple a[1] and a2 by.



        You could alter your function to accept the total number of qs as a second parameter (e.g. total_q), then iterate over range(1, total_q+1) and append each new q to a Python list, returning the list at the end.



        Try something like:



        def sgf(a, total_q):
        import random
        a2=random.randint(1,1068)
        p=1069
        q_list =
        for i in range(0,total_q+1):
        q = (a[0]+a[1]*i+a2*i**2)%p
        q_list.append(q)
        while any(q > 1060 for q in q_list):
        a2=random.randint(1,1068)
        q_list =
        for i in range(0,total_q+1):
        q =(a[0]+a[1]*i+a2*i**2)%p
        q_list.append(q)

        if all(q <= 1060 for q in q_list):
        break
        return q_list





        share|improve this answer

























          up vote
          1
          down vote



          accepted










          It looks like the only variables in your generation of qs are the integers that you multiple a[1] and a2 by.



          You could alter your function to accept the total number of qs as a second parameter (e.g. total_q), then iterate over range(1, total_q+1) and append each new q to a Python list, returning the list at the end.



          Try something like:



          def sgf(a, total_q):
          import random
          a2=random.randint(1,1068)
          p=1069
          q_list =
          for i in range(0,total_q+1):
          q = (a[0]+a[1]*i+a2*i**2)%p
          q_list.append(q)
          while any(q > 1060 for q in q_list):
          a2=random.randint(1,1068)
          q_list =
          for i in range(0,total_q+1):
          q =(a[0]+a[1]*i+a2*i**2)%p
          q_list.append(q)

          if all(q <= 1060 for q in q_list):
          break
          return q_list





          share|improve this answer























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            It looks like the only variables in your generation of qs are the integers that you multiple a[1] and a2 by.



            You could alter your function to accept the total number of qs as a second parameter (e.g. total_q), then iterate over range(1, total_q+1) and append each new q to a Python list, returning the list at the end.



            Try something like:



            def sgf(a, total_q):
            import random
            a2=random.randint(1,1068)
            p=1069
            q_list =
            for i in range(0,total_q+1):
            q = (a[0]+a[1]*i+a2*i**2)%p
            q_list.append(q)
            while any(q > 1060 for q in q_list):
            a2=random.randint(1,1068)
            q_list =
            for i in range(0,total_q+1):
            q =(a[0]+a[1]*i+a2*i**2)%p
            q_list.append(q)

            if all(q <= 1060 for q in q_list):
            break
            return q_list





            share|improve this answer












            It looks like the only variables in your generation of qs are the integers that you multiple a[1] and a2 by.



            You could alter your function to accept the total number of qs as a second parameter (e.g. total_q), then iterate over range(1, total_q+1) and append each new q to a Python list, returning the list at the end.



            Try something like:



            def sgf(a, total_q):
            import random
            a2=random.randint(1,1068)
            p=1069
            q_list =
            for i in range(0,total_q+1):
            q = (a[0]+a[1]*i+a2*i**2)%p
            q_list.append(q)
            while any(q > 1060 for q in q_list):
            a2=random.randint(1,1068)
            q_list =
            for i in range(0,total_q+1):
            q =(a[0]+a[1]*i+a2*i**2)%p
            q_list.append(q)

            if all(q <= 1060 for q in q_list):
            break
            return q_list






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 3:41









            dmitriys

            1469




            1469
























                up vote
                3
                down vote













                Your q should probably be a list. That way you can use a for loop with range to perform your calculation:



                for ind in range(len(q)):
                q[ind]=(a[0]+a[1]*(ind + 1)+a2*(ind + 1)**2)%p


                You can also represent your condition as an iteration, but the simplest way would probably be to use the any function and write your condition as a generator:



                while any(qx > 1060 for qx in q):





                share|improve this answer

























                  up vote
                  3
                  down vote













                  Your q should probably be a list. That way you can use a for loop with range to perform your calculation:



                  for ind in range(len(q)):
                  q[ind]=(a[0]+a[1]*(ind + 1)+a2*(ind + 1)**2)%p


                  You can also represent your condition as an iteration, but the simplest way would probably be to use the any function and write your condition as a generator:



                  while any(qx > 1060 for qx in q):





                  share|improve this answer























                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Your q should probably be a list. That way you can use a for loop with range to perform your calculation:



                    for ind in range(len(q)):
                    q[ind]=(a[0]+a[1]*(ind + 1)+a2*(ind + 1)**2)%p


                    You can also represent your condition as an iteration, but the simplest way would probably be to use the any function and write your condition as a generator:



                    while any(qx > 1060 for qx in q):





                    share|improve this answer












                    Your q should probably be a list. That way you can use a for loop with range to perform your calculation:



                    for ind in range(len(q)):
                    q[ind]=(a[0]+a[1]*(ind + 1)+a2*(ind + 1)**2)%p


                    You can also represent your condition as an iteration, but the simplest way would probably be to use the any function and write your condition as a generator:



                    while any(qx > 1060 for qx in q):






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 3:27









                    Score_Under

                    822614




                    822614






















                        up vote
                        1
                        down vote













                        def sgf(a):
                        import random
                        a2=random.randint(1,1068)
                        p=1069
                        items =
                        # if items is empty, or any element is greater than 1060, keep looping
                        while not items or any(item > 1060 for item in items):
                        items = [(a[0]+a[1]*i+a2*i**2)%p for i in range(1,10)]
                        return items





                        share|improve this answer

























                          up vote
                          1
                          down vote













                          def sgf(a):
                          import random
                          a2=random.randint(1,1068)
                          p=1069
                          items =
                          # if items is empty, or any element is greater than 1060, keep looping
                          while not items or any(item > 1060 for item in items):
                          items = [(a[0]+a[1]*i+a2*i**2)%p for i in range(1,10)]
                          return items





                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            def sgf(a):
                            import random
                            a2=random.randint(1,1068)
                            p=1069
                            items =
                            # if items is empty, or any element is greater than 1060, keep looping
                            while not items or any(item > 1060 for item in items):
                            items = [(a[0]+a[1]*i+a2*i**2)%p for i in range(1,10)]
                            return items





                            share|improve this answer












                            def sgf(a):
                            import random
                            a2=random.randint(1,1068)
                            p=1069
                            items =
                            # if items is empty, or any element is greater than 1060, keep looping
                            while not items or any(item > 1060 for item in items):
                            items = [(a[0]+a[1]*i+a2*i**2)%p for i in range(1,10)]
                            return items






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 3:30









                            John Gordon

                            9,08551727




                            9,08551727






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245559%2fhow-iterate-condition-checking-in-python%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

                                Full-time equivalent

                                さくらももこ

                                13 indicted, 8 arrested in Calif. drug cartel investigation