Given a sequence \$S\$ with length \$n\$ of exactly one each of whole numbers from \$1\$ to \$n\$, your task is to return a number \$\sigma\$ indicating how shuffled it is.
Definition of shuffledness
Humans perceive shuffling randomness locally [Citation Needed], so the shuffledness of a sequence is based on the ability to find various patterns within each sliding window \$W\$ of 5 consecutive elements of the sequence (\$W = \{S_x, S_{x+1}, S_{x+2}, S_{x+3}, S_{x+4}\}\$ for some \$x\$- these will overlap, as each \$x\$ that is at least \$4\$ less than \$n\$ is considered). For each window of 5 elements, add a point for each test of the following that fails: (Note the 1-indexing due to the use of mathematical notation)
- Apply the following tests to the whole window:
- \$W\$ is monotonic
- \$W\$ is in ascending order
- Some permutation of \$W\$ is a run of sequential integers (e.g. 1, 2, 3, 4, 5)
- For all \$W_i\$ where \$i>1\$, \$W_i = W_{i-1}+k\$ for some integer \$k\$.
- This corresponds to regular linear intervals
- For all \$W_i\$ where \$i>1\$, \$W_i = W_{i-1}+ax+k\$ for some integers \$k, a\$, where \$x=i\$.
- This corresponds to regular quadratic intervals (which could also be linear)
- Apply the same tests for subwindows where 1 or 2 elements have been removed (if any one of those reduced windows passes a particular test, this step passes. Only count one failure when a test fails for all reduced windows.
- Apply the same tests for windows with some pair of adjacent elements optionally swapped (this step will pass if all tests passed for the whole unmodified window) Replace test 3 with the following:
- \$W\$ is an ascending or descending sequence of sequential integers (e.g. 5, 4, 3, 2, 1)
Notes
- Ascending sequential sequences will pass every test for all windows and therefore be scored as \$0\$
- Descending sequential sequences will pass all tests except the "ascending" one for all windows and will therefore be scored as \$3(n-4)\$
- The naive maximum shuffledness is \$15(n-4)\$
Examples
- [1, 5, 3, 2, 4]
- All elements:
- Passes test 3
- Fails all other tests (+4)
- Elements removed:
- Passes tests 1, 2, 5 with [1, 2, 4] (remove 5 and 3)
- Passes test 3 with [5, 3, 2, 4] (remove 1)
- Fails test 4 (+1)
- Elements swapped:
- Fails all tests (+5)
- Test 3 fails because it requires the elements to be in order for this case only
- Fails all tests (+5)
- \$\therefore \sigma = 10\$
- All elements:
- [1, 2, 4, 3, 5]
- All elements:
- Passes test 3
- Fails all other tests (+4)
- Elements removed:
- All tests pass with [1, 2, 3] (remove 4, 5)
- Elements swapped:
- Passes all tests (swap 4, 3)
- \$\therefore \sigma = 4\$
- All elements:
- [3, 6, 9, 12, 15] (a sliding window)
- All elements:
- Passes tests 1, 2, 4, 5
- Fails test 3 (+1)
- Elements removed: same (+1)
- Elements swapped: same (+1) (no swap)
- \$\therefore \sigma = 3\$
- All elements:
- [8, 12, 13, 11, 6] (a sliding window)
- All elements:
- Passes test 5 (\$a=-3, k=4\$)
- Fails all other tests (+4)
- Elements removed:
- Passes tests 1, 2, 5 with [8, 12, 13] (remove 11, 6)
- Passes test 3 with [12, 13, 11] (remove 8, 6)
- Fails test 4 (+1)
- Elements swapped: same as all elements (+4) (no swap)
- \$\therefore \sigma = 9\$
- All elements:
- [2, 4, 6, 1, 3, 5] (whole sequence)
- First window: ([2, 4, 6, 1, 3])
- All elements: Fails all tests (+5)
- Elements removed:
- Passes tests 1, 2, 4, 5 with [2, 4, 6]
- Passes test 3 with [2, 1, 3]
- Elements swapped: Fails all tests (+5)
- Second window: ([4, 6, 1, 3, 5])
- All elements: Fails all tests (+5)
- Elements removed:
- Passes tests 1, 2, 4, 5 with [1, 3, 5]
- Passes test 3 with [4, 6, 5]
- Elements swapped: Fails all tests (+5)
- \$\therefore \sigma = 20\$
- First window: ([2, 4, 6, 1, 3])
Rules and such
- Standard IO rules, loopholes, etc
- You must handle sequences of up to 1000 elements long
- You may assume \$S\$ is at least 5 elements long
- No validation necessary. You may assume that all inputs contain exactly one each of every whole number from \$1\$ to \$n\$
- Shortest code wins!