Longest Increasing Substring

12

1

Given a list of positive integers, write code that finds the length of longest contiguous sub-list that is increasing (not strictly). That is the longest sublist such that each element is greater than or equal to the last.

For example if the input was:

\$[1,1,2,1,1,4,5,3,2,1,1]\$

The longest increasing sub-list would be \$[1,1,4,5]\$, so you would output \$4\$.

Your answer will be scored by taking its source as a list of bytes and then finding the length of the longest increasing sub-list of that list. A lower score is the goal. Ties are broken in favor of programs with fewer overall bytes.

Post Rock Garf Hunter

Posted 2018-10-07T14:59:35.530

Reputation: 55 382

Is it okay to return true instead of 1? And do we have to handle an empty list? – Jo King – 2018-10-08T05:28:30.987

For your first one, whatever meta consensus is on numeral output you may do, I don't recall True being a substitute for 1 but it may be. You should be able to handle the empty list (Output is of course 0). – Post Rock Garf Hunter – 2018-10-08T07:12:25.183

2Suggested test cases: [] => 0, [0] => 1, [3,2,1] => 1, [1,2,1,2] => 2 – Sok – 2018-10-08T08:22:58.287

Would you mind elaborating on the 'score' a bit more? – ouflak – 2018-10-08T09:58:19.267

1@ouflak I'm not sure what more there is to say on the score. Convert your submission to a list of bytes and pass it through your own program and that's your score. If scores are equal, the tie-breaker is the bytecount – Jo King – 2018-10-08T10:19:06.340

You really should add those test cases. It seems like a majority of these answers don't handle the empty list case correctly. – Jo King – 2018-10-10T07:55:49.527

Answers

6

Pyth, score 2 (8 bytes)

lefSIT.:

Try it here!

Code points [108, 101, 102, 83, 73, 84, 46, 58]. Another shorter solution, leSI#.: scores 3, but its code points are [108, 101, 83, 73, 35, 46, 58], which are very close to a score of 1, actually. Rearranging a bit may help Nevermind, the substrings built-in is .: which cannot be rearranged, so the lowest score must be 2 if the program makes use of it.

How?

lefSIT.:     Full program. Accepts either a list or a string from STDIN.
      .:     Substrings.
  f  T       Only keep those that are...
   SI        Sorting-Invariant.
le           Length of the last item.

Mr. Xcoder

Posted 2018-10-07T14:59:35.530

Reputation: 39 774

5

JavaScript (Node.js), score 3, 53 46 bytes score 2, 51 50 bytes

-7 bytes thanks @Arnauld

+5 +4 spaces in exchange of -1 score

a=> a.map($= p=n=>$=(p<=(p=n)?++ x:x=1)<$?$: x)&&$

Try it online!

Assumes non-empty input. 61 bytes if empty list must be handled. Score 2 still.

a=> a.map($= p=n=>$=(p<=(p=n)?++ x:x=1)<$?$: x)&& a.length&&$

Try it online!

...or 58 if returning false is allowed. Score 2 still.

a=> a.map($= p=n=>$=(p<=(p=n)?++ x:x=1)<$?$: x)&& a>[ ]&&$

Shieru Asakoto

Posted 2018-10-07T14:59:35.530

Reputation: 4 445

This should work for 46 bytes and the same score. – Arnauld – 2018-10-07T16:18:24.917

1@Arnauld added 5 spaces to your suggestion so that it's now a score 2 – Shieru Asakoto – 2018-10-08T00:00:57.503

5

Haskell, score 2, 66 64 61 60 65 bytes

  • -2 bytes thanks to Max Yekhlakov
  • -1 byte thanks to nimi
  • +5 bytes to handle the empty list
foldr1 max.g
g[]=[0]
g(x:y:z)|x>y=1: g(y:z)
g(_:y)|a:b<-g y=1+a:b

Try it online! (verifies itself).

I never thought I could get a score of 2 with Haskell, and yet here I am!

