Creating a function that returns an increasing sequence









up vote
0
down vote

favorite
1












I am trying to make a function that for a sequence of integers as an array can determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. If an element can be remove than the output is True otherwise return False. I tried,



def almostIncreasingSequence(sequence):

if sequence[:-1] == sequence[1::]:
return True
else:
return False


It works for list,



 sequence = [1, 3, 2, 1]
>>> False


Since you cannot remove any number that would lead to an increasing sequence. However, if the list was



sequence: [1, 3, 2]
>>> True


It is true since you can remove 2 or 3 to have an increasing sequence. My function incorrectly outputs False though.










share|improve this question

















  • 4




    It's unclear how the code you tried would ever be used to solve the problem described in the question. All it checks is that the last len-1 and first len-1 items are the same; it doesn't check that anything is strictly increasing or consider the consequence of removing any intermediate item. I'm not sure if this counts as a good-faith attempt to solve the problem... it might help to include, in the OP, a description of how your algorithm is intended to work.
    – Ollin Boer Bohan
    Nov 11 at 16:24














up vote
0
down vote

favorite
1












I am trying to make a function that for a sequence of integers as an array can determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. If an element can be remove than the output is True otherwise return False. I tried,



def almostIncreasingSequence(sequence):

if sequence[:-1] == sequence[1::]:
return True
else:
return False


It works for list,



 sequence = [1, 3, 2, 1]
>>> False


Since you cannot remove any number that would lead to an increasing sequence. However, if the list was



sequence: [1, 3, 2]
>>> True


It is true since you can remove 2 or 3 to have an increasing sequence. My function incorrectly outputs False though.










share|improve this question

















  • 4




    It's unclear how the code you tried would ever be used to solve the problem described in the question. All it checks is that the last len-1 and first len-1 items are the same; it doesn't check that anything is strictly increasing or consider the consequence of removing any intermediate item. I'm not sure if this counts as a good-faith attempt to solve the problem... it might help to include, in the OP, a description of how your algorithm is intended to work.
    – Ollin Boer Bohan
    Nov 11 at 16:24












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am trying to make a function that for a sequence of integers as an array can determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. If an element can be remove than the output is True otherwise return False. I tried,



def almostIncreasingSequence(sequence):

if sequence[:-1] == sequence[1::]:
return True
else:
return False


It works for list,



 sequence = [1, 3, 2, 1]
>>> False


Since you cannot remove any number that would lead to an increasing sequence. However, if the list was



sequence: [1, 3, 2]
>>> True


It is true since you can remove 2 or 3 to have an increasing sequence. My function incorrectly outputs False though.










share|improve this question













I am trying to make a function that for a sequence of integers as an array can determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. If an element can be remove than the output is True otherwise return False. I tried,



def almostIncreasingSequence(sequence):

if sequence[:-1] == sequence[1::]:
return True
else:
return False


It works for list,



 sequence = [1, 3, 2, 1]
>>> False


Since you cannot remove any number that would lead to an increasing sequence. However, if the list was



sequence: [1, 3, 2]
>>> True


It is true since you can remove 2 or 3 to have an increasing sequence. My function incorrectly outputs False though.







python list boolean func






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:14









user1821176

3161420




3161420







  • 4




    It's unclear how the code you tried would ever be used to solve the problem described in the question. All it checks is that the last len-1 and first len-1 items are the same; it doesn't check that anything is strictly increasing or consider the consequence of removing any intermediate item. I'm not sure if this counts as a good-faith attempt to solve the problem... it might help to include, in the OP, a description of how your algorithm is intended to work.
    – Ollin Boer Bohan
    Nov 11 at 16:24












  • 4




    It's unclear how the code you tried would ever be used to solve the problem described in the question. All it checks is that the last len-1 and first len-1 items are the same; it doesn't check that anything is strictly increasing or consider the consequence of removing any intermediate item. I'm not sure if this counts as a good-faith attempt to solve the problem... it might help to include, in the OP, a description of how your algorithm is intended to work.
    – Ollin Boer Bohan
    Nov 11 at 16:24







4




