Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

SICP Exercise 1.3

Name: Anonymous 2008-01-19 7:59

Exercise 1.3.  Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

(define (ex3 x y z)
                     (cond
                       ((> y z x) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> y x z) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> z y x) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> z x y) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> x z y) ((define a x) (define b y) (+ (square a) (square b))))
                       ((> x y z) ((define a x) (define b y) (+ (square a) (square b)))))


I know I'm thinking about this the wrong way. My solution doesn't work.

Name: Anonymous 2010-10-05 11:48

int ex3(int a, int b, int c) {
  int x;
  if (a < b) x = a, a = b, b = x;
  if (b < c) x = b, b = c, c = x;
  return a*a + b*b;
}

Name: Anonymous 2010-10-05 11:50

>>79
The FIOC was shorter, cleaner and faster as well.

Name: >>80 2010-10-05 11:56

Correction.
+/1↓X[⍋X]*2

I feel like an idiot.

Name: Mr. Vile !l9/dToUs32 2010-10-05 12:11

    #!/usr/bin/perl
    @_ = sort @ARGV;
    print $_[1]**$_[1]+$_[2]**$_[2];

Name: Anonymous 2010-10-05 12:16

>>84
And people say Perl is ugly. That is the most beautiful solution in this thread.

Name: Anonymous 2010-10-05 13:09

>>84,85
It's also completely wrong.  It doesn't square the arguments (unless they happen to be 2) and it sorts them as strings instead of numbers.

Also, assigning to @_ considered harmful.

Name: Anonymous 2010-10-05 13:22

>>84-86
#!/usr/bin/perl
@_ = sort { $a <=> $b} @ARGV;
print $_[1]**2+$_[2]**2;


Fixed it for you.

Name: Anonymous 2010-10-05 13:34

>>13
POKÉMON¡

Name: Anonymous 2010-10-05 14:17

>>87
#!/usr/bin/env perl

FTFY

Name: Anonymous 2010-10-05 14:31

Also, Perl 6:
sub anus($a, $b, $c) {
  [+] ($a, $b, $c).sort.reverse[0,1].map: {$_*$_}
}

Name: Anonymous 2010-10-05 14:55

>>86
Lucky it doesn't happen on every statement then, huh.

Name: Anonymous 2010-10-05 14:58

ex3 = sum . map (**2) . take 2 . reverse . sort

DEADDOGS

Name: Anonymous 2010-10-05 14:59

Could've dropped instead. Oh well.

Name: Anonymous 2010-10-05 15:29

#!/usr/bin/perl
use List::Util 'sum';
print sum(map $_**2, (sort {$a <=> $b} @ARGV)[1,2]),"\n";

Name: Anonymous 2010-10-05 17:01

#!/usr/bin/env python
print reduce(lambda a,b:int(a)**2+int(b)**2,sorted(__import__('sys').argv[1:])[-2:])

Name: Anonymous 2010-10-05 17:32

enterprise c


#include <stdio.h>
#include <stdlib.h>

typedef double D;

int C(const void *va, const void *vb) {
    D a=*(D*)va, b=*(D*)vb;
    return a<b ? -1 : a>b ? 1 : 0;
}

D F(D a, D b, D c) {
    D A[3] = {a,b,c};
    qsort(A,3,sizeof(D),C);
    return A[1]*A[1]+A[2]*A[2];
}

int main() {
    printf("%f\n",F(3,2,1));
    return 0;
}

Name: Anonymous 2010-10-05 17:48

>>82
FIOC still sucks donkey dick however

Name: Anonymous 2010-10-05 19:27

>>97
back to /pr/, please.

Name: Anonymous 2010-10-05 20:21

"use strict";

// Defines a procedure that takes three numbers as arguments
// and returns the sum of the squares of the two larger numbers.
// @param a A number
// @param b A number
// @param c A number
// @return The sum of the squares of the two larger numbers
function eq3_1(a, b, c) {
  var args = Array.prototype.slice.apply(arguments);
  var n = Math.max.apply(null, args);
  var m = Math.max.apply.(null, args.splice(args.indexOf(n)), 1);
  return n*n + m*m;
}


This will also work for more than 3 numbers.

Name: Zaaaaaaaaaaaaaaaaaaaaaaaarn 2010-10-05 21:32

>>98
How about no? That works for me.

Name: Anonymous 2010-10-05 21:40

>>98
Gimme a break FIOC is so easy even girls can use it. One Leah Culver for instance. This fact alone proves it's useless for programming.

Name: Anonymous 2010-10-05 21:42

>>101
0/10

Name: Anonymous 2010-10-05 21:56

>>100
Got banned from 7chan, did you?

Name: Anonymous 2010-10-05 23:33

>>103
They don't ban the mods.

Name: Anonymous 2010-10-05 23:47

>>104
Well, they should.

Name: Anonymous 2010-10-05 23:53

>>105
You should go choke to death on a dick

Name: Anonymous 2010-10-06 20:23

CHOKE TO DEATH ON MY ANUS

Name: Anonymous 2010-12-21 1:06

Name: Anonymous 2011-02-04 13:19

Name: Anonymous 2011-02-04 15:23


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List