From: Javier Hidalgo Villegas Date: 2013-05-16T07:25:38+09:00 Subject: Re: n00b looking to learn from the best --_78da1618-610d-4a07-bfd8-132982204306_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi you can use the #each_with_index method to iterate through the array and= also get the position number of each element=2C then call #map to build an= array with the result of each iteration and finally the #inject method com= bines all the elements in the array with the + method. def lassy_sum(numbers) # if numbers is [1=2C2=2C3] result =3D numbers.each= _with_index.map { |n=2C i| n*i } # this will return [0=2C2=2C6] result.inj= ect(:+) # this will return 8end By the way=2C the # character in Ruby after each line means that everything= after it is a comment and doesn't affect the execution of the code.You can= try this code on irb=2C just do the following: 1. Open your terminal 2. Type: irb3. Press Enter4. Paste the code that i h= ave proposed5. Type: lassy_sum [1=2C2=2C3]6. It should return: 8 Javier Hidalgo V. L=EDder de Desarrollo > Date: Thu=2C 16 May 2013 06:46:51 +0900 > From: lists@ruby-forum.com > Subject: n00b looking to learn from the best > To: ruby-talk@ruby-lang.org >=20 > Hey Rubyians! >=20 > I am not used to joining online forum's and I am eager to join the Ruby > community. This is the first language I am learning and have a practice > problem that I cannot seem to get. Can you enlighten me? I'm looking > forward to learning from all of you! >=20 > Here is the question: >=20 > Write a method named lassy_sum(numbers) that takes an array of numbers. > lassy_sum should multiply each number in the array by its position in > the array=2C and return the sum. >=20 > I don't quite understand how to multiply each number in the array by its > position in the array. Any help will be greatly appreciated! Thanks. >=20 > Flex >=20 > --=20 > Posted via http://www.ruby-forum.com/. >=20 = --_78da1618-610d-4a07-bfd8-132982204306_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi you can use the #each_wi= th_index method to iterate through the array and also get the position numb= er of each element=2C then call #map to build an array with the result of e= ach iteration and finally the #inject method combines all the elements in t= he array with the + method.

def lassy_sum(numbers)= # if numbers is [1=2C2=2C3]
 =3B result =3D numbers.each_wit= h_index.map { |n=2C i| n*i } # this will return [0=2C2=2C6]
 = =3B result.inject(:+) # this will return 8
end

By the way=2C the # character in Ruby after each line means that everythi= ng after it is a comment and doesn't affect the execution of the code.
= You can try this code on irb=2C just do the following:

=
1. Open your terminal =3B
2. Type:  =3Birb
3. Press Enter
4. Paste the code that i have proposed
= 5. Type: lassy_sum [1=2C2=2C3]
6. It should return: 8

Javie= r Hidalgo V.
L=EDder de Desarrollo=



>=3B Date: Thu=2C 16 May 2013 06:46:51 +0900=
>=3B From: lists@ruby-forum.com
>=3B Subject: n00b looking to le= arn from the best
>=3B To: ruby-talk@ruby-lang.org
>=3B
>= =3B Hey Rubyians!
>=3B
>=3B I am not used to joining online foru= m's and I am eager to join the Ruby
>=3B community. This is the first = language I am learning and have a practice
>=3B problem that I cannot = seem to get. Can you enlighten me? I'm looking
>=3B forward to learnin= g from all of you!
>=3B
>=3B Here is the question:
>=3B >=3B Write a method named lassy_sum(numbers) that takes an array of numb= ers.
>=3B lassy_sum should multiply each number in the array by its po= sition in
>=3B the array=2C and return the sum.
>=3B
>=3B I= don't quite understand how to multiply each number in the array by its
= >=3B position in the array. Any help will be greatly appreciated! Thanks.=
>=3B
>=3B Flex
>=3B
>=3B --
>=3B Posted via ht= tp://www.ruby-forum.com/.
>=3B
= --_78da1618-610d-4a07-bfd8-132982204306_--