4




It's unclear how the code you tried would ever be used to solve the problem described in the question. All it checks is that the last len-1 and first len-1 items are the same; it doesn't check that anything is strictly increasing or consider the consequence of removing any intermediate item. I'm not sure if this counts as a good-faith attempt to solve the problem... it might help to include, in the OP, a description of how your algorithm is intended to work.
– Ollin Boer Bohan
Nov 11 at 16:24




It's unclear how the code you tried would ever be used to solve the problem described in the question. All it checks is that the last len-1 and first len-1 items are the same; it doesn't check that anything is strictly increasing or consider the consequence of removing any intermediate item. I'm not sure if this counts as a good-faith attempt to solve the problem... it might help to include, in the OP, a description of how your algorithm is intended to work.
– Ollin Boer Bohan
Nov 11 at 16:24












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










I really don't see what was your first idea...
How about a more simple solution ?



def fn(seq):
last_i = None
lives = 1
for i in seq :
if last_i is None :
last_i = i
else :
if (i <= last_i):
lives = lives - 1
if (lives < 0) :
return False
last_i = i
return True

>>> fn([1, 3, 2, 1])
False
>>> fn([1, 3, 2])
True
>>> fn([1, 3, 2, 3])
True
>>> fn([1, 3, 2, 4, 6, 8])
True
>>> fn([1, 3, 2, 4, 6, 8, 2])
False