Function g computes the lengths of all the increasing substrings recursively. foldr1 max.g takes the maximum of those lengths (foldr1 max is equivalent to maximum, but with a lower score).

Delfad0r

Posted 2018-10-07T14:59:35.530

Reputation: 1 688

1Looks like the whitespace in 1+a : b is not necessary, so this is 62 bytes. – Max Yekhlakov – 2018-10-08T10:08:57.500

@MaxYekhlakov You're right, I don't know how I missed that. – Delfad0r – 2018-10-08T11:42:27.777

Your code returns 1 for the empty list, where it should return 0 – Jo King – 2018-10-10T07:52:03.667

@Jo King Indeed, I had missed the discussion in the comments. Fixing that right now. – Delfad0r – 2018-10-10T08:12:57.143

4

Husk, 5 bytes, score = 2

00000000: bc6d 4cdc 14                   ▲mLġ≥

Try it online!

It's unlikely to get a score lower than 2 with Husk because ġ1 has a really high codepoint and there needs to be something before it to get the maximum and length. An attempt could be made with trying to use multiple functions but \n would be before any helper functions which has a really low codepoint so anything after it would create an increasing byte-sequence of at least length 2.

1: This seems like the best way to use for the comparison operators would need to follow the various split functions like (span).

Explanation

▲mLġ≥  -- example input: [1,1,2,1,1,4,5,3,2,1,1]
   ġ≥  -- group elements by geq: [[1,1,2],[1,1,4,5],[3],[2],[1,1]]
 mL    -- map length: [3,4,1,1,2]
▲      -- maximum: 4

ბიმო

Posted 2018-10-07T14:59:35.530

Reputation: 15 345

3

MATL, score 2, 13 bytes

d0< ~Y'w)X>sQ

Input can be:

  • An array of numbers.
  • A string enclosed with single quotes. Single quotes within the string are escaped by duplicating.

MATL uses ASCII encoding. The codepoints of the above code are

100, 48, 60, 32, 126, 89, 39, 119, 41, 88, 62, 115, 81

Try it online!

Explanation

d     % Implicit input. Consecutive differences (of code points) 
0<    % Less than 0? Element-wise. Gives true or false
      % Space. This does nothing; but it breaks an increasing substring
~     % Negate
Y'    % Run-length encoding. Gives array of true/false and array of lengths
w     % Swap
)     % Index. This keeps only lenghts of runs of true values
X>    % Maximum. Gives empty array if input is empty
s     % Sum. This turns empty array into 0
Q     % Add 1. Implicit display

Luis Mendo

Posted 2018-10-07T14:59:35.530

Reputation: 87 464

3

Retina 0.8.2, 40 bytes, score 3

\d+
$*
(?<=(1+)),(?!\1)
¶
T`1`_
^O`
\G,?

Try it online! Link includes itself as byte codes as input. Explanation:

\d+
$*

Convert to unary.

(?<=(1+)),(?!\1)
¶

Split on decreasing pairs.

T`1`_

Delete the digits.

^O`

