-7
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Howard Dec 11 '11 at 17:55
  • 2
    \$\begingroup\$ Also, the definition of 'simplest' is highly subjective. \$\endgroup\$ – Zaid Dec 11 '11 at 18:42
  • 7
    \$\begingroup\$ -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) \$\endgroup\$ – J B Dec 11 '11 at 19:20
  • 1
    \$\begingroup\$ -1 boring. In Golfscript it's $0= and $-1=. \$\endgroup\$ – Peter Taylor Dec 11 '11 at 23:00
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – user unknown Dec 13 '11 at 8:00
0
\$\begingroup\$

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

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;
\$\endgroup\$
  • \$\begingroup\$ I only did this as a contest you don't need to use perl but anyway I decided to end it \$\endgroup\$ – linuxrules94 Dec 12 '11 at 23:30
1
\$\begingroup\$

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)
         (error "Read The Fucking Code!")
         (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))]))
\$\endgroup\$
1
\$\begingroup\$

If all numbers are non-negative, this works:

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

Japt -g, 1 byte Min

n

Try it online!


Japt -g, 2 bytes Max

n<

Try it online!


Total: 3 bytes

\$\endgroup\$
0
\$\begingroup\$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.