share|improve this answer





























    up vote
    0
    down vote













    The code below uses the monotonicity check as wrote in this answer and iterates over the elements of the list to check if poping a single element results in increasing monotonicity.



    def strictly_increasing(L):
    return all(x<y for x, y in zip(L, L[1:]))
    def non_decreasing(L):
    return all(x<=y for x, y in zip(L, L[1:]))

    L = [1, 3, 2]
    L_mod = L.copy()
    my_bool = False
    if not strictly_increasing(L):
    for i, x in enumerate(L):
    L_mod.pop(i)
    if strictly_increasing(L_mod):
    my_bool = True
    exit
    else: L_mod = L.copy()
    else:
    my_bool = True


    Use strictly_increasing or non_decreasing as you wish.






    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%2f53250641%2fcreating-a-function-that-returns-an-increasing-sequence%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








      up vote
      3
      down vote



      accepted










      I really don't see what was your first idea...
      How about a more simple solution ?



      def fn(seq):
      last_i = None
      lives = 1
      for i in seq :
      if last_i is None :
      last_i = i
      else :
      if (i <= last_i):
      lives = lives - 1
      if (lives < 0) :
      return False
      last_i = i
      return True

      >>> fn([1, 3, 2, 1])
      False
      >>> fn([1, 3, 2])
      True
      >>> fn([1, 3, 2, 3])
      True
      >>> fn([1, 3, 2, 4, 6, 8])
      True
      >>> fn([1, 3, 2, 4, 6, 8, 2])
      False





      share|improve this answer


























        up vote
        3
        down vote



        accepted










        I really don't see what was your first idea...
        How about a more simple solution ?



        def fn(seq):
        last_i = None
        lives = 1
        for i in seq :
        if last_i is None :
        last_i = i
        else :
        if (i <= last_i):
        lives = lives - 1
        if (lives < 0) :
        return False
        last_i = i
        return True

        >>> fn([1, 3, 2, 1])
        False
        >>> fn([1, 3, 2])
        True
        >>> fn([1, 3, 2, 3])
        True
        >>> fn([1, 3, 2, 4, 6, 8])
        True
        >>> fn([1, 3, 2, 4, 6, 8, 2])
        False





        share|improve this answer
























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          I really don't see what was your first idea...
          How about a more simple solution ?



          def fn(seq):
          last_i = None
          lives = 1
          for i in seq :
          if last_i is None :
          last_i = i
          else :
          if (i <= last_i):
          lives = lives - 1
          if (lives < 0) :
          return False
          last_i = i
          return True

          >>> fn([1, 3, 2, 1])
          False
          >>> fn([1, 3, 2])
          True
          >>> fn([1, 3, 2, 3])
          True
          >>> fn([1, 3, 2, 4, 6, 8])
          True
          >>> fn([1, 3, 2, 4, 6, 8, 2])
          False





          share|improve this answer














          I really don't see what was your first idea...
          How about a more simple solution ?



          def fn(seq):
          last_i = None
          lives = 1
          for i in seq :
          if last_i is None :
          last_i = i
          else :
          if (i <= last_i):
          lives = lives - 1
          if (lives < 0) :
          return False
          last_i = i
          return True

          >>> fn([1, 3, 2, 1])
          False
          >>> fn([1, 3, 2])
          True
          >>> fn([1, 3, 2, 3])
          True
          >>> fn([1, 3, 2, 4, 6, 8])
          True
          >>> fn([1, 3, 2, 4, 6, 8, 2])
          False






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 16:50

























          answered Nov 11 at 16:35









          theplatypus

          465




          465






















              up vote
              0
              down vote













              The code below uses the monotonicity check as wrote in this answer and iterates over the elements of the list to check if poping a single element results in increasing monotonicity.



              def strictly_increasing(L):
              return all(x<y for x, y in zip(L, L[1:]))
              def non_decreasing(L):
              return all(x<=y for x, y in zip(L, L[1:]))

              L = [1, 3, 2]
              L_mod = L.copy()
              my_bool = False
              if not strictly_increasing(L):
              for i, x in enumerate(L):
              L_mod.pop(i)
              if strictly_increasing(L_mod):
              my_bool = True
              exit
              else: L_mod = L.copy()
              else:
              my_bool = True


              Use strictly_increasing or non_decreasing as you wish.






              share|improve this answer


























                up vote
                0
                down vote













                The code below uses the monotonicity check as wrote in this answer and iterates over the elements of the list to check if poping a single element results in increasing monotonicity.



                def strictly_increasing(L):
                return all(x<y for x, y in zip(L, L[1:]))
                def non_decreasing(L):
                return all(x<=y for x, y in zip(L, L[1:]))

                L = [1, 3, 2]
                L_mod = L.copy()
                my_bool = False
                if not strictly_increasing(L):
                for i, x in enumerate(L):
                L_mod.pop(i)
                if strictly_increasing(L_mod):
                my_bool = True
                exit
                else: L_mod = L.copy()
                else:
                my_bool = True


                Use strictly_increasing or non_decreasing as you wish.






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The code below uses the monotonicity check as wrote in this answer and iterates over the elements of the list to check if poping a single element results in increasing monotonicity.



                  def strictly_increasing(L):
                  return all(x<y for x, y in zip(L, L[1:]))
                  def non_decreasing(L):
                  return all(x<=y for x, y in zip(L, L[1:]))

                  L = [1, 3, 2]
                  L_mod = L.copy()
                  my_bool = False
                  if not strictly_increasing(L):
                  for i, x in enumerate(L):
                  L_mod.pop(i)
                  if strictly_increasing(L_mod):
                  my_bool = True
                  exit
                  else: L_mod = L.copy()
                  else:
                  my_bool = True


                  Use strictly_increasing or non_decreasing as you wish.






                  share|improve this answer














                  The code below uses the monotonicity check as wrote in this answer and iterates over the elements of the list to check if poping a single element results in increasing monotonicity.



                  def strictly_increasing(L):
                  return all(x<y for x, y in zip(L, L[1:]))
                  def non_decreasing(L):
                  return all(x<=y for x, y in zip(L, L[1:]))

                  L = [1, 3, 2]
                  L_mod = L.copy()
                  my_bool = False
                  if not strictly_increasing(L):
                  for i, x in enumerate(L):
                  L_mod.pop(i)
                  if strictly_increasing(L_mod):
                  my_bool = True
                  exit
                  else: L_mod = L.copy()
                  else:
                  my_bool = True


                  Use strictly_increasing or non_decreasing as you wish.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 0:41

























                  answered Nov 11 at 16:36









                  b-fg

                  1,70911422




                  1,70911422



























                      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%2f53250641%2fcreating-a-function-that-returns-an-increasing-sequence%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto