Recent

Author Topic: Challenge: Almost Increasing Sequence  (Read 21989 times)

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Challenge: Almost Increasing Sequence
« on: April 06, 2017, 10:50:20 pm »
Hi, this is a test from the website CodeFights, where you can test your coding skills. Unfortunately they don't have Pascal as a language to do the challenges, but I come to an interesting one that has been solved only by ~6000 people only in that website.

Personally at this time I've solved only 25 of 26 test cases. So is not solved for me.

A thing to take into consideration when trying to solve this is than you have a time limit of execution. In the case of JavaScript is 4000 ms, in the case of C++ is 500 ms, so for Pascal we can have something similar, how fast you can run this test in Pascal?

Hope you enjoy doing this test!

This is the actual test:

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Example

For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Input/Output

[time limit] 4000ms (js)
[input] array.integer sequence

Guaranteed constraints:
2 ≤ sequence.length ≤ 10^5,
-10^5 ≤ sequence[ i ] ≤ 10^5.

[output] boolean

Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.

All test cases:

I buyed all test cases, here are them all:

Test1
Input:
sequence: [1, 3, 2, 1]
Output:
false
Expected Output:
false

Test2
Input:
sequence: [1, 3, 2]
Output:
true
Expected Output:
true

Test3:
Input:
sequence: [1, 2, 1, 2]
Output:
false
Expected Output:
false

Test4:
Input:
sequence: [1, 4, 10, 4, 2]
Output:
false
Expected Output:
false

Test5:
Input:
sequence: [10, 1, 2, 3, 4, 5]
Output:
true
Expected Output:
true

Test6:
Input:
sequence: [1, 1, 1, 2, 3]
Output:
false
Expected Output:
false

Test7:
Input:
sequence: [0, -2, 5, 6]
Output:
true
Expected Output:
true

Test8:
Input:
sequence: [1, 2, 3, 4, 5, 3, 5, 6]
Output:
false
Expected Output:
false

Test9:
Input:
sequence: [40, 50, 60, 10, 20, 30]
Output:
false
Expected Output:
false

Test10:
Input:
sequence: [1, 1]
Output:
true
Expected Output:
true

Test11:
Input:
sequence: [10, 1, 2, 3, 4, 5, 6, 1]
Output:
false
Expected Output:
false

Test12:
Input:
sequence: [1, 2, 3, 4, 3, 6]
Output:
true
Expected Output:
true

Test13:
Input:
sequence: [1, 2, 3, 4, 99, 5, 6]
Output:
true
Expected Output:
true

Test14:
Input:
sequence: [1, 1, 1]
Output:
false
Expected Output:
false

Test15:
Input:
sequence: [10, 12, 12, 12, 23]
Output:
false
Expected Output:
false

Test16:
Input:
sequence: [999, -987, -983, -972, -966, -934, -924, -917, -900, -898, -887, -879, -874, -867, -842, -804, -762, -729, -712, -703, -688, -677, -663, -661, -650, -628, -619, -610, -607, -599, -581, -578, -494, -488, -487, -477, -468, -461, -432, -381, -377, -376, -339, -330, -329, -304, -292, -291, -277, -257, -256, -242, -236, -235, -220, -137, -100, -46, -33, -17, -3, -2, 6, 35, 110, 124, 127, 130, 186, 214, 236, 255, 301, 311, 322, 348, 355, 374, 375, 384, 391, 392, 400, 410, 437, 487, 520, 572, 603, 627, 644, 657, 666, 676, 714, 750, 897, 898, 950, 972, 995]
Output:
true
Expected Output:
true

Test17:
Input:
sequence: [-997, -978, -975, -968, -959, -956, -907, -872, -871, -858, -836, -827, -823, -794, -786, -771, -740, -728, -716, -711, -697, -660, -659, -638, -633, -607, -601, -597, -573, -562, -548, -536, -517, -500, -433, -402, -387, -384, -301, -291, -283, -270, -252, -240, -230, -221, -215, -202, -200, -185, -171, -73, -47, -38, -34, -17, -3, 8, 42, 56, 65, 133, 140, 146, 180, 192, 201, 233, 241, 256, 285, 302, 320, 353, 324, 420, 428, 429, 442, 470, 487, 542, 547, 561, 564, 664, 666, 670, 693, 738, 793, 824, 845, 848, 864, 929, 931, 952, 985, 997, 1]
Output:
false
Expected Output:
false

