| Bytes | Lang | Time | Link |
|---|---|---|---|
| 058 | Tcl | 171112T233847Z | sergiol |
| 044 | AWK | 250227T170510Z | xrs |
| 020 | Juby | 240314T002945Z | Jordan |
| 003 | Vyxal 3 | 240313T232733Z | pacman25 |
| 077 | Swift | 240313T223749Z | macOSist |
| 003 | Thunno 2 h | 230709T134543Z | The Thon |
| 119 | C++ g++ | 160825T041754Z | ceilingc |
| 019 | Julia 0.6 | 180104T181409Z | gggg |
| 087 | SNOBOL4 CSNOBOL4 | 180104T180135Z | Giuseppe |
| 079 | C | 160924T022309Z | NoSeatbe |
| 019 | x86 opcode | 171229T184512Z | l4m2 |
| 097 | Python | 171219T165112Z | Alex Hul |
| 110 | Python | 171115T001339Z | Bolce Bu |
| 028 | Clojure | 171114T214333Z | NikoNyrh |
| 004 | Pyke | 160824T074307Z | Blue |
| 005 | APL Dyalog | 160824T081705Z | Adá |
| 066 | C++14 | 161206T175723Z | Karl Nap |
| 065 | PHP | 160923T132639Z | aross |
| 052 | Processing | 160823T215954Z | Cody |
| 058 | JavaScript ES6 | 160923T193347Z | Sunny Pu |
| 046 | JavaScript ES6 | 160924T164238Z | ETHprodu |
| 029 | Python | 160924T014443Z | Dennis |
| 115 | Java | 160923T174056Z | AMACB |
| 031 | Python | 160823T213335Z | acrolith |
| 110 | dc | 160825T142558Z | juh |
| 030 | Bash + coreutils | 160912T091103Z | seshouma |
| 026 | ARM Machine Code | 160825T181115Z | Ian Chew |
| 055 | Scala | 160825T134037Z | AmazingD |
| 022 | zsh | 160825T031607Z | izabera |
| 034 | Python | 160824T015536Z | TLW |
| 035 | Octave | 160825T002442Z | Luis Men |
| 044 | POSIX Awk | 160824T225354Z | Zombo |
| 044 | PHP | 160824T122359Z | Dexa |
| 060 | C# | 160824T120435Z | TheLetha |
| 045 | dc | 160824T115006Z | Toby Spe |
| 034 | Python 2 | 160824T075103Z | Blue |
| 004 | MATL | 160824T012641Z | Luis Men |
| 034 | JavaScript ES6 | 160824T003729Z | Neil |
| 038 | Factor | 160823T235727Z | cat |
| 013 | Perl 6 | 160823T233630Z | Brad Gil |
| 027 | Haskell | 160823T231921Z | Michael |
| 009 | CJam | 160823T222432Z | PurkkaKo |
| 004 | 05AB1E | 160823T214810Z | Emigna |
| 218 | BrainFlak | 160823T220421Z | Wheat Wi |
| 012 | V | 160823T220553Z | Riker |
| 005 | Actually | 160823T213321Z | user4594 |
| 038 | BASH | 160823T215617Z | Riley |
| 023 | Ruby | 160823T213511Z | anna328p |
| 009 | Brachylog | 160823T214207Z | Leaky Nu |
| 017 | Retina | 160823T214206Z | Martin E |
| 036 | Haskell | 160823T213432Z | ThreeFx |
| 003 | Jelly | 160823T213012Z | Leaky Nu |
| 020 | Octave | 160823T213105Z | flawr |
| 018 | Mathematica | 160823T212829Z | Martin E |
Swift, 77 bytes
let f={(($0+[]).reduce(Int.max){$1>$0 ?$0:$1},$0.reduce(.min){$1<$0 ?$0:$1})}
The relatively obvious approach. I believe this is how the max() and min() methods are actually implemented for Sequence in the standard library.
Thunno 2 h, 3 bytes
ṠḲZ
Thunno 2, 4 bytes
Ṡçht
Explanation
ṠḲZ # Implicit input
Ṡ # Sort the list in ascending order
Ḳ # Bifurcate: duplicate and reverse
Z # Zip the two lists together
# Implicit output of first item
Ṡçht # Implicit input
Ṡ # Sort the list
ç # Parallelly apply:
h # First item
t # Last item
# Implicit output
C++ (g++), 148 133 131 122 119 bytes
#import<map>
#import<vector>
[](std::vector<int>v){int a=v[0],b=a;for(int i:v)i<a?a=i:i<b?:b=i;printf("%d %d\n",a,b);};
SNOBOL4 (CSNOBOL4), 87 bytes
M =INPUT
N =M
R I =INPUT :F(O)
N =LT(I,N) I
M =GT(I,M) I :(R)
O OUTPUT =N ' ' M
END
C, 83 81 79 bytes
m,M;f(a,s)int*a;{for(m=M=*a;s--;++a)*a<m?m=*a:*a>M?M=*a:0;printf("%i %i",m,M);}
x86 opcode, 19 bytes
00: mov ebx, [esi]
02: mov edx, ebx
04: lodsd
05: cmp eax, ebx
07: cmovg ebx, eax
0A: cmp eax, edx
0C: cmovl edx, eax
0F: loop lp1
11: xchg eax, ebx
12: ret
Input: ECX(length), ESI(array)
Output: EAX(max), EDX(min)
Removing xchg eax, ebx makes 18B but output is not usual reg
Python, 97 bytes
def k(s):
d=s[0]
if s[1:]:
a,b=k(s[1:])
return(a+d+abs(a-d))/2,(b+d-abs(b-d))/2
return d,d
Only clever part is that in python [] is falsy.
Only builtin used is abs.
It uses more builtins than the 110 byte python solution, and more bytes than the 26 byte solution using sort.
Python, 110 Bytes
Long, but it doesn't use sort.
def m(a):
d=a[0]
if len(a)==1:return d,d
b,c=m(a[1:])
if b>=d>=c:return b,c
if d>b:return d,c
return b,d
Output is a tuple of the form (max, min). Probably the least computationally efficient way to get the max/min of an array (other than bongo sorting it :P)
APL (Dyalog), 11 10 5 bytes
-5 thanks to Erik the Outgolfer.
According to the original poster, using a function that picks the largest of exactly two elements (in contrast to the largest of a whole array) [is] allowed. Therefore, the obvious solution is:
⌊/,⌈/
⌊/ minimum-reduction
, followed by
⌈/ maximum-reduction
More in the spirit of the challenge:
(⊃,⊢/)⍋⊃¨⊂
(
⊃ first
, followed by
⊢/ the last (lit. right-reduction)
) of
⍋ the indices of the elements in ascending order
⊃¨ each picks from
⊂ the entire argument
C++14, 66 bytes
As unnamed lambda returning via reference parameters. Accepts any random access container of integers.
[](int&a,int&b,auto&C){a=b=C[0];for(auto x:C)a=x<a?x:a,b=x>b?x:b;}
Ungolfed and usage:
#include<vector>
#include<iostream>
auto f=
[](int&a,int&b,auto&C){
a=b=C[0];
for(auto x:C)
a=x<a?x:a,
b=x>b?x:b;
}
;
int main(){
std::vector<int> v{1,2,3,4,5,6,7,8,9,0,10,121,-12,100,100,100};
int a;
int b;
f(a,b,v);
std::cout << a << ", " << b << std::endl;
}
PHP, 66 65 bytes
Note: this doesn't use any sort builtin. Also, PHP 7 required for the null coalesce operator.
for(;n|$x=$argv[++$i];$x>$h&&$h=$x)$l=min($l??$x,$x);echo"$l,$h";
The max part is straightforward. The min part is a bit more elaborate. since in PHP null is less than 123, null needs to be handled. For this I'm using the null coalesce operator. If $l is null, use the current number ($x) instead, then take the smaller of the 2 numbers.
Run like this:
php -d error_reporting=30709 -r 'for(;n|$x=$argv[++$i];$x>$h&&$h=$x)$l=min($l??$x,$x);echo"$l,$h";' -- 0 15 2 3 7 18 -2 9 6 -5 3 8 9 -14;echo
37 bytes (with sort)
Being allowed to sort makes it trivial (windows-1252 encoding):
php -d error_reporting=30709 -r '$a=$argv;echo$a[sort($a)],~ß,end($a);' -- 0 15 2 3 7 18 -2 9 6 -5 3 8 9 -14;echo
Sort returns 1 when successful, and the list of arguments will start with - which will be sorted as the first item, so we need item 1 to yield min. Therefore we happen to be able to use the return value of sort as the index, saving 2 bytes.
Tweaks
- Use
n|"0"to make"0"truthy to continue the loop, instead of0 ."0". Saved a byte
Processing, 59 52 bytes
void m(int[]x){x=sort(x);print(x[0],x[x.length-1]);}
Processing doesn't actually let me read from stdin that I've been able to find, and I don't know if its internal Java compiler supports lambdas (and its been so long since I've had to write serious Java that I don't remember how).
JavaScript (ES6), 68 58 bytes (without sorting)
Oh my first try in Golf: without explicit sort (and without min max).
Thanks to @ETHproductions for golfing tips!!
a=>(b=[],a.map(n=>b[n]=0),[b.indexOf(0),b.lastIndexOf(0)])
Does not work with negative integers.
Tried @Luis Mendo 's approach but got it 2 bytes longer... Cost for supporting negative numbers lol?
a=>[a.find(n=>a.every(e=>e>=n)),a.find(n=>a.every(e=>e<=n))]
Could get shorter if order of output is not important:
a=>a.filter(n=>a.every(e=>e>=n)||a.every(e=>e<=n))
JavaScript (ES6), 46 bytes
I wanted to try an answer without sorting:
a=>a.map(i=>i<n?n=i:i>x?x=i:0,n=x=a[0])&&[n,x]
Bonus recursive version, 53 bytes:
f=([i,...a],n=i,x=i)=>i+.5?f(a,i<n?i:n,i>x?i:x):[n,x]
Java, 115 bytes
String f(int[]i){int t=i[0],a=t,b=t;for(int c=0;c<i.length;c++){a=i[c]<a?i[c]:a;b=i[c]>b?i[c]:b;}return""+a+" "+b;}
Ungolfed:
String f(int[] i) {
int t=i[0], a=t, b=t; // assigns a and b to i[0]
for (int c=0; c < i.length; c++) { // loop through the array
a=(i[c]<a) ? i[c] : a;
b=(i[c]>b) ? i[c] : b; // assignment with ternary operator
}
return ""+a+" "+b; // returns a string
}
My first ever code "golf" solution.
Python, 61 49 37 36 34 31 bytes
lambda s:s.sort()or[s[0],s[-1]]
-12 bytes thanks to RootTwo
Another -12 bytes thanks to chepner
-2 bytes thanks to johnLate
-3 bytes thanks to johnLate
dc, 110 bytes
?ddsMsmzdsAsa[z1-:az0<S]dsSx[>R]s?[la;asM]sR[lM]sQ[lQxla1-dsa;al?xla0<N]dsNxlAsa[<R]s?[la;asm]sR[lm]sQlNxlmlMf
Help me, dcers! You are my only hope!
Thanks to @seshoumara for finding that bug!
I'll add an explanation later. Here it is broken up a bit:
?dd sM sm
zd sA sa
[z 1- :a z0<S]dsSx
[>R]s?
[la;asM]sR
[lM]sQ
[lQx la 1- dsa ;a l?x la0<N]dsNx
lA sa
[<R]s?
[la;a sm]sR
[lm]sQ
lNx
lm lM f
Bash + coreutils, 30 bytes
tr \ \\n|sort -n|sed '$p;1!d'
The sed script prints, after the input is sorted, the first and last integers.
ARM Machine Code, 26 bytes
Hex dump (little endian):
6810 4601 f852 cb04 4560 bfc8 4660 4561 bfb8 4661 3b01 d8f5 4770
This is a function, with no system call or library dependence. The encoding is Thumb-2, a variable (2 or 4 byte) encoding for 32-bit ARM. As one might imagine, there's no easy way to just sort and pick the first and last elements here. Overall there's nothing really that fancy going on here, it's more or less the same as the reference implementation.
Ungolfed assembly (GNU syntax):
.syntax unified
.text
.global minmax
.thumb_func
minmax:
@Input: @r0 and r1 are dummy parameters (they don't do anything)
@r2 - Pointer to list of integers (int*)
@r3 - Number of integers to sort (size_t)
@Output:
@Minimum of the list in r0 (int)
@Maximum in r1 (int)
ldr r0,[r2] @min=r2[0]
mov r1,r0 @max=min
loop:
@ip is intra-procedure call register, a.k.a. r12
ldr ip,[r2],#4 @ip=*r2++
cmp r0,ip
it gt @if (r0>ip)
movgt r0,ip @r0=ip
cmp r1,ip
it lt @if (r1<ip)
movlt r1,ip @r1=ip
subs r3,r3,#1
bhi loop @while (--r3>0)
bx lr @Return
Tested on the Raspberry Pi 3; here's the testing script (C99, input through argv):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//First 2 arguments are dummies.
uint64_t minmax(int,int,int* array,size_t size);
int main(int argc,char** argv) {
int i;
int array[argc-1];
for (i=1;i<argc;i++) {
array[i-1]=atoi(argv[i]);
}
uint64_t result = minmax(0,0,array,argc-1);
printf("Minimum is %d, maximum is %d.\n",(unsigned)result,(unsigned)(result>>32));
}
Scala, 55 bytes
val s=args.map(_.toInt).sorted
print(s.head+" "+s.last)
To execute:
$ scala minmax.scala 1 2 3 4 5 6 7 8 9
zsh, 22 bytes
(){echo $1 $_} ${(n)@}
defines a lambda function that prints its first arg ($1) and the last argument to the previous command ($_), and passes it $@ after sorting it so the previous command becomes the invocation of that lambda
zsh, 21 bytes
this only works fine if there's more than 1 argument :(
<<<"${${(n)@}/ * / }"
sorts $@, makes it a string and replaces everything from the first space to the last one with a single space, then passes it as input to cat with <<<
usage:
$ ./minmax 23 342 21 10
10 342
Python, 35 34 bytes
lambda s:sorted(s+s[:1])[::len(s)]
Alternative version:
lambda s:sorted(s+s)[::len(s)*2-1]
Old version, 35 bytes.
lambda s:sorted(s+[s[0]])[::len(s)]
Fairly simple: take the input list, append the first element, sort it, then take the first and (length)th element of the resulting list. As the length of the input after appending an element is length + 1, this ends up taking the first and last element of said list, which are the minimum and maximum elements.
Octave, 35 bytes
@(x)[x(all(t=x<=x')) x(sum(t)==1)]
This is an anoynymous function. Try it at ideone.
The code avoids using sorting. Namely, it does all pairwise "less than or equal" comparisons between elements of the input. The minimum is the element for which all comparisons are true. The maximum is that for which only one comparison is true.
POSIX Awk, 44 bytes
awk '{for(;NF-1;NF--)if($1>$NF)$1=$NF}1' RS=
PHP, 44 bytes
function a($a){sort($a);echo $a[0].end($a);}
C#, 60 bytes
n=>{System.Array.Sort(n);return new[]{n[0],n[n.Length-1]};};
A naïve method at 93 bytes:
n=>{var r=new[]{n[0],n[0]};foreach(int i in n){if(i<r[0])r[0]=i;if(i>r[1])r[1]=i;}return r;};
dc, 45 bytes
[dsa]dsAx[dsz]dsZx[dla>Adlz<Zs.z0<f]dsfxlzlaf
This is a fairly straightforward equivalent of the C++ reference code:
#Test data
0 15 2 3 7 18 _2 9 6 _5 3 8 9 _14
# Uses two variables: a for min, and z for max
# Functions A and Z simply store a copy to a and z respectively. Call
# them both, to initialize the variables.
[dsa]dsAx
[dsz]dsZx
# Function f is the main work - define and call it
[
dla>A # copy into a if lower
dlz<Z # copy into z if higher
s. # discard value
z0<f # recurse until end of input
]dsfx
# print the result
lzlaf
Python 2, 34 bytes
x=sorted(input());print x[0],x[-1]
MATL, 4 bytes
S5L)
Explanation
S % Implicitly input the array. Sort
5L % Push [1 0]. When used as a (modular, 1-based) index, this means "first and last"
) % Apply as an indexing vector into the sorted array. Implicitly display
JavaScript (ES6), 34 bytes
a=>[a.sort((x,y)=>x-y)[0],a.pop()]
sort sorts in-place, so I can just refer to the [0] index for the lowest value and pop the highest value from the array, however it does a string sort by default so I have to pass a comparator.
Factor, 38 bytes
[ natural-sort [ first ] [ last ] bi ]
Sort the sequence and get its first and last elements.
Perl 6 13 bytes
*.sort[0,*-1]
Test:
my &min-max = *.sort[0,*-1];
say min-max 1;
# (1 1)
say min-max (0, 15, 2, 3, 7, 18, -2, 9, 6, -5, 3, 8, 9, -14)
# (-14 18)
Haskell, 27 bytes
f x=(`foldl1`x)<$>[min,max]
In Haskell, min and max give minimum and maximum of two arguments, not of a list. I couldn't tell whether this is disallowed (it seems that instead only minimum and maximum would be disallowed) so please let me know if they are and I'll promptly delete this answer.
CJam, 10 9 bytes
q~$_(p;W>
I'm really not good at CJam.
q~ e# eval input
$ e# sort
_ e# duplicate
( e# pop first
p e# print
; e# remove array
W> e# get last element
Brain-Flak 220 218 bytes
(({}))([]){({}[()]<(([])<{({}[()]<([([({}<(({})<>)<>>)<><({}<>)>]{}<(())>)](<>)){({}())<>}{}({}<><{}{}>){{}<>(<({}<({}<>)<>>)<>({}<>)>)}{}({}<>)<>>)}{}<>{}>[()]){({}[()]<({}<>)<>>)}{}<>>)}{}({}<((())){{}{}([][()])}{}>)
Explanation
First it doubles the top value (in cast the list is only one long)
(({}))
Then it uses my bubble sort algorithm:
([]){({}[()]<(([])<{({}[()]<([([({}<(({})<>)<>>)<><({}<>)>]{}<(())>)](<>)){({}())<>}{}({}<><{}{}>){{}<>(<({}<({}<>)<>>)<>({}<>)>)}{}({}<>)<>>)}{}<>{}>[()]){({}[()]<({}<>)<>>)}{}<>>)}{}
Then it picks up the top value of the stack (i.e. the min)
({}<...>)
Then it pops until the height of the stack is one:
((())){{}{}([][()])}{}
Actually, 5 bytes
S;F@N
Explanation:
S;F@N
S sort
; dupe
F first element
@N and last element
BASH 38
grep -o '[^ ]*'|sort -g|sed -n '1p;$p'
Ruby, 38 23 bytes
def m(v) puts v.sort![0];$><<v[-1];end
->v{[v.sort![0],v[-1]]}
Thanks @Jordan!
Retina, 17 bytes
O#`
$
¶$`
Ss`¶.+¶
Input and output are linefeed-separated.
Explanation
O#`
Sort lines by numerical value.
$
¶$`
Duplicate the entire input to make sure that the first and last element are distinct.
Ss`¶.+¶
Remove all intermediate lines, leaving only the minimum and maximum.
Haskell, 36 bytes
(\l->(head l,last l)).Data.List.sort
Would love to use &&& here but imports are so costly...
Jelly, 3 bytes
Ṣ.ị
Sort the array, and then takes the 0.5-th element.
Jelly uses 1-indexing, and floating points indexing means take its floor and its ceil.
So the 0.5-th element would give you the 0th element and the 1st element.
The 0th element is the last element.
Octave, 20 bytes
@(n)sort(n)([1,end])
This sorts the input vector and outputs the first and last value.
Mathematica, 18 bytes
Sort[#][[{1,-1}]]&
Sorts the array and extracts the first and last values.