Sort the commas in reverse order. (I would normally write this as O^ but can't do that here for obvious reasons.)

\G,?

Count the longest comma run, and add one to include the final number.

Neil

Posted 2018-10-07T14:59:35.530

Reputation: 95 035

3

Japt -h, 6 bytes, score 2

Don't think a score of 1 is possible. Should work with strings & character arrays too.

ò>¹mÊn

Try it - included test case is the charcodes of the solution.


Explanation

ò          :Partition after each integer
 >         :  That's greater than the integer that follows it
  ¹        :End partition
   m       :Map
    Ê      :  Length
     n     :Sort
           :Implicitly output last element

Shaggy

Posted 2018-10-07T14:59:35.530

Reputation: 24 623

3

Pascal (FPC), score 2

111 bytes

var a,b,c,t:bYte;bEgIn repeat read(a); iNc(c); if a<b then c:=1; if c>t then t:= c;b:= a;until eOf;write(t)eNd.

Try it online!

Assumes non-empty input. Numbers are taken from standard input separated by spaces.

AlexRacer

Posted 2018-10-07T14:59:35.530

Reputation: 979

2

Jelly, 8 bytes, score 2

There is probably a score 1 solution somehow...

IṠµṣ-ZL‘

Try it online!

Source code as a list of byte values:

[73, 205, 9, 223, 45, 90, 76, 252]

How?

IṠµṣ-ZL‘ - Link: list of integers  e.g. [ 1, 1, 2, 1, 1, 4, 5, 3, 2, 1, 1]
I        - increments                    [ 0, 1,-1, 0, 3, 1,-2,-1,-1, 0]
 Ṡ       - sign                          [ 0, 1,-1, 0, 1, 1,-1,-1,-1, 0]
  µ      - start a new monadic chain (a low byte to stop score being 3)
    -    - literal minus one             -1
   ṣ     - split at                      [[0, 1], [0, 1, 1], [], [], [0]]
     Z   - transpose                     [[0, 0, 0], [1, 1], 1]
      L  - length                        3
       ‘ - increment                     4

Jonathan Allan

Posted 2018-10-07T14:59:35.530

Reputation: 67 804

2

Perl 6, score 2, 46 bytes

{my&g=1+*×*;+max 0,|[\[&g]] [ |@_] Z>=0,|@_ }

Try it online!

Handles the empty list. The original code was:

{my&g=1+*×*;+max 0,|[\[&g]] @_ Z>=0,|@_}

So only 5 extra bytes to reduce the score to 2.

Edit: Ah, I figured out how to remove the assignment, but then I can't get that score below 3 because of the )]]...

Explanation:

{                                  }  # Anonymous code block
 my&g=     ;  # Assign to &g an anonymous Whatever lambda
      1+*×*   # That multiplies the two inputs together and adds 1
                            @_ Z  0,|@_   # Zip the list with itself off-set by one
                                >=        # And check if each is equal or larger than the previous
                                         # e.g. [5,7,7,1] => [1,1,1,0]
                    [\[&g]]  # Triangular reduce it by the function declared earlier
                          # This results in a list of the longest substring at each index
                          # e.g. [5,7,7,1] => [1,2,3,1]
            +max 0,|      # And return the max value from this list, returning 0 if empty

Jo King

Posted 2018-10-07T14:59:35.530

Reputation: 38 234

So [[&(*+*)]] works like [+]? Amazing... – nwellnhof – 2018-10-08T11:41:02.957

@nwellnhof Yeah, there's a few caveats like you can't have any whitespace (at all), but you can even use it with Z and X. Try it online!

– Jo King – 2018-10-08T12:02:51.593

1I was going to try something like: {max 0,|.[[X..] ^$_ xx 2].map({+$_ if [<=] $_})} – Brad Gilbert b2gills – 2018-10-19T18:40:47.063

1

05AB1E, score 3 (9 bytes)

Œʒ¥dP}éθg

Can most likely be a score of 2 somehow.

Code points of the program-bytes: [140,1,90,100,80,125,233,9,103] (two sublists of length 3: [1,90,100] and [80,125,233])

Try it online.

Explanation:

Π           # Sublists
 ʒ   }       # Filter by:
  ¥          #  Take the deltas
   d         #  Check for each whether the number is >= 0
    P        #  And check if it was truthy for all deltas
      é      # Then sort by length
       θ     # Take the last element
        g    # And take its length as result

Kevin Cruijssen

Posted 2018-10-07T14:59:35.530

Reputation: 67 575

1

Powershell, score 3, 44 bytes

($args|%{$i*=$_-ge$p;$p=$_;(++$i)}|sort)[-1]

Test script:

$f = {

(
    $args|%{        # for each integer from argument list
        $i*=$_-ge$p # -ge means >=.
                    # This statement multiplies the $i by the comparison result.
                    # A result of a logical operator is 0 or 1.
                    # So, we continue to count a current sequence or start to count a new sequence
        $p=$_       # let $p stores a 'previous integer'
        (++$i)      # increment and return incremented as length of a current sequence
    }|sort          # sort lengthes 
)[-1]               # take last one (maximum)

}

