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!