Creating a function that returns an increasing sequence
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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 lastlen-1
and firstlen-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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python list boolean func
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 lastlen-1
and firstlen-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
add a comment |
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 lastlen-1
and firstlen-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
add a comment |
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
add a comment |
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.
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 11 at 16:50
answered Nov 11 at 16:35
theplatypus
465
465
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 12 at 0:41
answered Nov 11 at 16:36
b-fg
1,70911422
1,70911422
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 firstlen-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