@(
    ,(4, 1,1,2,1,1,4,5,3,2,1,1)
) | % {
    $e,$a = $_
    $r = &$f @a
    "$($r-eq$e): $r"
}

Output:

True: 4

Explanation:

  • The script takes integers as argument list (spaltting).
  • Each integer maps by a function to legnth of contiguous sub-list that is increasing (not strictly). Then the script sorts lengthes and take a last (maximum) (...|sort)[-1].

Powershell 6, score 3, 43 bytes

$args|%{$i*=$_-ge$p;$p=$_;(++$i)}|sort -b 1

Same as above. One difference: sort -b 1 is shortcut for sort -Bottom 1 and means 1 element from the end of sorted array. So we need not an index [-1].

mazzy

Posted 2018-10-07T14:59:35.530

Reputation: 4 832

1

Java (JDK), score 3, 94 bytes

a->{int m=0,p=0,x=0,i=0,n;while(i<a.length){n=a[i++];m=(p<=(p=n)?++x:(x=1)) <m?m:x;}return m;}

Try it online!

Port of my (with suggestions from Arnauld) JS answer. etu in return and hil in while make it impossible to golf to score 2.

for cannot be used here because:

  • ;for is ascending
  • for cannot be used at the beginning of the lambda body (scope restrictions). It is possible to wrap it with {} but apparently using while saves bytes.

Shieru Asakoto

Posted 2018-10-07T14:59:35.530

Reputation: 4 445

I was going to suggest possibly using \u in some places, but then you have to have 00 followed by a digit which is 3 anyway... – ETHproductions – 2018-10-08T18:32:02.283

1

Stax, score 3 (15 bytes)

Z|[F|]F:^!C_%|M

Run and debug it

recursive

Posted 2018-10-07T14:59:35.530

Reputation: 8 616

1

Python 2, score 5, 87 bytes score 2, 101 93 92 101 bytes

lambda a,m=1,o=[1]:max(reduce(lambda B,c:[B[:-m]+[B[-m]+m],B+o][c[0]>c[m]],zip(a,a[m:]), o)) *(a>[ ])

Try it online!

Ooops! Thought this was code-golf first time through...

Chas Brown

Posted 2018-10-07T14:59:35.530

Reputation: 8 959

2

Score of 5. Try it online!

– mypetlion – 2018-10-09T20:55:47.400

2Indent with tabs to get a score of 4. – mypetlion – 2018-10-09T21:15:37.970

@mypetition: D'oh! Thought this was code golf... editing my answer now. – Chas Brown – 2018-10-10T01:31:35.307

Funny that removing the m=1,o=[1] part doesn't end up saving any bytes once we reduce the score

– Jo King – 2018-10-10T03:39:54.820

@Jo King: Chuckle! I kept hoping that by squirming in that way, I could chip off another byte; but no such luck! – Chas Brown – 2018-10-10T04:30:42.463

1

Dyalog APL, score 2, 20 bytes

{⍵≡⍬:0⋄⌈/≢¨⍵⊂⍨1,2>/⍵}

Try it online!

Zacharý

Posted 2018-10-07T14:59:35.530

Reputation: 5 710

0

Wolfram Language (Mathematica), score 3, 45 bytes

