# Code Golf: simple way to get min & max

You will take a set of input numbers, and output the minimum and maximum of the set. You can either write two programs, with 1 program doing each function, or one combined program.

Here's an example in Perl, 50 and 42 bytes (92 total):

Max:

sub mx {return ( reverse sort {$a <=>$b} @_ )[0]}


Min:

sub mn {return ( sort {$a <=>$b} @_ )[0]}


And a single program, 96:

sub mxmn {return ( reverse sort {$a <=>$b} @_ )[0] if shift; return ( sort {$a <=>$b} @_ )[0]}


Here's some more test cases.

• Welcome to code-golf. Please specify on what kind of input it should run (see faq on how to ask questions here). You also have to decide on what you wan't: "simplest way", "code-golf" or "fastest-code"? I suspect they contradict each other. – Howard Dec 11 '11 at 17:55
• Also, the definition of 'simplest' is highly subjective. – Zaid Dec 11 '11 at 18:42
• -1 because of: 1) language-specific, 2) overly broad (min or max? strings? sweet and simple despite the code-golf tag?); 3) a bit misinformed (Perl has min and max) – J B Dec 11 '11 at 19:20
• -1 boring. In Golfscript it's $0= and $-1=. – Peter Taylor Dec 11 '11 at 23:00
• It's a one-liner in many languages - not challenging. Prepare your next question in the chat or on meta, where you get hints before going public. I vote to close. – user unknown Dec 13 '11 at 8:00

## Perl, 28 char

This sub provides an array that contains the min in the first member and the max in the second member:

sub a{@_=sort@_;(shift,pop)}


## Perl, 34 char (27 inside the sub)

According to comments, in fact the above doesn't work for array length less than 2, here a new attempt that works even for zero length array and array that contains only numerics:

sub a{@_&&(sort{$a<=>$b}@_)[0,-1]}

• whoa, its 28 char and does min and max and is easy to read? amazing! great job! – linuxrules94 Dec 11 '11 at 20:00
• Are you sure that this works e.g. if you provide a single number (and not a list)? – Howard Dec 11 '11 at 21:20
• Make it shorter and handle the degenerate case: replace (shift,pop) with a slice: @_[0,-1] – J B Dec 11 '11 at 21:58
• And then get rid of temporaries: sub a{(sort@_)[0,-1]} is 21 characters long. – J B Dec 11 '11 at 22:00
• I like this one because you used a language not dedicated to code golf – linuxrules94 Dec 11 '11 at 23:42

Perl does have min and max subs.

use List::Util qw( min max );
my $min = min @values; my$max = max @values;


A common module has a minmax function which finds both at once faster than they can be found individually (which is far more important than compactness of code).

use List::MoreUtils qw( minmax );
my ($min,$max) = minmax @values;

• I only did this as a contest you don't need to use perl but anyway I decided to end it – linuxrules94 Dec 12 '11 at 23:30

If it doesn't have to be Perl and you just want the simplicity of Scheme instead of just shaving off characters (I don't care about long variable names (symbols in Scheme)), here's my Scheme for this (pun intended):

Of course we could cheat and use the fonctions (min a b c d ...) and (max a b c ...).

Don't mind the long variable names, they were chosen to be descriptive:

(define (min . args) (extremum < args))
(define (max . args) (extremum > args))

(define-syntax extremum
(syntax-rules ()
[(_ op . args)
(if (null? args)
(reduce (lambda (a b) (if (op a b) a b))
(car rest) (cdr rest)))]
[else (error "Read The Fucking Code!")]))


But if you want less code (fold is sort of like reduce):

(define (min . args) (e < args)) (define (max . args) (e > args))
(define-syntax e
(syntax-rules ()
[(_ o . r) (fold (lambda (a b) (if (o a b) a b)) (car r) (cdr r))]))


If all numbers are non-negative, this works:

sub max{\@s[@_];$#s}  • a.k.a. Thelen's Device. I prefer to write it as \@_[@_];$#_, but only because it's more obscure that way. – primo Sep 18 '13 at 11:17

# Japt-g, 1 byte Min

n


Try it online!

# Japt-g, 2 bytes Max

n<


Try it online!

# Total: 3 bytes

I don't think Perl is very golfy for such a problem.

Here are two alternatives for numerical lists only:

20 chars (inside sub)

sub min{(sort{$a<=>$b}@_)[0]}
sub max{(sort{$b<=>$a}@_)[0]}


30 chars (inside sub)

sub min{$r=pop;$r=$_<$r?$_:$r for@_;$r} sub max{$r=pop;$r=$_>$r?$_:$r for@_;$r}

• Wow amazing only 20 characters! "the greatest thing since sliced bread!" as they would say – linuxrules94 Dec 11 '11 at 19:14
• or the greatest thing since [soda] pop (pun intended) – linuxrules94 Dec 11 '11 at 19:15
• you may rename the subs to n & x or mx & mn – linuxrules94 Dec 11 '11 at 19:42
• or use a different language – linuxrules94 Dec 11 '11 at 19:43