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 2009-03-18 3:41

I wants lots and lots of some delectable pot!

Marijuana MUST be legalized.

Name: Anonymous 2009-09-04 17:40

>>4

max is not allowed.

Name: Anonymous 2009-09-04 17:57

def ex3(a,b,c); [a,b].max**2+[[a,b].min,c].max**2; end

TOO EASY

Name: Anonymous 2009-09-04 17:58

>>35

also, if you need to use any kind of sort to solve this exercise you are fucking pathetic. go back to /b/ immediately.

Name: Haxus the SICP-reader 2009-09-04 17:58

I'm actually working on this right now myself.  I must shield my eyes from this thread until I solve it on my own!

Name: Sagey McSagerson !!WGlXfZ0twvi7ADs 2009-09-04 18:03

For this act of Thread Necromancy >>33-kun is awarded the rank of Lesser Thread Necromancer.


32  Name: OP : 2008-01-21 20:31
[omitted for brevity]
33 Name: Anonymous : 2009-03-18 03:41

Name: Anonymous 2009-09-04 18:04

>>10

Input: a, b, c
Output: x, y

If (a > b)
 x = a
 if (b > c); y = b; else; y = c; end
else
 x = b
 if (a > c); y = a; else; y = c; end
end

needs three comparisons.

Name: Anonymous 2009-09-04 19:28

(define max
    (λ(a . rest)
      (let L ((max a)
              (rest rest))
        (if (empty? rest)
            a
            (L (if (> a (car rest)) a (car rest)) (cdr rest))))))

Name: Anonymous 2009-09-04 19:30

which of course should be
(define max
    (λ(a . rest)
      (let L ((max a)
              (rest rest))
        (if (empty? rest)
            max
            (L (if (> a (car rest)) a (car rest)) (cdr rest))))))

Name: Haxus the Novice 2009-09-05 5:55

Fuck, I came up with this after reading the first sections of SICP (leading up to the exercise obviously):

(define (sum-of-high-two-sq a b c)
  (cond ((< a b) (if (< a c)
                             (+ (square b) (square c))
                             (+ (square a) (square b))
                 ))
        (else (if (> b c)
                          (+ (square a) (square b))
                          (+ (square a) (square c))))))


As I look through the code posted in this thread I realize I still have far to go to become an EXPERT PROGRAMMER

Name: Anonymous 2009-09-05 6:20

>>42
That's not a solution.

Name: Haxus the Failed 2009-09-05 6:27

>>43
Then I have even farther to go than I thought!

Nevertheless, I read >>4-chan's solution since I created my failed attempt so I'll just have to try harder on the next exercises.

Name: Anonymous 2009-09-05 6:45

Name: Anonymous 2009-09-05 7:29

>>45
weaboo faggot

Name: Anonymous 2009-09-05 7:32

>>46
Welcome to 4chan. Please, enjoy your stay.

http://www.4chan.org/japanese/

Name: Anonymous 2009-09-05 9:06

>>46
oh, wow.
you do realise that /prog/'s sister board is /jp/ right?

Name: bitches and hoes 2009-09-05 9:21

bitches and hoes

Name: Anonymous 2009-09-05 9:56

>>48
This gives a whole new meaning to ``Hax my anus, onee-chan!''. ( ≖‿≖)

Name: Anonymous 2009-09-05 10:02

>>50
Goddammit, now I'm imagining an older sister /jp/ totally raping /prog/ and I'm enjoying it ;)

Name: Anonymous 2009-09-05 10:05

(define (sum-largest-two x y z)
 
(sum (largest-two (list x y z))))

Name: Anonymous 2009-09-05 10:07

(define (sum-largest-two x y z)
 
(sum (map square (largest-two (list x y z)))))

Name: Anonymous 2009-09-05 10:28

i was going to reply with:


(define (sum-two-biggest-squares x y z)
  (apply + (map (lambda (x) (* x x))
                (filter (lambda (x) (not (eq? x (min x y z)))) (list x y z)))))


but this fails on certain types of input.