Max[Length/@SequenceCases[#,x_/;OrderedQ@x]]&

Try it online!

SequenceCases and OrderedQ by themselves give a score of 3, so the score can't be improved without changing the approach significantly.

Misha Lavrov

Posted 2018-10-07T14:59:35.530

Reputation: 4 846

The correct way to use patterns would have us do Max[Length/@SequenceCases[#,_?OrderedQ]]&, but _?Or is an increasing subsequence of length 4. (As is _?AnyCamelCaseCommand.) – Misha Lavrov – 2018-10-07T17:10:11.970

0

Kotlin, Score 6, 119 bytes

 fun x(a : IntArray){var m=1;var k=0;var d=1;while(k<a.size-1){if(a[k]<=a[k+1])m++;else{if(d<m)d=m;m=1};k++};println(d)}

Try Online

Explanation

  1. Step 1: Check for previous value to next value
  2. Step 2: If previous value is less or equal then add plus 1 keep doing while condition is true
  3. Step 3: check previous count with next count, keep highest count in variable d.

Syed Hamza Hassan

Posted 2018-10-07T14:59:35.530

Reputation: 129

Ok, i got it, i will edit it shortly. – Syed Hamza Hassan – 2018-10-08T05:44:47.717

Please check, i have made a function in which input can be given. As per my sample string answer would be [2,4,5,6,7,7,7] Score is 7. – Syed Hamza Hassan – 2018-10-08T05:54:25.513

I have updated score and link please check. – Syed Hamza Hassan – 2018-10-08T06:12:58.083

Ok, I gave updated. – Syed Hamza Hassan – 2018-10-08T06:39:43.780

Let us continue this discussion in chat.

– Jo King – 2018-10-08T06:49:28.117

0

Java (JDK), 126 bytes, Score 6

Golfed

private static int l(byte[] o){int m=0;int c=1;int p=0;for(byte i:o){if(m<c){m=c;}if(i>=p){p= i;c++;}else{c=1;p=0;}}return m;}

Ungolfed

private static int longest(byte[] input) {
    int maxc = 0;
    int consec = 1;
    int prev = 0;
    for (byte i : input) {
        if (maxc < consec) {
            maxc = consec;
        }
        if (i >= prev) {
            prev = i;
            consec++;
        }
        else {
            consec = 1;
            prev = 0;
        }
    }
    return maxc;
}

Input

[112, 114, 105, 118, 97, 116, 101, 32, 115, 116, 97, 116, 105, 99, 32, 105, 110, 116, 32, 108, 40, 98, 121, 116, 101, 91, 93, 32, 111, 41, 123, 105, 110, 116, 32, 109, 61, 48, 59, 105, 110, 116, 32, 99, 61, 49, 59, 105, 110, 116, 32, 112, 61, 48, 59, 102, 111, 114, 40, 98, 121, 116, 101, 32, 105, 58, 111, 41, 123, 105, 102, 40, 109, 60, 99, 41, 123, 109, 61, 99, 59, 125, 105, 102, 40, 105, 62, 61, 112, 41, 123, 112, 61, 32, 105, 59, 99, 43, 43, 59, 125, 101, 108, 115, 101, 123, 99, 61, 49, 59, 112, 61, 48, 59, 125, 125, 114, 101, 116, 117, 114, 110, 32, 109, 59, 125]

Jaden Lee

Posted 2018-10-07T14:59:35.530

Reputation: 1

Shouldn't byte be int, since byte would be restricted to 8 bits? – Jo King – 2018-10-08T10:17:29.603

@JoKing I am not exactly sure what you mean. Do you mean that I should change the byte class to int class? – Jaden Lee – 2018-10-10T02:29:37.873

Yes, since the input is a list of integers – Jo King – 2018-10-10T02:47:11.750

0

Kotlin, Score 4, 67 bytes

{a:IntArray->var i=0;var p=0;a.map{if(it<p){i=0};p=it;(++i)}.max()}

Main idea is: Transform each integer to length of contiguous sub-sequences that is increasing (not strictly). Return maximum.

  • a.map{...} - for each integer in the array do
  • if(it<p){i=0} - if current integer is less then a previous integer, then reset counter
  • p=it - store current integer in the previous
  • (++i) - increment counter and return value of the expression
  • .max() - get maximun of all length

mazzy

Posted 2018-10-07T14:59:35.530

Reputation: 4 832

0

Ruby, 64 bytes

->e{e.size.downto(1).find{|l|e.each_cons(l).find{|c|c==c.sort}}}

Try it online!

Idva

Posted 2018-10-07T14:59:35.530

Reputation: 97

1Note that this is not [tag:code-golf]. Your current score is 6. Also, your code does not handle the empty list (where the output should be 0) – Jo King – 2018-10-10T07:45:54.777