| Bytes | Lang | Time | Link |
|---|---|---|---|
| 011 | Uiua | 241101T010931Z | nyxbird |
| 009 | iogii | 241028T033913Z | Unrelate |
| 055 | AWK | 241026T003629Z | xrs |
| 038 | Ruby | 241025T164927Z | Jordan |
| 009 | Japt | 170811T125307Z | Shaggy |
| 005 | Thunno 2 | 230813T192600Z | The Thon |
| 044 | Desmos | 230323T032803Z | Youserna |
| 004 | Nekomata | 230323T021447Z | alephalp |
| 044 | Arturo | 230211T090147Z | chunes |
| 041 | Factor + grouping.extras | 230211T070811Z | chunes |
| 032 | Zsh | 210130T113631Z | GammaFun |
| nan | Perl 5 | 170811T130012Z | Xcali |
| 5846 | Scala | 210129T143436Z | Razetime |
| 005 | Jelly | 201005T201744Z | caird co |
| 008 | Ohm | 170811T125014Z | totallyh |
| 013 | J | 170815T035615Z | cole |
| 056 | Clojure | 170814T123417Z | NikoNyrh |
| 051 | JavaScript ES6 | 170812T055752Z | Aaron Hi |
| 051 | Octave | 170813T134648Z | rahnema1 |
| 013 | k | 170812T195601Z | zgrep |
| 010 | MATL | 170811T185000Z | DanTheMa |
| 015 | Pyth | 170812T120654Z | Karan El |
| 045 | Python 2 | 170812T002206Z | Sisyphus |
| 049 | R | 170811T130825Z | djhurio |
| 042 | Mathematica 42 Bytes | 170811T192302Z | Kelly Lo |
| 016 | CJam | 170811T125015Z | Erik the |
| 053 | PHP | 170811T154345Z | Titus |
| 086 | Java OpenJDK 8 | 170811T143629Z | Nevay |
| 005 | 05AB1E | 170811T140120Z | Mr. Xcod |
| 067 | Swift 3 | 170811T134515Z | Mr. Xcod |
| 015 | APL Dyalog | 170811T132916Z | user4180 |
| 007 | Husk | 170811T122224Z | H.PWiz |
| 050 | JavaScript ES6 | 170811T124137Z | Arnauld |
| 123 | Batch | 170811T131128Z | Neil |
| 010 | Brachylog | 170811T130742Z | Erik the |
| 008 | Jelly | 170811T122651Z | miles |
| 081 | C# Mono | 170811T125159Z | TheLetha |
| 008 | Pyth | 170811T123629Z | Mr. Xcod |
| 005 | 05AB1E | 170811T123149Z | Erik the |
| 045 | Mathematica | 170811T122401Z | ZaMoC |
| 054 | Python 2 | 170811T122617Z | Rod |
Uiua, 11 bytes
↘2⧅(⍣⊏∞1⇌⍆)
↘2⧅(⍣⊏∞1⇌⍆)
⧅( ) # for each prefix:
⇌⍆ # sort backwards
⊏ 1 # and get the second element
⍣ ∞ # (replacing out-of-bounds errors with ∞)
↘2 # drop the first two (both are ∞)
iogii, 9 bytes
:l}k:sbth
Some clever expand solution feels eminently possible, but at this point I'm beginning to question if it would even actually be shorter.
:s Sort each
:l}k prefix
b backwards,
th and take the second element of each.
Japt, 12 10 9 bytes
Includes a null element at the beginning.
£¯YÄ ÍÔÅÎ
£¯YÄ ÍÔÅÎ :Implicit input of array U
£ :Map 0-based indices Y
¯ : Slice U from index 0 to
YÄ : Index Y+1
Í : Sort
Ô : Reverse
Å : Slice off first element
Î : Get first element
Thunno 2, 5 bytes
ƒıṠṫt
Uses [] as the (arbitrary) first value.
Explanation
ƒıṠṫt # Implicit input
ƒ # Prefixes of the input
ı # Map over this list:
Ṡ # Sort this list
ṫt # Second-last item
# Implicit output
Nekomata, 4 bytes
poil
p Prefix
o Sort
i Init (remove the last element)
l Last
Accidentally, poil means hair in French.
Arturo, 45 44 bytes
$->a->map..2size a=>[last chop sort take a&]
$->a-> ; a function taking an argument a
map..2size a => [ ; map over the indices of a sans the first few
last ; the last element
chop ; of a block without its last element
sort ; sorted
take a & ; prefix of input given current index
] ; end map
Factor + grouping.extras, 41 bytes
[ head-clump rest [ 1 kth-largest ] map ]
head-clump restPrefixes without the first[ 1 kth-largest ] mapMap each to the second-largest element
Zsh, 32 bytes
for x;a+=($x)&&<<<${${(On)a}[2]}
Reverse Order numerically, <<< print the [2]nd element.
Scala, 58 46 bytes
x=>2 to x.size map(x take _ sortBy(-_)apply 1)
Takes a List as an argument, and returns a Vector.
-12 bytes from user.
Jelly, 5 bytes
Ṣ-ịƊƤ
Outputs the first value of the input as the first value in the output. This can be removed for adding one byte.
How it works
Ṣ-ịƊƤ - Main link. Takes l on the left
Ƥ - Generate the prefixes of l
Ɗ - Run the following code over each prefix:
Ṣ - Sort the prefix
-ị - Take the -1th index.
- Jelly lists are 1 indexed and modular. This means that the 0th index of a list is the final element and -1 the second to last element
Ohm, 10 8 bytes
-2 bytes thanks to ETHproductions.
∙p»îS2~ª
Uh, this is odd but I don't know how else to push a negative number... I don't really know Ohm. :P
J, 13 bytes
_2&{@/:~ ::#\
Try it online! The first element is always 1.
Explanation
_2&{@/:~ ::#\
\ Apply to prefixes
_2&{@/:~ Sort and take second-to-last atom
/:~ Sort upwards
_2 { Take second-to-last atom
:: If there's an error (i.e only one atom)
# Return the length of the list (1)
Clojure, 56 bytes
#(for[i(drop 2(reductions conj[]%))](nth(sort-by - i)1))
Maybe there is a better way to generate those prefixes.
JavaScript (ES6), 43 51 bytes
Edit: Added 8 bytes since numerical sorting is desired. :(
a=>a.map((_,b)=>a.slice(0,b+1).sort((a,b)=>b-a)[1])
Keeping this one here, though, as it is shorter providing one wants lexicographical ordering:
a=>a.map((_,b)=>a.slice(0,b+1).sort()[b-1])
Both expressions produce undefined for the first element.
Test code
const golfed =
a=>a.map((_,b)=>a.slice(0,b+1).sort((a,b)=>b-a)[1])
const inputs = [
[1, 5, 2, 3, 5, 9, 5, 8],
[1, 1, 2, 2, 3, 3, 4],
[2, 1, 0, -1, 0, 1, 2]
];
console.log(inputs.map(x => golfed(x)));
// [ [ undefined, 1, 2, 3, 5, 5, 5, 8 ],
// [ undefined, 1, 1, 2, 2, 3, 3 ],
// [ undefined, 1, 1, 1, 1, 1, 2 ] ]
Octave, 51 bytes
@(a){[~,I]=cummax(a);a(I(2:end))=-inf;cummax(a)}{3}
- An arbitrary value returned for the first element.
k, 13 bytes
{x(>x)1}'1_,\
,\ /sublists of increasing lengths (scan concat)
1_ /remove the first sublist
{ }' /for each sublist:
(>x) / indices to permute sublist into largest to smallest
1 / get second index
x / get sublist[that index]
MATL, 19 10 bytes
Thanks to Luis Mendo for shaving off 9 bytes!
"GX@:)SP2)
Try it here.
Explanation
"GX@:)SP2)
" for all the values in the input
G get input
X@:) get values up to the iteration
SP sort it in descending order
2) get the second value
implicit end of loop and output
Pyth, 15 bytes
FNr2hlQ@_S<QN1
Try it here: https://pyth.herokuapp.com/?code=FNr2hlQ%40_S%3CQN1&input=%5B1%2C+5%2C+2%2C+3%2C+5%2C+9%2C+5%2C+8%5D&debug=0
Explanation
FNr2hlQ - For loop with counter 2 <= N <= list length
@_S<QN1 - 2nd element when the first N elements of the list are sorted in ascending order and then reversed (sorted in descending order)
Python 2, 45 bytes
f=lambda l:l[1:]and f(l[:-1])+[sorted(l)[-2]]
The right side of the code is self-explanatory. However, what do we put to the left of the and? Because we are concatenating parts of a list recursively, we need the left side to be truthy if l has 2 or more elements, and an empty list otherwise. l[1:] satisfies this criterion nicely.
R, 54 49 bytes
Thanks to Giuseppe -5 bytes. I did not know this feature of seq().
for(i in seq(x<-scan()))cat(sort(x[1:i],T)[2],"")
Mathematica 42 Bytes
Independently arrived at a answer very similar to @Jenny_mathy but 3 bytes shorter
Sort[#[[;;i]]][[-2]]~Table~{i,2,Length@#}&
Realized that the 1st running max takes only 15 bytes and two function calls!:
Max~FoldList~#&
This can be done so concisely because Max has the attributes Flat and OneIdentity but that's not true for RankedMax which would be the logical replacement. Unfortunately defining attributes or modifying them on existing functions takes up way too many bytes, so the flattening must be done by other means.
All of the nth running maxes can be found in 48 bytes:
PadLeft[Sort/@Flatten/@FoldList[{##}&,{},#&@#]]&
PHP, 53 bytes
for(;a&$n=$argv[++$i];rsort($a),print$a[1]._)$a[]=$n;
takes input from command line arguments. Output delimited, lead and trailed by semicola.
Run with -nr or try it online.
Yields a warning in PHP 7.1; replace a& with ""< to fix.
Or use for(;++$i<$argc;rsort($a),print$a[1]._)$a[]=$argv[$i]; (54 bytes)
Java (OpenJDK 8), 87 86 bytes
a->{int x,y=x=1<<-1;for(int c:a){if((c>x?x=c:c)>y){x=y;y=c;}System.out.print(x+" ");}}
05AB1E, 5 bytes
Found another 5-byter, very different from Erik's solution. The arbitrary value is the first element of the list.
ηε{Áθ
Explanation
ηε{Áθ - Full program that reads implicitly from STDIN and outputs to STDOUT.
η - Push the Prefixes of the list.
ε - Apply to each element (each prefix):
{ - Sort the prefix list.
Á - Shift the list to the right by 1, such that the first element goes to the
beginning and the second largest one becomes the last.
θ - Get the last element (i.e. the second largest)
- Print implicitly.
Let's take an example, to make it easier to understand.
First we get the implicit input, let's say it's
[1, 5, 2, 3, 5, 9, 5, 8].Then, we push its prefixes using
η-[[1], [1, 5], [1, 5, 2], [1, 5, 2, 3], [1, 5, 2, 3, 5], [1, 5, 2, 3, 5, 9], [1, 5, 2, 3, 5, 9, 5], [1, 5, 2, 3, 5, 9, 5, 8]].Now, the code maps through the list and sorts each prefix using
{-[[1], [1, 5], [1, 2, 5], [1, 2, 3, 5], [1, 2, 3, 5, 5], [1, 2, 3, 5, 5, 9], [1, 2, 3, 5, 5, 5, 9], [1, 2, 3, 5, 5, 5, 8, 9]].We then take the very last element and move it to the beginning:
[[1], [5, 1], [5, 1, 2], [5, 1, 2, 3], [5, 1, 2, 3, 5], [9, 1, 2, 3, 5, 5], [9, 1, 2, 3, 5, 5, 5], [9, 1, 2, 3, 5, 5, 5, 8]].Of course, now the code gets the last element of each sublist using
θ-[1, 1, 2, 3, 5, 5, 5, 8](the first one being the arbitrary value.
Swift 3, 67 bytes
func g(l:[Int]){print((1..<l.count).map{l[0...$0].sorted()[$0-1]})}
Swift 3, 65 bytes
{l in(1..<l.count).map{l[0...$0].sorted()[$0-1]}}as([Int])->[Int]
How to run these?
The first one is a complete function that takes the input as a function parameter and prints the result. You can use them exactly as shown in the testing link. I decided to add instructions though, because the second type of function is used very rarely and most people don't even know of its existence. Usage:
g(l: [1, 1, 2, 2, 3, 3, 4] )
The second one is an anonymous function, like lambdas. You can use it exactly as you'd do it Python, declaring a variable f and calling it:
var f = {l in(1..<l.count).map{l[0...$0].sorted()[$0-1]}}as([Int])->[Int]
print(f([1, 1, 2, 2, 3, 3, 4]))
or wrap it between brackets and call it directly ((...)(ArrayGoesHere)):
print(({l in(1..<l.count).map{l[0...$0].sorted()[$0-1]}}as([Int])->[Int])([1, 1, 2, 2, 3, 3, 4]))
Husk, 9 7 bytes
Saved a byte or two thanks to @Zgarb
mȯ→hOtḣ
Returns 0 for the first "second maximum"
Explaination
-- implicit input, e.g [1,5,3,6]
ḣ -- prefixes [[],[1],[1,5],[1,5,3],[1,5,3,6]]
t -- remove the first element [[1],[1,5],[1,5,3],[1,5,3,6]]
mȯ -- map the composition of 3 functions
O -- sort [[1],[1,5],[1,3,5],[1,3,5,6]]
h -- drop the last element [[],[1],[1,3],[1,3,5]
→ -- return the last element [0,1,3,5]
-- implicit output
JavaScript (ES6), 58 51 50 bytes
Saved 1 byte thanks to @Neil
Appends undefined for k = 1.
a=>a.map(e=>(b=[e,...b]).sort((a,b)=>b-a)[1],b=[])
Test cases
NB: This snippet uses JSON.stringify() for readability, which -- as a side effect -- converts undefined to null.
let f =
a=>a.map(e=>(b=[e,...b]).sort((a,b)=>b-a)[1],b=[])
console.log(JSON.stringify(f([1, 5, 2, 3, 5, 9, 5, 8]))) // 1 2 3 5 5 5 8
console.log(JSON.stringify(f([1, 1, 2, 2, 3, 3, 4]))) // 1 1 2 2 3 3
console.log(JSON.stringify(f([2, 1, 0, -1, 0, 1, 2]))) // 1 1 1 1 1 2
Batch, 123 bytes
@set/af=s=%1
@for %%n in (%*)do @call:c %%n
@exit/b
:c
@if %1 gtr %s% set s=%1
@if %1 gtr %f% set/as=f,f=%1
@echo %s%
Jelly, 8 bytes
ḣJṢ€Ṗ€Ṫ€
The first value will be 0, always, and the following numbers will be the second maximums of each prefix.
Explanation
ḣJṢ€Ṗ€Ṫ€ Input: array A
J Enumerate indices, [1, 2, ..., len(A)]
ḣ Head, get that many values from the start for each, forms the prefixes
Ṣ€ Sort each
Ṗ€ Pop each, removes the maximum
Ṫ€ Tail each
C# (Mono), 81 bytes
using System.Linq;a=>a.Select((_,i)=>a.Take(++i).OrderBy(n=>n).Skip(i-2).First())
Pyth, 8 bytes
m@Sd_2._
Try it online! or Try the Test Suite!
How?
This outputs the first element of the list as the first value in the list, as per the spec You can output an arbitrary value for the first element.
m@Sd_2._ - Full program with implicit input.
m ._Q - Map over the prefixes of the input with a variable d.
Sd - Sorts the current prefix.
@ - Gets the element...
_2 - At index - 2 (the second highest).
- Print implicitly.