# Help with this

**URL:** <https://www.ruby-forum.com/t/help-with-this/178673>\
**Category:** Ruby\
**Created:** [November 9, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/help-with-this/178673 "2009-11-09T12:09:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/1 "2009-11-09T12:09:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/2 "2009-11-09T12:42:02Z")

</div>

Hi John,

On Mon, Nov 9, 2009 at 11:09 AM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://ruby-doc.org/core/classes/Enumerable.html#M003127)

- Ian 🙂

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/3 "2009-11-09T12:48:28Z")

</div>

John M. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 12:53pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/4 "2009-11-09T12:53:15Z")

</div>

John M. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 12:53pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/5 "2009-11-09T12:53:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 12:51pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/6 "2009-11-09T12:51:44Z")

</div>

hi Ian

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

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 1:56pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/7 "2009-11-09T13:56:26Z")

</div>

On 2009-11-09, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/8 "2009-11-09T13:03:18Z")

</div>

I am completely stuck I dont know what to do…

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 9, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/help-with-this/178673/9 "2009-11-09T14:00:32Z")

</div>

On 2009-11-09, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
