Check if the digits in the number are in increasing sequence in python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












14















I was working on a problem that determines whether the digits in the numbers are in the increasing sequence. Now, the approach I took to solve the problem was, For instance, consider the number 5678.



To check whether 5678 is an increasing sequence, I took the first digit and the next digit and the last digit which is 5,6,8 and substitute in range function range(first,last,(diff of first digit and the next to first digit)) i.e range(5,8+1,abs(5-6)).The result is the list of digits in the ascending order



To this problem, there is a constraint saying



For incrementing sequences, 0 should come after 9, and not before 1, as in 7890. Now my program breaks at the input 7890. I don't know how to encode this logic. Can someone help me, please?.



The code for increasing sequence was



 len(set(['5','6','7','8']) - set(map(str,range(5,8+1,abs(5-6))))) == 0 









share|improve this question



















  • 1





    Does each digit have to be exactly one bigger than the last?

    – John Gordon
    Mar 2 at 2:38












  • yes @JohnGordon

    – s326280
    Mar 2 at 2:40






  • 1





    The accepted answer currently seems to fail for 78901.

    – גלעד ברקן
    Mar 2 at 5:10












  • Sorry, i didn't notice that !!.

    – s326280
    Mar 2 at 5:25











  • Going from 9 to 0 is not an increase, no matter what your problem statement says. Badly worded question.

    – jpmc26
    Mar 2 at 15:47















14















I was working on a problem that determines whether the digits in the numbers are in the increasing sequence. Now, the approach I took to solve the problem was, For instance, consider the number 5678.



To check whether 5678 is an increasing sequence, I took the first digit and the next digit and the last digit which is 5,6,8 and substitute in range function range(first,last,(diff of first digit and the next to first digit)) i.e range(5,8+1,abs(5-6)).The result is the list of digits in the ascending order



To this problem, there is a constraint saying



For incrementing sequences, 0 should come after 9, and not before 1, as in 7890. Now my program breaks at the input 7890. I don't know how to encode this logic. Can someone help me, please?.



The code for increasing sequence was



 len(set(['5','6','7','8']) - set(map(str,range(5,8+1,abs(5-6))))) == 0 









share|improve this question



















  • 1





    Does each digit have to be exactly one bigger than the last?

    – John Gordon
    Mar 2 at 2:38












  • yes @JohnGordon

    – s326280
    Mar 2 at 2:40






  • 1





    The accepted answer currently seems to fail for 78901.

    – גלעד ברקן
    Mar 2 at 5:10












  • Sorry, i didn't notice that !!.

    – s326280
    Mar 2 at 5:25











  • Going from 9 to 0 is not an increase, no matter what your problem statement says. Badly worded question.

    – jpmc26
    Mar 2 at 15:47













14












14








14








I was working on a problem that determines whether the digits in the numbers are in the increasing sequence. Now, the approach I took to solve the problem was, For instance, consider the number 5678.



To check whether 5678 is an increasing sequence, I took the first digit and the next digit and the last digit which is 5,6,8 and substitute in range function range(first,last,(diff of first digit and the next to first digit)) i.e range(5,8+1,abs(5-6)).The result is the list of digits in the ascending order



To this problem, there is a constraint saying



For incrementing sequences, 0 should come after 9, and not before 1, as in 7890. Now my program breaks at the input 7890. I don't know how to encode this logic. Can someone help me, please?.



The code for increasing sequence was



 len(set(['5','6','7','8']) - set(map(str,range(5,8+1,abs(5-6))))) == 0 









share|improve this question
















I was working on a problem that determines whether the digits in the numbers are in the increasing sequence. Now, the approach I took to solve the problem was, For instance, consider the number 5678.



To check whether 5678 is an increasing sequence, I took the first digit and the next digit and the last digit which is 5,6,8 and substitute in range function range(first,last,(diff of first digit and the next to first digit)) i.e range(5,8+1,abs(5-6)).The result is the list of digits in the ascending order



To this problem, there is a constraint saying



For incrementing sequences, 0 should come after 9, and not before 1, as in 7890. Now my program breaks at the input 7890. I don't know how to encode this logic. Can someone help me, please?.



