Hey Rubyians!
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!
Here is the question:
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, and return the sum.
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.
Flex
Hi Flex,
Welcome to Ruby, I’m sure you’ll have fun!
Why don’t you show us how far you have gotten, the beauty of learning is
doing 
try this:
- create an array of numbers.
- iterate over every item in the array, print the value or each item
and print the position in the array (hint: index)
I’d skip the mapping and just use inject…
def lassy_sum( numbers )
numbers.each.with_index.inject(0) do |sum, (num, idx)|
sum + num * idx
end
end
Depending on what you mean by the position in the array, you might want
“num * ( idx + 1 )”, otherwise you’ll always get 0 for the first
Hi you can use the #each_with_index method to iterate through the array
and also get the position number of each element, then call #map to
build an array with the result of each iteration and finally the #inject
method combines all the elements in the array with the + method.
def lassy_sum(numbers) # if numbers is [1,2,3] result =
numbers.each_with_index.map { |n, i| n*i } # this will return [0,2,6]
result.inject(:+) # this will return 8end
By the way, 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, just do the following:
- Open your terminal 2. Type: irb3. Press Enter4. Paste the code that
i have proposed5. Type: lassy_sum [1,2,3]6. It should return: 8
Javier Hidalgo V.
Lder de Desarrollo
Here is a hint.
You may want to change some of the variable names.
def try_this(shoe)
Array.new(shoe.size){|horn| shoe[horn]*horn}.inject{|at,heel| at+heel}
end
foot = [1,2,3,4,5]
p try_this(foot) #> 40
Harry
David W. wrote in post #1109127:
Hi Flex,
Welcome to Ruby, I’m sure you’ll have fun!
Why don’t you show us how far you have gotten, the beauty of learning is
doing 
try this:
- create an array of numbers.
- iterate over every item in the array, print the value or each item
and print the position in the array (hint: index)
Class: Array (Ruby 1.9.3)
Hi David!
I’ve got to say, I love your teaching style
I’ll run some code and if
I can’t figure it out still, I’ll show you at least what I’ve got.
Thanks for your help!
Flex
Jonan S. wrote in post #1109126:
Look up each_with_index.
Thanks Jonah! I didn’t even know that “each_with_index” method
existed… I will look into it further.I appreciate your help!
Try this or that.
def try_this(n)
Array.new(n.size){|i| n[i]*i}.inject{|a,b| a+b}
end
def try_that(n)
t = 0
(0…n.size).each{|x| t+=n[x]*x}
t
end
n = [1,2,3,4,5]
p try_this(n)
p try_that(n)
Harry