Test18:
Input:
sequence: [1, 2, 3, 1]
Output:
true
Expected Output:
true

Test19:
Input:
sequence: [3, 4, 5, 10, 20, 10, 20, 30]
Output:
false
Expected Output:
false

Test20:
Input:
sequence: [5, 5, 5]
Output:
false
Expected Output:
false

Test21:
Input:
sequence: [-5, -4, -3, -2, 10, 2, 8]
Output:
true
Expected Output:
true

Test22:
Input:
sequence: [5384, 12, 34, 54, 48]
Output:
false
Expected Output:
false

Test23:
Input:
sequence: [1, 4, 5, 2, 3]
Output:
false
Expected Output:
false

Test24:
Input:
sequence: [1, 2, 3, 4, 5, 1, 2, 3, 4]
Output:
false
Expected Output:
false

Test25 and Test26 are stress tests, I will attach them because are too big to post them.
« Last Edit: April 06, 2017, 10:55:49 pm by lainz »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Challenge: Almost Increasing Sequence
« Reply #1 on: April 06, 2017, 11:14:32 pm »
Hope you enjoy doing this test!
Nope, because it is flawed.

Quote
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Example

For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
Wrong. Because, if i remove 1 then i can still produce a increasing sequence.

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Challenge: Almost Increasing Sequence
« Reply #2 on: April 06, 2017, 11:41:04 pm »
Hope you enjoy doing this test!
Nope, because it is flawed.

Quote
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Example

For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
Wrong. Because, if i remove 1 then i can still produce a increasing sequence.

Hi molly, if you remove the first 1 is like this
3, 2, 1
That's not left-to-right increasing sequence.

And if you remove the last 1 is like this
1, 3, 2
That's again not an increasing sequence.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Challenge: Almost Increasing Sequence
« Reply #3 on: April 06, 2017, 11:42:23 pm »
Ah ok. my bad.

I though you was still allowed to reorder the array to make it an increase sequence.

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Challenge: Almost Increasing Sequence
« Reply #4 on: April 06, 2017, 11:44:59 pm »
Ah ok. my bad.

I though you was still allowed to reorder the array to make it an increase sequence.

No problem  :)

Is interesting mostly because I can't solve it fully, that does it more fun and at the same time I hate it.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Challenge: Almost Increasing Sequence
« Reply #5 on: April 07, 2017, 03:01:13 am »
Is interesting mostly because I can't solve it fully, that does it more fun and at the same time I hate it.
i can relate to that :-)

I'm almost there, still had an error (that i conveniently removed from my post)   :)
« Last Edit: April 07, 2017, 03:04:52 am by molly »

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Challenge: Almost Increasing Sequence
« Reply #6 on: April 07, 2017, 03:59:17 am »
The most voted JavaScript solution now has an error, seems that they added more tests recently that breaks old code but covers more possible cases. Because it don't work fully I can post it here (using ecma script 6):

Code: Pascal  [Select][+][-]
  1. //这个还行,简单了点
  2. function almostIncreasingSequence(sequence) {
  3.   return sequence.filter((x,i)=>x>=sequence[i+1]).length<2
  4. }

I've seen a C++ code with 10 lines of code that's the most voted and is really amazing (and works in every single test case), when all interested people is done here I can post it.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Challenge: Almost Increasing Sequence
« Reply #7 on: April 07, 2017, 09:47:51 am »
Why is [1 2 1 2] false but [1 1] true? "strictly increasing", I suppose [1 1] should return false as well, no?

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Challenge: Almost Increasing Sequence
« Reply #8 on: April 07, 2017, 10:14:58 am »
You can remove one, and only one, element from the array, so [1, 1] can become [1] which is a strictly increasing sequence.

I would have tried if we got the initial data. Or do we have to extract it from Lainz' output? I'm all day extracting data from strange places and formatting it properly, I'm lazy to do it again  :D
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Challenge: Almost Increasing Sequence
« Reply #9 on: April 07, 2017, 10:18:13 am »
Why is [1 2 1 2] false but [1 1] true? "strictly increasing", I suppose [1 1] should return false as well, no?
With that i can help:

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