The code for increasing sequence was



 len(set(['5','6','7','8']) - set(map(str,range(5,8+1,abs(5-6))))) == 0 






python algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 2 at 2:38







s326280

















asked Mar 2 at 2:35









s326280s326280

1319




1319







  • 1





    Does each digit have to be exactly one bigger than the last?

    – John Gordon
    Mar 2 at 2:38












  • yes @JohnGordon

    – s326280
    Mar 2 at 2:40






  • 1





    The accepted answer currently seems to fail for 78901.

    – גלעד ברקן
    Mar 2 at 5:10












  • Sorry, i didn't notice that !!.

    – s326280
    Mar 2 at 5:25











  • Going from 9 to 0 is not an increase, no matter what your problem statement says. Badly worded question.

    – jpmc26
    Mar 2 at 15:47












  • 1





    Does each digit have to be exactly one bigger than the last?

    – John Gordon
    Mar 2 at 2:38












  • yes @JohnGordon

    – s326280
    Mar 2 at 2:40






  • 1





    The accepted answer currently seems to fail for 78901.

    – גלעד ברקן
    Mar 2 at 5:10












  • Sorry, i didn't notice that !!.

    – s326280
    Mar 2 at 5:25











  • Going from 9 to 0 is not an increase, no matter what your problem statement says. Badly worded question.

    – jpmc26
    Mar 2 at 15:47







1




1





Does each digit have to be exactly one bigger than the last?

– John Gordon
Mar 2 at 2:38






Does each digit have to be exactly one bigger than the last?

– John Gordon
Mar 2 at 2:38














yes @JohnGordon

– s326280
Mar 2 at 2:40





yes @JohnGordon

– s326280
Mar 2 at 2:40




1




1





The accepted answer currently seems to fail for 78901.

– גלעד ברקן
Mar 2 at 5:10






The accepted answer currently seems to fail for 78901.

– גלעד ברקן
Mar 2 at 5:10














Sorry, i didn't notice that !!.

– s326280
Mar 2 at 5:25





Sorry, i didn't notice that !!.

– s326280
Mar 2 at 5:25













Going from 9 to 0 is not an increase, no matter what your problem statement says. Badly worded question.

– jpmc26
Mar 2 at 15:47





Going from 9 to 0 is not an increase, no matter what your problem statement says. Badly worded question.

– jpmc26
Mar 2 at 15:47












7 Answers
7






active

oldest

votes


















18














You can simply check if the number, when converted to a string, is a substring of '1234567890':



str(num) in '1234567890'





share|improve this answer


















  • 2





    simple and answers perfectly. Good job.

    – Jean-François Fabre
    Mar 2 at 6:53











  • This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

    – Jörg W Mittag
    Mar 2 at 7:31






  • 4





    @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

    – blhsing
    Mar 2 at 7:53



















3














you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all to check that numbers follow, using a modulo 10 to handle the 0 case.



num = 7890

result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))





share|improve this answer


















  • 1





    you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

    – Jon Clements
    Mar 2 at 3:11







  • 3





    This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

    – blhsing
    Mar 2 at 3:46












  • @JonClements nice but in that case I would create a string beforehand.

    – Jean-François Fabre
    Mar 2 at 5:21


















2














I would create a cycling generator and slice that:



from itertools import cycle, islice

num = 5678901234

num = tuple(str(num))
print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))


This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:



def digits(num):
while num:
yield num % 10
num //= 10

def check(num):
num = list(digits(num))
num.reverse()
for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
if i != j:
return False
return True





