| Bytes | Lang | Time | Link |
|---|---|---|---|
| 080 | Wolfram Language Mathematica | 210207T200840Z | att |
| 033 | Stax | 210208T104028Z | Razetime |
| 021 | 05AB1E | 210208T093244Z | Kevin Cr |
| 061 | Charcoal | 210207T205959Z | Neil |
| 020 | Jelly | 210207T172810Z | Jonathan |
| 022 | Jelly | 210207T165305Z | xigoi |
| 165 | JavaScript ES6 | 210207T133907Z | Arnauld |
| 076 | J | 210207T135506Z | xash |
Wolfram Language (Mathematica), 81 80 bytes
Min@Cases[#,a_/;!a#&&#~FreeQ~a&@{Mean@#,Sort@#//._[_,a__,_]:>a,Commonest@#}]&
is \[VectorLessEqual].
Returns Infinity if such a number does not exist.
Stax, 33 bytes
Ç∞♫ÑÄ‼ú╘màk■g5øg◘U½¢┴Åû)*ç╨£╨├╡óà
Getting the middle two elements takes most of the space here. Reducing that would significantly improve the score.
05AB1E, 21 bytes
¢ZQÏIÅAI{Ås)˜WUKʒX›}ß
Try it online or verify all test cases.
Explanation:
#Mode:
¢ # Count how many times each value occurs in the (implicit) input-list
Z # Get the maximum of these counts (without popping the list)
Q # Check for each if it's equal to this maximum count
Ï # Only leave the values in the (implicit) input-list at the truthy indices
#Mean:
IÅA # Push the input; pop and push its mean
#Median:
I{ # Push the input; sort it
Ås # Pop and push the middle (one or two) values
) # Wrap all three into a list
˜ # And flatten it
W # Push the minimum of this list
U # Pop and store it in variable `X`
K # Remove all medium, mean, and mode values from the (implicit) input-list
ʒ # Filter the remaining values by:
X› # Check if it's larger than `X`
}ß # After the filter: pop and push the minimum
# (after which it is output implicitly)
Although 05AB1E does have builtins for all three: mean (ÅA); median (Åm); and mode (.M), the median takes the average of the two values for even-length lists, and the mode only results in a single most frequent value instead of all, which is why we'll do those manually. In the legacy 05AB1E version the .M mode builtin did result in all most frequent values instead of just one, but unfortunately the legacy version doesn't have a median nor middle builtin, and doing that manually would be even longer.
Charcoal, 61 bytes
W⁻θυF№ι⌊ι⊞υ⌊ι≔✂υ⊘⊖Lθ⊕⊘Lθ¹υ≔Eθ№θιηF⌕Aη⌈η⊞υ§θι⊞υ∕ΣθLθI⌊Φ⁻θυ›ι⌊υ
Try it online! Link is to verbose version of code. Outputs None if no suitable value exists. Explanation: Not an ideal challenge for Charcoal.
W⁻θυ
While the sorted list does not contain all of the input values...
F№ι⌊ι⊞υ⌊ι
... push the next value to the sorted list according to the number of times it appears.
≔✂υ⊘⊖Lθ⊕⊘Lθ¹υ
Slice the middle element(s) from the sorted list.
≔Eθ№θιη
Tabulate how many times each element appears in the list.
F⌕Aη⌈η
Loop over the indices of maximum counts, ...
⊞υ§θι
... pushing the original value to the list of averages.
⊞υ∕ΣθLθ
Finally, push the mean to the list of averages.
I⌊Φ⁻θυ›ι⌊υ
Remove all of the averages from the input, then also filter out values lower than all of the averages, and print the minimum of the remainder (if any).
Jelly, 20 bytes
L‘HịṢ;Æm;ÆṃµṂ³>Ƈḟȯ.Ṃ
A full program accepting a list of numbers which prints the next to the middle or 0.5 if there is none.
Or see the test-suite (uses a functional form which uses the register atoms, © and ®, to replace the use of the program argument atom, ³).
How?
L‘HịṢ;Æm;ÆṃµṂ³>Ƈḟȯ.Ṃ - Main Link: list, A
L - length (A)
‘ - increment
H - halve
Ṣ - sort (A)
ị - index into (if A has an even number of elements we get the two in
the middle, due to Jelly's fractional indexing magic)
Æm - mean (A)
; - concatenate
Æṃ - mode (A)
; - concatenate
µ - start a new monadic chain, f(X=medians+[mean,mode])
³ - programs argument, A
Ṃ - minimum (X)
Ƈ - filter (A) keeping if:
> - greater than (minimum (X))
ḟ - filter - discard those which are in (X)
. - a half
ȯ - logical OR (replace an empty list with 0.5)
Ṃ - minimum
Jelly, 22 bytes
L‘HịṢ;Æm;Æṃ
ḟÇ>ƇÇṂ$ȯ.Ṃ
Returns 0.5 for an invalid input.
Explanation
L‘HịṢ;Æm;Æṃ Auxiliary monadic link
L Length
‘ Increment
H Half
ị Index into
Ṣ the sorted array
; Join with
Æm the mean
; Join with
Æṃ the mode(s)
ḟÇ>ƇÇṂ$ȯ.Ṃ Main monadic link
ḟ Filter out
Ç the results of the previous link
Ƈ Filter
> greater than
$ (
Ç The results of the previous link
Ṃ Maximum
$ )
ȯ Logical OR (non-vectorizing) with
. 0.5
Ṃ Minimum
JavaScript (ES6), 165 bytes
Returns undefined if no valid answer is found.
a=>a.sort((a,b)=>a-b).map(c=v=>(n++,t+=v,i=c[v]=-~c[v],i<m?0:m=i,v),m=t=n=0).find(v=>v>Math.min(...A=[t/n,a[n>>1],a[n-1>>1],...a.filter(v=>c[v]==m)])&!A.includes(v))
Commented
a => // a[] = input array
a.sort((a, b) => // sort a[] from lowest to highest
a - b // so that we can compute the median value(s)
) //
.map(c = v => // for each value v in a[]:
( n++, // increment the total number of values n
t += v, // add v to the sum t of all values
i = c[v] = -~c[v], // update the number i of occurrences of v
i < m ? 0 : m = i, // update m to max(m, i)
v // yield v so that the array is unchanged
), // and we can chain with .find()
m = t = n = 0 // start with m = t = n = 0
) // end of map()
.find(v => // find the first value v that satisfies:
v > Math.min( // 1) it's greater than the minimum of ...
...A = [ // the values stored in A[], which are:
t / n, // - the mean
a[n >> 1], // - the 1st median value
a[n - 1 >> 1], // - the 2nd median value (if any)
...a.filter(v => // - all mode values
c[v] == m // i.e. values that appear m times
) //
] // end of array
) & // end of Math.min()
!A.includes(v) // 2) it's not one of the values defined above
) // end of find()
J, 76 bytes
Returns an empty list '' if there is no element.
(0{ ::''-./:~@#~-.><./@])({./.~#~[:(=>./)#/.~),(>.@<:@-:@#(-@[}.}.)/:~),+/%#
+/%#mean: sum divided by length>.@<:@-:@#(-@[}.}.)/:~median(s): sort, drop first and lastfloor(length % 2 - 1)elements (can this be shorter?)#/.~mode(s): group the list by itself, get the length of each group[:(=>./)the maximum length{./.~#~take the groups' first element that have the maximum length<./@]minimum of mean, medians and modes-. #~-.>get every element that is not in (mean, medians, modes), but also larger,/:~@sort them and0{ ::''get the first element – if there is none and an error would occur, return''instead