A thing to take into consideration when trying to solve this is than you have a time limit of execution. In the case of JavaScript is 4000 ms, in the case of C++ is 500 ms, so for Pascal we can have something similar, how fast you can run this test in Pascal?
Best that i can come up with atm (this includes the writeln's that show the test results (except last line), so timing should be off)

Quote
Test number 1 ... Passed
Test number 2 ... Passed
Test number 3 ... Passed
Test number 4 ... Passed
Test number 5 ... Passed
Test number 6 ... Passed
Test number 7 ... Passed
Test number 8 ... Passed
Test number 9 ... Passed
Test number 10 ... Passed
Test number 11 ... Passed
Test number 12 ... Passed
Test number 13 ... Passed
Test number 14 ... Passed
Test number 15 ... Passed
Test number 16 ... Passed
Test number 17 ... Passed
Test number 18 ... Passed
Test number 19 ... Passed
Test number 20 ... Passed
Test number 21 ... Passed
Test number 22 ... Passed
Test number 23 ... Passed
Test number 24 ... Passed
Test number 25 ... Passed
Test number 26 ... Passed
Succeeded: 26   Failed: 0  which took 15 ms, ticks = 16

J-G

  • Hero Member
  • *****
  • Posts: 1026
Re: Challenge: Almost Increasing Sequence
« Reply #10 on: April 07, 2017, 11:38:05 am »
You can remove one, and only one, element from the array, so [1, 1] can become [1] which is a strictly increasing sequence.

Whist I accept that in Mathematics the empty sequence (or set) is a valid concept, in this problem there is an additional issue  -   increasing. 

This (for me) means that there must be at least 2 elements and although [1,1] passes that test the sequence doesn't increase. [1] doesn't pass either the 'at least 2 elements' test, or the 'increasing' test so must fail.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Challenge: Almost Increasing Sequence
« Reply #11 on: April 07, 2017, 03:49:47 pm »
You can remove one, and only one, element from the array, so [1, 1] can become [1] which is a strictly increasing sequence.

I would have tried if we got the initial data. Or do we have to extract it from Lainz' output? I'm all day extracting data from strange places and formatting it properly, I'm lazy to do it again  :D

1) You're right. That's the way at least the people that did the test think about it.
2) And sorry for that. In the website we have that automatically, I just did copy and paste of all the inputs. Maybe is better to ask them to add pascal as a new language and play directly in the website :)

