Beginner needing help - Writing right-angle triangle program

I’ve been going through a Haskell tutorial (Just to see what it’s like)
and saw an example program which would tell me how long each side a
right-angle triangle is, when given the perimeter. It seemed pretty
simple, and me, being the kind of guy that likes to write the same
program in multiple languages, tried to write it in ruby.

I’m pretty stumped though, and would like someone a little higher up to
give me a nudge in the right direction :D.

(Eg. program would return (6, 8, 10) and (8, 6, 10) when you say that
the perimeter is 24)

Thanks in advance!

I couldn’t help but notice that this is exactly the same email
as sent yesterday by “Kane W.” (see below). Are you
connected somehow?

In any case, it is hard to say anything useful without seeing your code
or a precise description of the task. i.e. - There are an infinite
number
of right triangles for a given perimeter. So given just your brief
description, the problem doesn’t sound well formed.

Regards,
Dan N.

On Sep 3, 2011, at 9:49 AM, Patrick L. wrote:

the perimeter is 24)

Thanks in advance!


Posted via http://www.ruby-forum.com/.

On Sat, Sep 3, 2011 at 8:49 AM, Patrick L.
[email protected]wrote:

the perimeter is 24)

Thanks in advance!


Posted via http://www.ruby-forum.com/.

Show us your Haskell and Ruby code.

There are a lot of ways to generate pythagorean triples (what I assume
you’re actually asking for), so hard to translate it without seeing the
original. Also, just FYI, this is the domain of problem that Haskell was
made for, but Ruby isn’t particularly good at math algorithms.

On Sep 3, 2011, at 7:49 AM, Patrick L. wrote:

I’ve been going through a Haskell tutorial (Just to see what it’s like)
and saw an example program which would tell me how long each side a
right-angle triangle is, when given the perimeter. It seemed pretty
simple, and me, being the kind of guy that likes to write the same
program in multiple languages, tried to write it in ruby.

Here’s a simple, rather nave approach:

def right_triangle_sides( circumference )
[].tap do |solutions|
1.upto(circumference-2) do |a|
1.upto(a-1) do |b|
c = circumference-a-b
if aa+bb == c*c
solutions.concat( [a,b,c].permutation.to_a )
end
end
end
end
end

right_triangle_sides(24).each do |sides|
p sides
end
#=> [8, 6, 10]
#=> [8, 10, 6]
#=> [6, 8, 10]
#=> [6, 10, 8]
#=> [10, 8, 6]
#=> [10, 6, 8]

On Sep 3, 2011, at 2:16 PM, Adam P. [email protected] wrote:

end
end
end
A very curious use of #tap.

:slight_smile: I get a lot of flack for “abusing” tap like this. I happen to find
this pattern annoying:

def xyz
  foo = ...
  # do things to foo
  foo
end

and so have taken to using the very-low-impact of tap to make it more
functional-style:

def xyz
  ....tap do |foo|
    # do things to foo
  end
end

On Sat, Sep 3, 2011 at 4:36 PM, Gavin K. [email protected] wrote:

end
end

A very curious use of #tap.

Gavin K. wrote in post #1020000:

On Sep 3, 2011, at 2:16 PM, Adam P. [email protected] wrote:

end
end
end
A very curious use of #tap.

:slight_smile: I get a lot of flack for “abusing” tap like this. I happen to find
this pattern annoying:

def xyz
  foo = ...
  # do things to foo
  foo
end

So when you read the thread title, “Beginner needing help”, you thought
you would make some moves to impress the new guy, and show him how
unintelligible ruby is?

On Sep 3, 2011, at 8:57 PM, 7stud – wrote:

 foo

end

So when you read the thread title, “Beginner needing help”, you thought
you would make some moves to impress the new guy, and show him how
unintelligible ruby is?

No, that was not my intention. I don’t personally find it
unintelligible; indeed, other than the name of the method “tap” I find
that pattern to look more elegant then a clumsy repetition of variable
names. (If the method were named something like “named” or “using” or
“with” my particular usage might read slightly better.)

For comparison and instruction of a new user, however, here’s my
solution slightly changed to use what I deem to be the uglier code, and
what I suppose 7stud feels is more attractive:

def right_triangle_sides( circumference )
solutions = []
1.upto(circumference-2) do |a|
1.upto(a-1) do |b|
c = circumference-a-b
if aa+bb == c*c
solutions.concat( [a,b,c].permutation.to_a )
end
end
end
solutions
end