Return Entire Thread First 100 Posts Prev 100 Next 100 Last 50 Posts Report Thread Style: Yotsuba, Pseud0ch, Terminal

Pages: 1-40 41-80 81-

SICP Exercise 1.3

1

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.

2

Read SICP

3

http://dis.4chan.org/read/prog/1199677250

4

Simple shit
`if a > b and b > c, then a > c'
^^ VERY BASIC STUFF.


(define (ex3 x y z)
    (+ (square (max x y)) (square (max (min x y) z))))

5

(define (ex3 x y z)
   (- (+ (square x) (square y) (square z)) (square (min x y z))))


6

needs moar (+ (square a) (* 2 a b) (square b))

7

>>6
(a+b)^2? why? fail.

8

I don't know LISP, but if i wanted to break it down functionally, i'd break it down this way:

First have a function that takes the three numbers and returns the two largest.

Second, have a function that squares two numbers, adds them and returns the result.

The argument of the second function is the result of the of the first function etc.





The design of the first function should be trivial. Finding the two largest numbers is the hard part of the problem.

I'm too drunk too push around the variables right now so I'll lets some other adequately quallified computer scientist fag do it

9

>>8
thank you for your valuable contribution!

10

>>8
The question is;

Give a method with the lowest number of comparisons for finding the two greatest numbers out of a set of three numbers.

to all the code monkeys on /prog/, this is the pure computer science shit you will never, ever know.

11

Use foldr to evolve max.

12

>>4
>>5
SICP doesn't even mention min and max at that point.

>>11
or foldr.

13

You used a Fold Stone on max.
What's that? max is evolving?

<<da da da da da da
dadadadadadadada
dada dada!>>

Congratulations! max now accepts arbitrary number of arguments!

14

This is very easy. You should've been able to figure this out yourself.

(define (square a) (* a a))
(define (sum-of-squares a b) (+ (square a) (square b)))
(define (ex3 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))))


It is simply a process of working out the smallest number, and then you know which two are the largest.

15

>>12
(define (min a b)
       (if (< a b) a b))

(define (max a b)
       (if (> a b) a b))


Surely you should be smart enough to define it yourself.

16

>>14
You could probably add an else case as

(else (sum-of-squares x y))

for when all the values are the same. It would depend on whether the problem states that there will always be two larger values or not.

17

>>4
Best solution.

18

: ex3 ( x y z -- r ) 3array natural-sort [ sq ] each + nip ;

19

>>18
Terrible solution.

20

>>18
NOT LISP

21

>>19
It's the best solution in this thread. That's not saying much, though.

>>20
REAL COMPUTER PROGRAMS ARE NOT WRITTEN IN LISP

22

>>21
ARE YOU STUPID OR SOMETHING. IT IS NOT A CHALLENGE TO DO SOMETHING USEFUL IN REAL LANGUAGES, IT IT HOWEVER TO DO IT IN RISPU. THANK YOU FOR YOUR ATTENTION.

(FUNNY FACT: I HAD NO IDEA I HAD THE CAPS LOCK ON WHEN WRITING >>20)
(FUNNY FACT: YOU JUST LOST THE GAME)

23

>>22
IT IT?
>>21
No it's not, you are using a fucking sort algorithm.

24


<?php
function faggot(){$a=func_get_args();@array_pop(rsort($a));return(int)($a[0]*$a[0])+($a[1]*$a[1]);}
echo faggot(7,5,3)
?>

25

sicp a b c = sum $ map (^2) $ tail $ List.sort [a,b,c]
lol readable

26

>>25
FAILTROLL

27

OP here, figured out to find the smallest number next time I sat down. Thanks for all the examples though.

I got this:

(define (ex3 x y z)
  (cond ((< x (+ y z)) (sum-of-squares y z))
    ((< y (+ z x)) (sum-of-squares z x))
    ((< z (+ x y)) (sum-of-squares x y))))

28

>>27
(ex3 3 2 2)

29

>>27
Will you faggot try to understand what >>4 posted?
Please?

30

>>25
lol readable
sicp a b c = case List.sort [a, b, c] of [_, x, y] -> x ^ 2 + y ^ 2

31

>>28
THIS INPUT DOES NOT HAVE 3 LARGEST NUMBERS

32

>>29
I do understand what >>4 posted. I used a variant of
if a>b and b>c then a>c
where
if a>b and b>c then a+b>c (where a and b are positive)
to solve it.

33

I wants lots and lots of some delectable pot!

Marijuana MUST be legalized.

34

>>4

max is not allowed.

35

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

TOO EASY

36

>>35

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

37

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

38

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

39

>>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.

40

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

41

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))))))

42

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

43

>>42
That's not a solution.

44

>>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.

45

>>44
http://img168.imageshack.us/img168/4667/scip.jpg

46

>>45
weaboo faggot

47

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

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

48

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

49

bitches and hoes

50

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

51

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

52

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

53

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

54

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.

55

>>48

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

56

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

57

(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)))))

58

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.

59

>>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?

60

#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))));
}

61

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

62

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

63


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

amidoinitrite?

64

>>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.

65

(͝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))))̢)̵

66

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

67

>>66

We're doing toy programs here ..

oh I see

68

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

69

>>65
VALID PERL CODE

70

>>65

Why did you scribble all over my code?!

71

>>51
pix plox

72

>>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.

73

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

74

(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

75

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)))

76

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

77

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

78

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)

79

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

80

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

81

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;
}

82

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

83

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

I feel like an idiot.

84

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

85

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

86

>>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.

87

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


Fixed it for you.

88

>>13
POKÉMON¡

89

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

FTFY

90

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

91

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

92

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

DEADDOGS

93

Could've dropped instead. Oh well.

94

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

95

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

96

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;
}

97

>>82
FIOC still sucks donkey dick however

98

>>97
back to /pr/, please.

99

"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.

100

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

101

>>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.

102

>>101
0/10

103

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

104

>>103
They don't ban the mods.

105

>>104
Well, they should.

106

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

107

CHOKE TO DEATH ON MY ANUS

109

110

111


Newer Posts

Name: Email:
112
Entire Thread Last 50 Posts First 100 Posts Thread List Report Thread