share|improve this answer
































    2














    Since you already have the zip version, here is an alternative solution:



    import sys


    order = dict(enumerate(range(10)))
    order[0] = 10

    def increasing(n):
    n = abs(n)
    o = order[n % 10] + 1
    while n:
    n, r = divmod(n, 10)
    if o - order[r] != 1:
    return False
    o = order[r]
    return True


    for n in sys.argv[1:]:
    print n, increasing(int(n))





    share|improve this answer




















    • 1





      n, r = divmod(n, 10)

      – thebjorn
      Mar 5 at 18:41











    • thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

      – khachik
      Mar 5 at 19:28


















    1














    Here's my take that just looks at the digits and exits as soon as there is a discrepancy:



    def f(n):
    while (n):
    last = n % 10
    n = n / 10
    if n == 0:
    return True
    prev = n % 10
    print last, prev
    if prev == 0 or prev != (last - 1) % 10:
    return False

    print f(1234)
    print f(7890)
    print f(78901)
    print f(1345)





    share|improve this answer

























    • This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

      – TerryA
      Mar 2 at 8:23


















    0














    Somehow this question got me thinking of Palindromes and that got me to thinking of this in a different way.



    5 6 7 8
    8 7 6 5
    -------------
    13 13 13 13

    9 0 1
    1 0 9
    ---------
    10 0 10


    9 0 1 2
    2 1 0 9
    -------------
    11 1 1 11


    And that leads to this solution and tests.



    pos_test_data = [5678, 901, 9012, 9012345678901]
    neg_test_data = [5876, 910, 9021]

    def monotonic_by_one(n):
    fwd = str(n)
    tgt = ord(fwd[0]) + ord(fwd[-1])
    return all([ord(f) + ord(r) in (tgt, tgt - 10) for f, r in zip(fwd, reversed(fwd))])


    print("Positive: ", all([monotonic_by_one(n) for n in pos_test_data]))
    print("Negative: ", all([not monotonic_by_one(n) for n in neg_test_data]))


    Results:



    Positive: True
    Negative: True


    Instead of using to full list comprehension you could use a for loop and bail out at the first failure. I'd want to look at the size of the numbers being checked and time somethings to decide which was faster.






    share|improve this answer






























      0














      I would try this, a little verbose for readability:



      seq = list(input())
      seq1 = seq[1:]
      seq2 = seq[:-1]

      diff = [x-y for x,y in zip([int(x) if int(x)>0 else 10 for x in seq1],[int(x) if int(x)>0 else 10 for x in seq2])]

      if any (t != 1 for t in diff) :
      print('not <<<<<')
      else :
      print('<<<<<<')





      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%2f54954713%2fcheck-if-the-digits-in-the-number-are-in-increasing-sequence-in-python%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        18














        You can simply check if the number, when converted to a string, is a substring of '1234567890':



        str(num) in '1234567890'





        share|improve this answer


















        • 2





          simple and answers perfectly. Good job.

          – Jean-François Fabre
          Mar 2 at 6:53











        • This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

          – Jörg W Mittag
          Mar 2 at 7:31






        • 4





          @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

          – blhsing
          Mar 2 at 7:53
















        18














        You can simply check if the number, when converted to a string, is a substring of '1234567890':



        str(num) in '1234567890'





        share|improve this answer


















        • 2





          simple and answers perfectly. Good job.

          – Jean-François Fabre
          Mar 2 at 6:53











        • This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

          – Jörg W Mittag
          Mar 2 at 7:31






        • 4





          @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

          – blhsing
          Mar 2 at 7:53














        18












        18








        18







        You can simply check if the number, when converted to a string, is a substring of '1234567890':



        str(num) in '1234567890'





        share|improve this answer













        You can simply check if the number, when converted to a string, is a substring of '1234567890':



        str(num) in '1234567890'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 2 at 3:51









        blhsingblhsing

        41.2k41743




        41.2k41743







        • 2





          simple and answers perfectly. Good job.

          – Jean-François Fabre
          Mar 2 at 6:53











        • This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

          – Jörg W Mittag
          Mar 2 at 7:31






        • 4





          @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

          – blhsing
          Mar 2 at 7:53













        • 2





          simple and answers perfectly. Good job.

          – Jean-François Fabre
          Mar 2 at 6:53











        • This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

          – Jörg W Mittag
          Mar 2 at 7:31






        • 4





          @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

          – blhsing
          Mar 2 at 7:53








        2




        2





        simple and answers perfectly. Good job.

        – Jean-François Fabre
        Mar 2 at 6:53





        simple and answers perfectly. Good job.

        – Jean-François Fabre
        Mar 2 at 6:53













        This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

        – Jörg W Mittag
        Mar 2 at 7:31





        This fails, for example for '901', which is increasing according to the OP's definition (0 comes after 9 and obviously 1 comes after 0).

        – Jörg W Mittag
        Mar 2 at 7:31




        4




        4





        @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

        – blhsing
        Mar 2 at 7:53






        @JörgWMittag No, the OP says "0 should come after 9, and not before 1", so 901 is no a valid increasing sequence because 0 should not come before 1.

        – blhsing
        Mar 2 at 7:53














        3














        you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all to check that numbers follow, using a modulo 10 to handle the 0 case.



        num = 7890

        result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))





        share|improve this answer


















        • 1





          you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

          – Jon Clements
          Mar 2 at 3:11







        • 3





          This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

          – blhsing
          Mar 2 at 3:46












        • @JonClements nice but in that case I would create a string beforehand.

          – Jean-François Fabre
          Mar 2 at 5:21















        3














        you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all to check that numbers follow, using a modulo 10 to handle the 0 case.



        num = 7890

        result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))





        share|improve this answer


















        • 1





          you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

          – Jon Clements
          Mar 2 at 3:11







        • 3





          This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

          – blhsing
          Mar 2 at 3:46












        • @JonClements nice but in that case I would create a string beforehand.

          – Jean-François Fabre
          Mar 2 at 5:21













        3












        3








        3







        you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all to check that numbers follow, using a modulo 10 to handle the 0 case.



        num = 7890

        result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))





        share|improve this answer













        you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all to check that numbers follow, using a modulo 10 to handle the 0 case.



        num = 7890

        result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 2 at 2:43









        Jean-François FabreJean-François Fabre

        106k1057115




        106k1057115







        • 1





          you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

          – Jon Clements
          Mar 2 at 3:11







        • 3





          This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

          – blhsing
          Mar 2 at 3:46












        • @JonClements nice but in that case I would create a string beforehand.

          – Jean-François Fabre
          Mar 2 at 5:21












        • 1





          you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

          – Jon Clements
          Mar 2 at 3:11







        • 3





          This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

          – blhsing
          Mar 2 at 3:46












        • @JonClements nice but in that case I would create a string beforehand.

          – Jean-François Fabre
          Mar 2 at 5:21







        1




        1





        you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

        – Jon Clements
        Mar 2 at 3:11






        you can avoid the double str'ing and slicing by using zip(*[iter(str(num))] * 2) instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...

        – Jon Clements
        Mar 2 at 3:11





        3




        3





        This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

        – blhsing
        Mar 2 at 3:46






        This would incorrectly return True for 78901, as the OP says "0 should come after 9, and not before 1".

        – blhsing
        Mar 2 at 3:46














        @JonClements nice but in that case I would create a string beforehand.

        – Jean-François Fabre
        Mar 2 at 5:21





        @JonClements nice but in that case I would create a string beforehand.

        – Jean-François Fabre
        Mar 2 at 5:21











        2














        I would create a cycling generator and slice that:



        from itertools import cycle, islice

        num = 5678901234

        num = tuple(str(num))
        print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))


        This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:



        def digits(num):
        while num:
        yield num % 10
        num //= 10

        def check(num):
        num = list(digits(num))
        num.reverse()
        for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
        if i != j:
        return False
        return True





        share|improve this answer





























          2














          I would create a cycling generator and slice that:



          from itertools import cycle, islice

          num = 5678901234

          num = tuple(str(num))
          print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))


          This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:



          def digits(num):
          while num:
          yield num % 10
          num //= 10

          def check(num):
          num = list(digits(num))
          num.reverse()
          for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
          if i != j:
          return False
          return True





          share|improve this answer



























            2












            2








            2







            I would create a cycling generator and slice that:



            from itertools import cycle, islice

            num = 5678901234

            num = tuple(str(num))
            print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))


            This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:



            def digits(num):
            while num:
            yield num % 10
            num //= 10

            def check(num):
            num = list(digits(num))
            num.reverse()
            for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
            if i != j:
            return False
            return True





            share|improve this answer















            I would create a cycling generator and slice that:



            from itertools import cycle, islice

            num = 5678901234

            num = tuple(str(num))
            print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))


            This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:



            def digits(num):
            while num:
            yield num % 10
            num //= 10

            def check(num):
            num = list(digits(num))
            num.reverse()
            for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
            if i != j:
            return False
            return True






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 2 at 3:07

























            answered Mar 2 at 2:48









            Tomothy32Tomothy32

            8,4031728




            8,4031728





















                2














                Since you already have the zip version, here is an alternative solution:



                import sys


                order = dict(enumerate(range(10)))
                order[0] = 10

                def increasing(n):
                n = abs(n)
                o = order[n % 10] + 1
                while n:
                n, r = divmod(n, 10)
                if o - order[r] != 1:
                return False
                o = order[r]
                return True


                for n in sys.argv[1:]:
                print n, increasing(int(n))





                share|improve this answer




















                • 1





                  n, r = divmod(n, 10)

                  – thebjorn
                  Mar 5 at 18:41











                • thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

                  – khachik
                  Mar 5 at 19:28















                2














                Since you already have the zip version, here is an alternative solution:



                import sys


                order = dict(enumerate(range(10)))
                order[0] = 10

                def increasing(n):
                n = abs(n)
                o = order[n % 10] + 1
                while n:
                n, r = divmod(n, 10)
                if o - order[r] != 1:
                return False
                o = order[r]
                return True


                for n in sys.argv[1:]:
                print n, increasing(int(n))





                share|improve this answer




















                • 1





                  n, r = divmod(n, 10)

                  – thebjorn
                  Mar 5 at 18:41











                • thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

                  – khachik
                  Mar 5 at 19:28













                2












                2








                2







                Since you already have the zip version, here is an alternative solution:



                import sys


                order = dict(enumerate(range(10)))
                order[0] = 10

                def increasing(n):
                n = abs(n)
                o = order[n % 10] + 1
                while n:
                n, r = divmod(n, 10)
                if o - order[r] != 1:
                return False
                o = order[r]
                return True


                for n in sys.argv[1:]:
                print n, increasing(int(n))





                share|improve this answer















                Since you already have the zip version, here is an alternative solution:



                import sys


                order = dict(enumerate(range(10)))
                order[0] = 10

                def increasing(n):
                n = abs(n)
                o = order[n % 10] + 1
                while n:
                n, r = divmod(n, 10)
                if o - order[r] != 1:
                return False
                o = order[r]
                return True


                for n in sys.argv[1:]:
                print n, increasing(int(n))






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 5 at 19:27

























                answered Mar 2 at 2:43









                khachikkhachik

                21.3k64582




                21.3k64582







                • 1





                  n, r = divmod(n, 10)

                  – thebjorn
                  Mar 5 at 18:41











                • thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

                  – khachik
                  Mar 5 at 19:28












                • 1





                  n, r = divmod(n, 10)

                  – thebjorn
                  Mar 5 at 18:41











                • thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

                  – khachik
                  Mar 5 at 19:28







                1




                1





                n, r = divmod(n, 10)

                – thebjorn
                Mar 5 at 18:41





                n, r = divmod(n, 10)

                – thebjorn
                Mar 5 at 18:41













                thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

                – khachik
                Mar 5 at 19:28





                thanks @thebjorn, amazingly, on SOI keep learning new stuff every day :)

                – khachik
                Mar 5 at 19:28











                1














                Here's my take that just looks at the digits and exits as soon as there is a discrepancy:



                def f(n):
                while (n):
                last = n % 10
                n = n / 10
                if n == 0:
                return True
                prev = n % 10
                print last, prev
                if prev == 0 or prev != (last - 1) % 10:
                return False

                print f(1234)
                print f(7890)
                print f(78901)
                print f(1345)





                share|improve this answer

























                • This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

                  – TerryA
                  Mar 2 at 8:23















                1














                Here's my take that just looks at the digits and exits as soon as there is a discrepancy:



                def f(n):
                while (n):
                last = n % 10
                n = n / 10
                if n == 0:
                return True
                prev = n % 10
                print last, prev
                if prev == 0 or prev != (last - 1) % 10:
                return False

                print f(1234)
                print f(7890)
                print f(78901)
                print f(1345)





                share|improve this answer

























                • This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

                  – TerryA
                  Mar 2 at 8:23













                1












                1








                1







                Here's my take that just looks at the digits and exits as soon as there is a discrepancy:



                def f(n):
                while (n):
                last = n % 10
                n = n / 10
                if n == 0:
                return True
                prev = n % 10
                print last, prev
                if prev == 0 or prev != (last - 1) % 10:
                return False

                print f(1234)
                print f(7890)
                print f(78901)
                print f(1345)





                share|improve this answer















                Here's my take that just looks at the digits and exits as soon as there is a discrepancy:



                def f(n):
                while (n):
                last = n % 10
                n = n / 10
                if n == 0:
                return True
                prev = n % 10
                print last, prev
                if prev == 0 or prev != (last - 1) % 10:
                return False

                print f(1234)
                print f(7890)
                print f(78901)
                print f(1345)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 2 at 12:19

























                answered Mar 2 at 5:05









                גלעד ברקןגלעד ברקן

                13.4k21544




                13.4k21544












                • This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

                  – TerryA
                  Mar 2 at 8:23

















                • This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

                  – TerryA
                  Mar 2 at 8:23
















                This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

                – TerryA
                Mar 2 at 8:23





                This should be the accepted answer. No string conversion, pure maths. Guaranteed to be much faster

                – TerryA
                Mar 2 at 8:23











                0














                Somehow this question got me thinking of Palindromes and that got me to thinking of this in a different way.



                5 6 7 8
                8 7 6 5
                -------------
                13 13 13 13

                9 0 1
                1 0 9
                ---------
                10 0 10


                9 0 1 2
                2 1 0 9
                -------------
                11 1 1 11


                And that leads to this solution and tests.



                pos_test_data = [5678, 901, 9012, 9012345678901]
                neg_test_data = [5876, 910, 9021]

                def monotonic_by_one(n):
                fwd = str(n)
                tgt = ord(fwd[0]) + ord(fwd[-1])
                return all([ord(f) + ord(r) in (tgt, tgt - 10) for f, r in zip(fwd, reversed(fwd))])


                print("Positive: ", all([monotonic_by_one(n) for n in pos_test_data]))
                print("Negative: ", all([not monotonic_by_one(n) for n in neg_test_data]))


                Results:



                Positive: True
                Negative: True


                Instead of using to full list comprehension you could use a for loop and bail out at the first failure. I'd want to look at the size of the numbers being checked and time somethings to decide which was faster.






                share|improve this answer



























                  0














                  Somehow this question got me thinking of Palindromes and that got me to thinking of this in a different way.



                  5 6 7 8
                  8 7 6 5
                  -------------
                  13 13 13 13

                  9 0 1
                  1 0 9
                  ---------
                  10 0 10


                  9 0 1 2
                  2 1 0 9
                  -------------
                  11 1 1 11


                  And that leads to this solution and tests.



                  pos_test_data = [5678, 901, 9012, 9012345678901]
                  neg_test_data = [5876, 910, 9021]

                  def monotonic_by_one(n):
                  fwd = str(n)
                  tgt = ord(fwd[0]) + ord(fwd[-1])
                  return all([ord(f) + ord(r) in (tgt, tgt - 10) for f, r in zip(fwd, reversed(fwd))])


                  print("Positive: ", all([monotonic_by_one(n) for n in pos_test_data]))
                  print("Negative: ", all([not monotonic_by_one(n) for n in neg_test_data]))


                  Results:



                  Positive: True
                  Negative: True


                  Instead of using to full list comprehension you could use a for loop and bail out at the first failure. I'd want to look at the size of the numbers being checked and time somethings to decide which was faster.






                  share|improve this answer

























                    0












                    0








                    0







                    Somehow this question got me thinking of Palindromes and that got me to thinking of this in a different way.



                    5 6 7 8
                    8 7 6 5
                    -------------
                    13 13 13 13

                    9 0 1
                    1 0 9
                    ---------
                    10 0 10


                    9 0 1 2
                    2 1 0 9
                    -------------
                    11 1 1 11


                    And that leads to this solution and tests.



                    pos_test_data = [5678, 901, 9012, 9012345678901]
                    neg_test_data = [5876, 910, 9021]

                    def monotonic_by_one(n):
                    fwd = str(n)
                    tgt = ord(fwd[0]) + ord(fwd[-1])
                    return all([ord(f) + ord(r) in (tgt, tgt - 10) for f, r in zip(fwd, reversed(fwd))])


                    print("Positive: ", all([monotonic_by_one(n) for n in pos_test_data]))
                    print("Negative: ", all([not monotonic_by_one(n) for n in neg_test_data]))


                    Results:



                    Positive: True
                    Negative: True


                    Instead of using to full list comprehension you could use a for loop and bail out at the first failure. I'd want to look at the size of the numbers being checked and time somethings to decide which was faster.






                    share|improve this answer













                    Somehow this question got me thinking of Palindromes and that got me to thinking of this in a different way.



                    5 6 7 8
                    8 7 6 5
                    -------------
                    13 13 13 13

                    9 0 1
                    1 0 9
                    ---------
                    10 0 10


                    9 0 1 2
                    2 1 0 9
                    -------------
                    11 1 1 11


                    And that leads to this solution and tests.



                    pos_test_data = [5678, 901, 9012, 9012345678901]
                    neg_test_data = [5876, 910, 9021]

                    def monotonic_by_one(n):
                    fwd = str(n)
                    tgt = ord(fwd[0]) + ord(fwd[-1])
                    return all([ord(f) + ord(r) in (tgt, tgt - 10) for f, r in zip(fwd, reversed(fwd))])


                    print("Positive: ", all([monotonic_by_one(n) for n in pos_test_data]))
                    print("Negative: ", all([not monotonic_by_one(n) for n in neg_test_data]))


                    Results:



                    Positive: True
                    Negative: True


                    Instead of using to full list comprehension you could use a for loop and bail out at the first failure. I'd want to look at the size of the numbers being checked and time somethings to decide which was faster.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 3 at 2:28









                    Nathan AtkinsNathan Atkins

                    1




                    1





















                        0














                        I would try this, a little verbose for readability:



                        seq = list(input())
                        seq1 = seq[1:]
                        seq2 = seq[:-1]

                        diff = [x-y for x,y in zip([int(x) if int(x)>0 else 10 for x in seq1],[int(x) if int(x)>0 else 10 for x in seq2])]

                        if any (t != 1 for t in diff) :
                        print('not <<<<<')
                        else :
                        print('<<<<<<')





                        share|improve this answer



























                          0














                          I would try this, a little verbose for readability:



                          seq = list(input())
                          seq1 = seq[1:]
                          seq2 = seq[:-1]

                          diff = [x-y for x,y in zip([int(x) if int(x)>0 else 10 for x in seq1],[int(x) if int(x)>0 else 10 for x in seq2])]

                          if any (t != 1 for t in diff) :
                          print('not <<<<<')
                          else :
                          print('<<<<<<')





                          share|improve this answer

























                            0












                            0








                            0







                            I would try this, a little verbose for readability:



                            seq = list(input())
                            seq1 = seq[1:]
                            seq2 = seq[:-1]

                            diff = [x-y for x,y in zip([int(x) if int(x)>0 else 10 for x in seq1],[int(x) if int(x)>0 else 10 for x in seq2])]

                            if any (t != 1 for t in diff) :
                            print('not <<<<<')
                            else :
                            print('<<<<<<')





                            share|improve this answer













                            I would try this, a little verbose for readability:



                            seq = list(input())
                            seq1 = seq[1:]
                            seq2 = seq[:-1]

                            diff = [x-y for x,y in zip([int(x) if int(x)>0 else 10 for x in seq1],[int(x) if int(x)>0 else 10 for x in seq2])]

                            if any (t != 1 for t in diff) :
                            print('not <<<<<')
                            else :
                            print('<<<<<<')






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 9 at 9:12









                            synapsionsynapsion

                            1




                            1



























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54954713%2fcheck-if-the-digits-in-the-number-are-in-increasing-sequence-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

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?