Quote
Best that i can come up with atm (this includes the writeln's that show the test results (except last line), so timing should be off)

Amazing molly! You solved all of them in no time so your code is really fast.

john horst

  • Jr. Member
  • **
  • Posts: 68
    • JHorst
Re: Challenge: Almost Increasing Sequence
« Reply #12 on: April 07, 2017, 04:00:07 pm »
Wouldn't the answer have to be false to all, removing 1 from [1,3,2,1] it is equal to [3,2,1] or [1,3,2] neither are strictly increasing sequence.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Challenge: Almost Increasing Sequence
« Reply #13 on: April 07, 2017, 04:00:59 pm »
Amazing molly! You solved all of them in no time so your code is really fast.
Well, your code isn't too bad either, see results below.

I took more a less a similar approach as you did (i waited until i was done before i took a look at your code), just going from it the other way (find the false as quick as possible and bail out, which resulted in very sloppy code because of the corner case exceptions).

Mine:
Code: [Select]
Molly's version:
Test number  1 (      4 items ) ... Passed
Test number  2 (      3 items ) ... Passed
Test number  3 (      4 items ) ... Passed
Test number  4 (      5 items ) ... Passed
Test number  5 (      6 items ) ... Passed
Test number  6 (      5 items ) ... Passed
Test number  7 (      4 items ) ... Passed
Test number  8 (      8 items ) ... Passed
Test number  9 (      6 items ) ... Passed
Test number 10 (      2 items ) ... Passed
Test number 11 (      8 items ) ... Passed
Test number 12 (      6 items ) ... Passed
Test number 13 (      7 items ) ... Passed
Test number 14 (      3 items ) ... Passed
Test number 15 (      5 items ) ... Passed
Test number 16 (    101 items ) ... Passed
Test number 17 (    101 items ) ... Passed
Test number 18 (      4 items ) ... Passed
Test number 19 (      8 items ) ... Passed
Test number 20 (      3 items ) ... Passed
Test number 21 (      7 items ) ... Passed
Test number 22 (      5 items ) ... Passed
Test number 23 (      5 items ) ... Passed
Test number 24 (      9 items ) ... Passed
Test number 25 (  10000 items ) ... Passed
Test number 26 ( 100000 items ) ... Passed
   Succeeded: 26      Failed: 0 Repetitions: 1000    Duration: 2469 ms       ticks = 2469
Lainz:
Code: [Select]
Lainz's version:
Test number  1 (      4 items ) ... Passed
Test number  2 (      3 items ) ... Passed
Test number  3 (      4 items ) ... Passed
Test number  4 (      5 items ) ... Passed
Test number  5 (      6 items ) ... Passed
Test number  6 (      5 items ) ... Passed
Test number  7 (      4 items ) ... Passed
Test number  8 (      8 items ) ... Passed
Test number  9 (      6 items ) ... Passed
Test number 10 (      2 items ) ... Passed
Test number 11 (      8 items ) ... Passed
Test number 12 (      6 items ) ... Passed
Test number 13 (      7 items ) ... Passed
Test number 14 (      3 items ) ... Passed
Test number 15 (      5 items ) ... Passed
Test number 16 (    101 items ) ... Passed
Test number 17 (    101 items ) ... Passed
Test number 18 (      4 items ) ... Passed
Test number 19 (      8 items ) ... Passed
Test number 20 (      3 items ) ... Passed
Test number 21 (      7 items ) ... Passed
Test number 22 (      5 items ) ... Passed
Test number 23 (      5 items ) ... Failed
Test number 24 (      9 items ) ... Passed
Test number 25 (  10000 items ) ... Passed
Test number 26 ( 100000 items ) ... Passed
   Succeeded: 25      Failed: 1 Repetitions: 1000    Duration: 3343 ms       ticks = 3343
i just don't understand why you would need 4000ms ... in any language  :o

edit
CPP:
Code: [Select]
CPP's version:
Test number  1 (      4 items ) ... Passed
Test number  2 (      3 items ) ... Passed
Test number  3 (      4 items ) ... Passed
Test number  4 (      5 items ) ... Passed
Test number  5 (      6 items ) ... Passed
Test number  6 (      5 items ) ... Passed
Test number  7 (      4 items ) ... Passed
Test number  8 (      8 items ) ... Passed
Test number  9 (      6 items ) ... Passed
Test number 10 (      2 items ) ... Passed
Test number 11 (      8 items ) ... Passed
Test number 12 (      6 items ) ... Passed
Test number 13 (      7 items ) ... Passed
Test number 14 (      3 items ) ... Passed
Test number 15 (      5 items ) ... Passed
Test number 16 (    101 items ) ... Passed
Test number 17 (    101 items ) ... Passed
Test number 18 (      4 items ) ... Passed
Test number 19 (      8 items ) ... Passed
Test number 20 (      3 items ) ... Passed
Test number 21 (      7 items ) ... Passed
Test number 22 (      5 items ) ... Passed
Test number 23 (      5 items ) ... Passed
Test number 24 (      9 items ) ... Passed
Test number 25 (  10000 items ) ... Passed
Test number 26 ( 100000 items ) ... Passed
   Succeeded: 26      Failed: 0 Repetitions: 1000    Duration: 1046 ms       ticks = 1047
ouch, beaten by a couple of miles :D
« Last Edit: April 07, 2017, 04:39:58 pm by molly »

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Challenge: Almost Increasing Sequence
« Reply #14 on: April 07, 2017, 04:18:29 pm »
Quote
i just don't understand why you would need 4000ms ... in any language  :o
Well the first attempt I did was really bad and passed the 4000ms, because I didn't stop and let all the loop run, so It's possible to code bad and get a long running. The time limit is to don't let pass these codes that does all test cases well but not with an efficient way.

Edit: I attach here the C++ version most voted from the user "urandom", so you can try how fast it is in pascal.
« Last Edit: April 07, 2017, 04:23:13 pm by lainz »

 

TinyPortal © 2005-2018