Help with this

I have a question to answer but can not find any solution for this
question.

You have a hash containing elements of quantity 'x' which you may
define. Each value within the hash is an array containing elements of
quantity 'y'. Return a new array containing the last element of each
array. Demonstrate with code.

For the life of me I can not think of a solution please could someone be
kind to provide me a solution in code for this I will forever be
thankful

···

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

Hi John,

···

On Mon, Nov 9, 2009 at 11:09 AM, John Muller <nish.patel@hotmail.com> wrote:

You have a hash containing elements of quantity 'x' which you may
define. Each value within the hash is an array containing elements of
quantity 'y'. Return a new array containing the last element of each
array. Demonstrate with code.

Have a look at 'collect', it should help you out!
http://ruby-doc.org/core/classes/Enumerable.html#M003127

- Ian :slight_smile:

John Muller wrote:

I have a question to answer but can not find any solution for this
question.

You have a hash containing elements of quantity 'x' which you may
define. Each value within the hash is an array containing elements of
quantity 'y'. Return a new array containing the last element of each
array. Demonstrate with code.

This is obviously a homework problem. You will learn more if you do not
ask others to do your homework. Also, you will gett better responses if
you use a meaningful subject line.

For the life of me I can not think of a solution please could someone be
kind to provide me a solution in code for this I will forever be
thankful

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

I have a question to answer but can not find any solution for this
question.

Okay.

You have a hash containing elements of quantity 'x' which you may
define. Each value within the hash is an array containing elements of
quantity 'y'. Return a new array containing the last element of each
array. Demonstrate with code.

For the life of me I can not think of a solution please could someone be
kind to provide me a solution in code for this I will forever be
thankful

As you say, it's homework, so let's help you by helping you walk through it.

This problem's a bit vaguely specified, and I'm not sure I think it's
specified correctly. But.

Let's start with a simpler problem.

Say you have an array. Can you figure out a way to produce the last element
of the array? Let's simplify; imagine that we have a value y, and we know
the array has y elements:

  a = [ 1, 2, 3 ]
  y = 3

How would you get the y'th element of the array? (Remember that Ruby, like
most computer languages, but unlike most people, indexes from zero rather than
1, so the 1st element is element zero.)

Once you have a way to do that, the text question is: You have a hash,
containing a bunch of objects. You have a thing you want to perform on
every object in the hash. How can you perform that thing on every object
in the hash?

Note that this is a very easy one-liner if you know all the methods for
Hash and Array, but you can do it in a more elaborate, but easier to follow,
way if you prefer.

Basically, there are two things you need to do when you can't even figure
out how to get started on a problem:

1. Subdivide it into component tasks.
2. Look at docs to see if you can find ways to do it.

If you can't find an easy way to do one of the component tasks, maybe it
needs to be subdivided again.

So my approach to this would be to start by saying "clearly, we want to do
something for each member of the hash." Then, what do we want to do? Extract
some value from the member and put it in an array. Well, it's pretty easy
to put values in an array. There's lots of ways to iterate over a hash.
Etecetera...

-s

···

On 2009-11-09, John Muller <nish.patel@hotmail.com> wrote:
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

hi Ian

I am a complete novice at this so would it be this as the solution

(x).collect {|i| i*i } #=> [y]

···

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

Yes it is homework problem but I have tryed to find this solution but I
need help I am learning but I just find some stuff more difficult than
others I am sorry to ask you guys but you learn if you knew the answer

···

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

This is obviously a homework problem.

Agreed.

You will learn more if you do not
ask others to do your homework.

Unless the others are bored and give you "interesting" answers. I'm
not good enough at Ruby to give people homework answers, but in C, I
like to provide high-quality implementations which carefully misinterpret
the spec in some interesting way but are otherwise highly robust,
well-tested, and written *extremely* badly -- using advanced techniques.

In short, programs which any reader will immediately realize were
not produced by the student.

This way, if the student wants to take advantage of the "free" homework
answer, figuring out what's going on (and correcting any misreading
of the spec) can actually be pretty educational.

-s

···

On 2009-11-09, Marnen Laibow-Koser <marnen@marnen.org> wrote:
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

John Muller wrote:

hi Ian

I am a complete novice at this so would it be this as the solution

(x).collect {|i| i*i } #=> [y]

Why don't you try it and find out?

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

I am completely stuck I dont know what to do...

···

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