Oddball_sum Help Request

112 views
Skip to first unread message

Dhaval Bhatt

unread,
Aug 3, 2015, 10:57:08 PM8/3/15
to rubyonra...@googlegroups.com
Hi,

I am new to Ruby on Rails. I am practicing problems to better
understand coding. I understand the logic but I am not able to put in
proper codes. Could someone please solve the following problem for me?

oddball_sum
Write a function oddball_sum(numbers), which takes in an array of
integers and returns the sum of all the odd elements.

oddball_sum(numbers)
i = 0
while i < numbers.length
if (numbers % 2 !== 0)
return numbers[i] += 1
i += 1
end
return result
end

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

Colin Law

unread,
Aug 4, 2015, 3:15:41 AM8/4/15
to Ruby on Rails: Talk
On 4 August 2015 at 03:56, Dhaval Bhatt <li...@ruby-forum.com> wrote:
> Hi,
>
> I am new to Ruby on Rails. I am practicing problems to better
> understand coding. I understand the logic but I am not able to put in
> proper codes. Could someone please solve the following problem for me?
>
> oddball_sum
> Write a function oddball_sum(numbers), which takes in an array of
> integers and returns the sum of all the odd elements.
>
> oddball_sum(numbers)

You need a def on the front of the above

> i = 0

You need a variable to hold the sum, so
result = 0

> while i < numbers.length
> if (numbers % 2 !== 0)

you meant numbers[i] % 2 != 0

> return numbers[i] += 1

Don't return here, add this number to the total
result += numbers[i]

> i += 1
> end
> return result
> end

But in fact it is better to use each, not a while loop, so something like

def oddball_sum(numbers)
result = 0
numbers.each { |n| result += n if n %2 != 0 }
return result
end

In fact there are even more concise ways of coding this, but I would
stick with something like the above for the moment in order to keep
the code readable.
I suggest you find some tutorials on Ruby first and work through them,
then when you think you have the basics of Ruby sorted move on to a
good Rails tutorial such as railstutorial.org, which will show you the
basics of Rails.

Colin

Elizabeth McGurty

unread,
Aug 4, 2015, 8:51:51 AM8/4/15
to Ruby on Rails: Talk
When I looked at your code sample, I immediately thought that I should look at the Ruby Integer, Emumerable  and Array API.  I reviewed them....
On review of the Ruby Integer API revealed the method .odd?
On review of the Ruby Array API revealed the methods .select
On review of the Ruby Enumerable API revealed the methods .reduce

So I built the solution, but from what I learned that are many possibilities, and admittedly this solutions may not be the best or the fastest

my_odd_ball_sum =  numbers.select {|n| n.odd?}.reduce(:+)

Breakdown...

numbers.select
select { |item| block } → new_ary
select → Enumerator

Returns a new array containing all elements of ary for which the given block returns a true value.


numbers.select {|n| n.odd?}  returns an Enumerator, so then apply reduce(:+)

reduce(sym) → obj

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.


My point is that it is a good idea/investment to explore Ruby API for solutions.
Hope this helps
Liz

Dhaval Bhatt

unread,
Aug 4, 2015, 9:01:10 PM8/4/15
to rubyonra...@googlegroups.com
Colin,

That's very helpful.

Sorry, I copy-pasted the codes I had from my word file so I missed the
"def."

Anyway,

Thank you for explaining. I have two more questions which I will ask in
another thread after I finish doing them on my own.

Do you have a personal e-mail I can reach you on? I would love to pick
your brain if you give me a chance.

Best,

DB



Colin Law wrote in post #1177221:

Dhaval Bhatt

unread,
Aug 4, 2015, 9:01:42 PM8/4/15
to rubyonra...@googlegroups.com
Thank you Liz :)

Walter Lee Davis

unread,
Aug 6, 2015, 6:03:25 PM8/6/15
to rubyonra...@googlegroups.com
You win all the points. That is slick!

Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/87da231f-6a96-4814-909d-5b39a47827df%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages