A "Sorting" algorithm

33

4

There is a "sorting algorithm" sometimes called Stalin sort in which in order to sort a list you simply remove elements from the list until it is sorted in increasing order. For example the list

[1, 2, 4, 5, 3, 6, 6]

When "sorted" using Stalin sort becomes

[1, 2, 4, 5, 6, 6]

The three was removed because it was out of order.

Now obviously there are many ways to remove elements to sort a list. For example any list with less than two elements must be sorted so by just removing enough elements blindly we can always sort a list. Since this is the case we only care about the longest possible result from a Stalin sort.

Your task will be to take a list of positive integers and output the length of the longest sorted (increasing) list that can be arrived at by removing elements from the original list. That is find the length of the longest sorted (possibly non-contiguous) sub-list.

Sorted lists can have the same element more than once in a row. You do not need to support the empty list unless your program itself is empty.

Scoring

Your answer will be scored by the length of its own longest possible Stalin sort. Programs will be interpreted as a sequence of bytes rather than characters, and their order will be the natural one that arises by interpreting the bytes as numbers. Lower scores are better.

This is not

Here's a neat-o tool to help you score your answers.

Test cases

[1, 2, 4, 5, 3, 6, 6] -> 6
[19, 2] -> 1
[3, 3, 4, 3] -> 3
[10] -> 1
[1, 2, 4, 9] -> 4
[1, 90, 2, 3, 4, 5] -> 5
[1, 90, 91, 2, 3, 4, 5] -> 5

Post Rock Garf Hunter

Posted 2018-10-30T15:05:28.697

Reputation: 55 382

3

In short: output the length of the longest (non-strictly) increasing sequence.

– user202729 – 2018-10-30T15:16:10.660

1I like the rule "You do not need to support the empty list unless your program itself is empty." – Paŭlo Ebermann – 2018-11-01T00:24:42.443

This challenge reminds me a lot of the dropsort challenge: https://codegolf.stackexchange.com/questions/61808/lossy-sorting-implement-dropsort

– Stefnotch – 2018-11-01T20:24:08.797

1

I made a checker at https://ptpb.pw/SVSt.html . Still not very functional, but it works. (TODO: * bar chart * partition into least decreasing sequences * support for other code pages)

– user202729 – 2018-11-02T16:17:32.383

@user202729 Cool! I've added it to the post. Feel free to edit newer versions in if necessary. – Post Rock Garf Hunter – 2018-11-02T21:37:24.970

Answers

8

Python 2, length 14 12 10 9

M=max;X=exit;i=input();L=[0]*M(i)
for	a	in	i:L[a-1]=M(L[:a])+1
X(M(L))

Output is via exit code.

Try it online!

How it works

At all times, the array \$L\$ keeps track of the longest sorted subarrays encountered so far; \$L[a-1]\$ is the length of longest one that ends with \$a\$.

Initially, we haven't processed an array elements, so \$L\$ consists entirely of zeroes.

When processing the array element \$a\$, we first take the maximum of \$[L[0], \dots, L[a-1]]\$, which is length of the longest sorted subarray encountered so far that ends with \$a\$ or a smaller integer. Appending \$a\$ to such an array will keep it sorted, so the longest sorted subarray ending in \$a\$ is one element longer than that maximum. We update \$L[a-1]\$ with the computed value.

The final result is the maximum of \$L\$.

Dennis

Posted 2018-10-30T15:05:28.697

Reputation: 196 637

Can you please explain, why it works? I'm having hard times to comprehend it :( – Dead Possum – 2018-10-31T14:01:09.327

I've added an explanation. – Dennis – 2018-10-31T15:54:51.300

7

Wolfram Language (Mathematica), score 9

Length@*LongestOrderedSequence

Try it online!

user202729

Posted 2018-10-30T15:05:28.697

Reputation: 14 620

5

Haskell, Score 8 7, 48 bytes

(l:k)%d|d>l=k%d|3>2=max(1+k%l)(k%d)
c%b=0
a=(%0)

Try it online!

The longest sorted sublist is

%%%%%%)

Post Rock Garf Hunter

Posted 2018-10-30T15:05:28.697

Reputation: 55 382

5

Perl 6, score 9

*.combinations.map({@^a*[<=] @a}).max

Try it online!

nwellnhof

Posted 2018-10-30T15:05:28.697

Reputation: 10 037

4

Jelly, length  4  2

ṢƑƇZLƲ}ŒP

Try it online!

Bytes in Jelly's code page

183 146 144 90 76 169 125 19 80

How it works

ṢƑƇZLƲ}ŒP  Main link. Argument: A (array)

       ŒP  Powerset; yield P, the array of all sub-arrays of A.
     Ʋ     Vier; combine the preceding four links into a monadic chain...
      }    and apply the chain to the right argument (P).
  Ƈ            Comb; only keep arrays for which the link to the left returns 1.
ṢƑ             Sort fixed; yield 1 if sorting doesn't alter the array.
   Z           Zip; read the filtered powerset by columns.
    L          Take the length.

Dennis

Posted 2018-10-30T15:05:28.697

Reputation: 196 637

3

Pyth, score 3 2 (7 bytes)

leSI#y

Saved a point thanks to Anders Kaseorg.
Try it here

Explanation

leSI#y
     yQ    Take the power set of the (implicit) input (preserving order).
  SI#      Get the ones that are sorted.
 e         Take the last (longest).
l          Get the length.

user48543

Posted 2018-10-30T15:05:28.697

Reputation:

leSI#y scores 2. – Anders Kaseorg – 2018-10-30T21:53:56.567

2

Stax, 4 maximal length stalin sort

S{:^fF%|M

Run and debug it

It works like this.

S       powerset of input
{:^f    filter by non-descending sequences
F%|M    take the maximum length remaining

recursive

Posted 2018-10-30T15:05:28.697

Reputation: 8 616

2

R, Score 15 11, 72 62 bytes

function(L,M=max,A=1:M(L)*0){for(Y in L)A[Y]=M(A[1:Y])+1;M(A)}

Try it online!

Ports Dennis' Python answer to R.

Giuseppe

Posted 2018-10-30T15:05:28.697

Reputation: 21 077

Just changing variable names won't help, because as your last link shows, none of them are used in the (found) substring that gives score 15. – Ørjan Johansen – 2018-10-31T01:10:37.363

@ØrjanJohansen ah, of course, I'm rather dumb. I suppose another approach is necessary. – Giuseppe – 2018-10-31T18:48:44.783

2

Brachylog, length 2 (4 bytes)

⊇≤₁l

Try it online!

An answer which makes up for being so concise by not being that much shorter sorted.

(08 03 80 6C in Brachylog's code page)

        Output
   l    the length of
 ≤₁     a non-decreasing
⊇       sublist of
        the input.
        (maximizing the size of the sublist)

Unrelated String

Posted 2018-10-30T15:05:28.697

Reputation: 5 300

I came up with ►LSnmOṖ for Husk but its score (for its length at least) is too bad to bother posting... – Unrelated String – 2019-03-25T07:52:02.057