Name: Anonymous 2009-09-05 10:29

>>48

You do realise I just grabbed your dick right? *grabs dick*

Name: Anonymous 2009-09-05 10:29

>>50,51
We should have an artist illustrate it.

Name: Anonymous 2009-09-05 10:31

(define (sum-two-biggest-squares x y z)
  (let ((sorted (sort (list x y z) >))
        (square (lambda (x) (* x x))))
    (+ (square (car sorted))
       (square (cadr sorted)))))

Name: Anonymous 2009-09-05 12:09

Fucking faggotry. There's three solutions, nested conditional, sorting the sequence, or using min/max. this thread is over. anything else is posted by fucking faggots that shouldn't be on /prog/ anyawy and the only thing they do is pollute it with nonsense unrelate to programming and redundant crap.

Name: Anonymous 2009-09-05 13:22

>>58
the only thing they do is pollute it with nonsense unrelate to programming and redundant crap.
Wait a minute... It took you this long to figure that out?

Name: Haxus the C-Lisper 2009-09-05 19:43

#define MIN( a , b ) ((a<b)?a:b)
#define MAX( a , b ) ((a>b)?a:b)

#define SQUARE( x ) (x*x)

uint32_t ex3(uint32_t x, uint32_t y, uint32_t z)
{
        return (SQUARE(MAX(x,y)) + (SQUARE(MAX(MIN(x,y),z))));
}

Name: Anonymous 2009-09-05 23:43

(((7 8 +) 5 /) e -)

Name: Anonymous 2009-09-06 1:21

>>61
Is it Forth, or is it Lisp? FITHP!

Name: Anonymous 2009-09-06 3:22


function gsq(a,b,c) return a^2 + b^2 + c^2 - math.min(a, b, c)^2 end

amidoinitrite?

Name: Anonymous 2009-09-06 5:09

>>63
No. This uses a tonne of extra clock cycles. If you had any clue how compilers work you would understand why this is going to take about 2-3 times as long as conditionals.

Name: Anonymous 2009-09-06 5:47

(͝define (͏ęx͡3̀ x҉ ͟y z)͡
̧ ̷ ҉ ̡ ͟ ̡ ̸ ͠ ́ ̸ ͟ ͠(c̡on̨d
̴ ͢ ̛ ҉ ̛ ̢ ̢ ͟ ҉ ͡ ̧ ̷ ̸ ̡ ͢ ̴ ̡(̢(̨>͞ y z͘ x̵)̢ ͜((҉de͝fi̷ǹe a ͘x)̵ (͠d̨e̕f̀in͠e ͟b̛ ̵y̴)̷ (+ (s̨qu̸are a͡)̨ (sq͝ua̕rȩ b))͝)̡)
̕ ̕ ͝ ҉ ҉ ͜ ̶ ̵ ̢ ((͜>̵ y x́ ź)҉ ͘(͝(̴def͘i͢n͞e ̀a x)͞ (defi͜n͝e͢ b y͢) ̢(+ ̨(͜śq̛u̢ar͠e ̕a͏)͝ ͘(s҉q͜u̸àre͢ ̴b))͢))
͟ ̨ ̢ ́ ̵ ̢ ͟ ͝ ͡(͡(͟>̡ z̢ y x͠) (́(́d͘efi͘ņe͘ a ͠x̴)́ (͏defi̷n͞e b ̧y̷)̨ (̨+ (͝sq͞u̸áre ̸a̧)͢ ̡(s̶q͠uar̵e̸ b̶)͞)̧)̧)
̴ ̷ ͏ ̢ ̢ ̵ ̷ ̶ ̧((> ̧z x y)̀ (҉(͝d͞ef̵in͢e ͢a x͠)͡ (d̷ef͜i͏nę ̶b͏ ̧ỳ)҉ ͟(+ ͢(̸s̡q̡uare ̨a͠) (sq̢u͞are ̵b))))̡
̢ ̷ ͝ ̶ ̸ ̨ ̸ ͠ ̀ ̸ ҉ ͘ ((͘≯ x z̸ ̸y) ̀(҉(d́efi҉ne a ̨x) ͟(͜de͘fi̶n̛e ̕b y) ̨(͡+ ͟(sq͘uar̛e̛ a)͝ ͠(̢s̵q̢ua̶re ́b͜))̷)̀)
̡ ̕ ͘ ̕ ̕ ̸ ͜ ̕ ̷ ͘ ̷ ̡ ̕ ((> x ҉y z) ((define a ̕x͝) ́(de̸fin͢e͘ b y) ̛(+͘ (̛s̀q͜u̢a͘re a̷) ̨(̛squar͏e ̕b))))̢)̵

Name: Anonymous 2009-09-06 5:48

>>64
We're doing programming textbook exercises here, not enterprise scalable work

Name: Anonymous 2009-09-06 7:37

>>66

We're doing toy programs here ..

oh I see

Name: Anonymous 2009-09-06 7:47

>>67
Most textbook exercises ask you to write toy programs so you could further your knowledge. I don't see a problem with that.

Name: Anonymous 2009-09-06 10:04

>>65
VALID PERL CODE

Name: Anonymous 2009-09-06 14:16

>>65

Why did you scribble all over my code?!

Name: Anonymous 2009-10-25 19:14

>>51
pix plox

Name: QBasic Master 2009-10-29 1:33

>>60
I like this one best.

I will now attempt to solve this in QBasic.

DECLARE FUNCTION MAX% (x%, y%)
DECLARE FUNCTION MIN% (x%, y%)
DECLARE FUNCTION SQUARE% (x%)

FUNCTION MAX% (x%, y%)
    IF x% > y% THEN MAX% = x% ELSE MAX% = y%
END FUNCTION

FUNCTION MIN% (x%, y%)
    IF x% < y% THEN MAX% = x% ELSE MAX% = y%
END FUNCTION

FUNCTION SQUARE% (x%)
    SQUARE% = x% * x%
END FUNCTION


' This is the main program
CLS

INPUT "Enter 3 integers: ", a%, b%, c%
PRINT
PRINT SQUARE%(MAX%(a%, b%)) + SQUARE%(MAX%(MIN%(a%, b%), c%))

END


Didn't run it in the interpreter yet but it should work to the best of my knowledge.

Name: QBasic Master 2009-10-29 1:40

ah shit, already see an error in MIN%.  will fix it when I actually run the code.

Name: ty 2010-10-05 7:07

(define (square a) (* a a))
(define (sum-of-squares a b) (+ (square a) (square b)))
(define (func x y z)
  (cond ((and (>= y x) (>= z x)) (sum-of-squares y z))
        ((and (>= x y) (>= z y)) (sum-of-squares x z))
        ((and (>= x z) (>= y z)) (sum-of-squares x y))))

works with all combinations of input right

Name: Anonymous 2010-10-05 7:46

74 posts and no recursive solution?
(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (sum-of-largest-two-squares x y z)
  (if (and (<= x y) (<= x z))
      (sum-of-squares y z)
      (sum-of-largest-two-squares y z x)))

Name: Anonymous 2010-10-05 9:16

lambda *n: sum(map(lambda x: x * x, sorted(n)[-2:]))

Name: Anonymous 2010-10-05 9:22

>>72
Know what I liked about QBasic? UPPERCASE KEYWORDS and relatively devoid of ant shit (;:.,). I loathe type characters though.

Name: Anonymous 2010-10-05 9:49

SLOW AS FUCK HIPSTER


#!/usr/bin/env ruby

def foo(a,b,c)
    l=0
    r=0
    [a,b,c].each{|e|e<l&&l=e}.reject{|e|e==l}.map{|e|e*e}.each{|e|r+=e}
    r
end

puts foo(10,3,5)

Name: Anonymous 2010-10-05 11:19

>>78
...and yet cleaner, easier to read and much more intelligible than all the other Lithpth code posted. Feature that.

Name: Anonymous 2010-10-05 11:32

X←6 3 4
+/2*1↓X[⍋X]

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