Stupid question

Hi everyone.

I have a really stupid question here, but i'm a newbie so hear me out.

I want to add together all the digets in a number.
(for example, 2011 would be 4).
but i want to do it with a program.

I don't know how to split the individual digits up.

Thank you so much.

James. :slight_smile:

ยทยทยท

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

I'm sure someone will come up with a better solution, but a quick and dirty
way would be to convert it to a string then split it:

2011.to_s.split(//).inject(0){|sum, a| sum + a.to_i}

Like I say, not the cleanest, but it works. Hope that helps.

ยทยทยท

On 30 November 2011 14:10, James Gallagher <lollyproductions@mac.com> wrote:

Hi everyone.

I have a really stupid question here, but i'm a newbie so hear me out.

I want to add together all the digets in a number.
(for example, 2011 would be 4).
but i want to do it with a program.

I don't know how to split the individual digits up.

Thank you so much.

James. :slight_smile:

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

How about this one?

n = 2011
n.size.pred.downto(0).reduce(0) { |sum, a| sum += (n.abs / 10 ** a) % 10 }

ยทยทยท

On Nov 30, 2011, at 3:10 PM, James Gallagher wrote:

Hi everyone.

I have a really stupid question here, but i'm a newbie so hear me out.

I want to add together all the digets in a number.
(for example, 2011 would be 4).
but i want to do it with a program.

I don't know how to split the individual digits up.

Thank you so much.

James. :slight_smile:

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

If you split on an empty delimiter, you get the characters
individually. E.g., "12345".split('') gives you ["1", "2", "3", "4",
"5"].

Let us know how you do with the "get the sum of the digits" problem.
It's a good opportunity for you to learn some interesting concepts in
"functional programming" and some of the bizarrer-looking magic of
Ruby. See how short you can make the solution.... :slight_smile:

-Dave

ยทยทยท

On Wed, Nov 30, 2011 at 09:10, James Gallagher <lollyproductions@mac.com> wrote:

I don't know how to split the individual digits up.

--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

If the number of digits is less than five, how about this one for Ruby
Golf :wink:

ย ย ย ย "2011".sum(48)%48

Regards,

Bill

ยทยทยท

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

Thank you everyone for Helping me out. :slight_smile:

ยทยทยท

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

Mr Jaba wrote in post #1034419:

I'm sure someone will come up with a better solution, but a quick and
dirty
way would be to convert it to a string then split it:

2011.to_s.split(//).inject(0){|sum, a| sum + a.to_i}

Like I say, not the cleanest, but it works. Hope that helps.

Thank you.

That really helps. :slight_smile:

ยทยทยท

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

You can do it with #chars, too.

"2011".chars.inject(0) { |a, b| a + b.to_i }

ยทยทยท

On Wed, Nov 30, 2011 at 19:23, Dave Aronson <rubytalk2dave@davearonson.com>wrote:

If you split on an empty delimiter, you get the characters
individually. E.g., "12345".split('') gives you ["1", "2", "3", "4",
"5"].

Clever!

Is the parameter to sum even required in this case? The docs say the
parameter n merely limits the sum modulo 2**n-1 which defaults to 16,
or modulo 65535. And the number of digits could be more than five I'd
think, as the limiting factor is that modulo operation and so long as
the actual sum of each character does NOT cross that threshold (65535
in the default parameter case), it should be fine, no? (And using
ASCII values 48-57 inclusive, we should be safe at least up to strings
of 1149 digits in length.)

Thanks for teaching me yet another interesting tidbit of Ruby--I'd
never known about String#sum before.

Aaron out.

ยทยทยท

On Wed, Nov 30, 2011 at 1:42 PM, Admin Tensor <admin@tensor.heliohost.org> wrote:

If the number of digits is less than five, how about this one for Ruby
Golf :wink:

"2011".sum(48)%48

Regards,

Bill

Thank you everyone for Helping me out. :slight_smile:

If I am not mistaken, everybody in this thread resorted to using a
string conversion. Why does nobody want to calculate this
numerically?

x = 2011

=> 2011

q = 0

=> 0

a = x

=> 2011

while a>0;a,b=a.divmod 10;q+=b;end

=> nil

q

=> 4

Granted, this is not too short - but it works without creating a
String instance.

Kind regards

robert

ยทยทยท

On Mon, Dec 5, 2011 at 10:03 AM, James Gallagher <lollyproductions@mac.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

You can do it with #chars, too.

Oh yeah! Good catch! That knocks four chars (no pun intended) off my solution.

"2011".chars.inject(0) { |a, b| a + b.to_i }

Yup, inject was part of what I was thinking. The other half is map.
Result, using chars:

  "2011".chars.map(&:to_i).inject(&:+)

Can anyone make that even shorter? Let's play Ruby Golf! :wink:

-Dave

ยทยทยท

On Wed, Nov 30, 2011 at 14:26, Adam Prescott <adam@aprescott.com> wrote:

--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

Is the parameter to sum even required in this case? The docs say the
parameter n merely limits the sum modulo 2**n-1 which defaults to 16,
or modulo 65535. And the number of digits could be more than five I'd
think, as the limiting factor is that modulo operation and so long as
the actual sum of each character does NOT cross that threshold (65535
in the default parameter case), it should be fine, no? (And using
ASCII values 48-57 inclusive, we should be safe at least up to strings
of 1149 digits in length.)

First of all, to be more exact, I have to say in my previous post "if
the number of digits is less than or equal to five."

I am assuming that we want the shortest code (for the Ruby Golf :wink: ).
Yes, you are right that with slightly longer code, we can deal with
larger number.

Regards,

Bill

ยทยทยท

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

Thank you everyone for Helping me out. :slight_smile:

If I am not mistaken, everybody in this thread resorted to using a
string conversion. Why does nobody want to calculate this
numerically?

I posted a similar version to yours which uses an iterator.

n = 2011
n.size.pred.downto(0).reduce(0) { |sum, a| sum += (n.abs / 10 ** a) % 10 }

The while loop is more straightforward though. Perhaps we could combine the two?

ยทยทยท

On Dec 5, 2011, at 11:38 AM, Robert Klemme wrote:

On Mon, Dec 5, 2011 at 10:03 AM, James Gallagher > <lollyproductions@mac.com> wrote:

x = 2011

=> 2011

q = 0

=> 0

a = x

=> 2011

while a>0;a,b=a.divmod 10;q+=b;end

=> nil

q

=> 4

Granted, this is not too short - but it works without creating a
String instance.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

...

Yup, inject was part of what I was thinking. The other half is map.
Result, using chars:

"2011".chars.map(&:to_i).inject(&:+)

Can anyone make that even shorter? Let's play Ruby Golf! :wink:

-Dave

If we're golfing, here's my amateur golf attempt: *chuckle*

s=0;'2011'.chars{|x|s+=x.to_i};s

(Are we allowing semicolons?)

Aaron out.

ยทยทยท

On Wed, Nov 30, 2011 at 1:03 PM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:

4

:slight_smile:

ยทยทยท

On Wed, Nov 30, 2011 at 20:03, Dave Aronson <rubytalk2dave@davearonson.com>wrote:

"2011".chars.map(&:to_i).inject(&:+)

Can anyone make that even shorter? Let's play Ruby Golf! :wink:

More importantly, it's much clearer.

ยทยทยท

On Wed, Nov 30, 2011 at 2:03 PM, Dave Aronson <rubytalk2dave@davearonson.com > wrote:

On Wed, Nov 30, 2011 at 14:26, Adam Prescott <adam@aprescott.com> wrote:

> You can do it with #chars, too.

Oh yeah! Good catch! That knocks four chars (no pun intended) off my
solution.

Can anyone make that even shorter? Let's play Ruby Golf! :wink:

can we cheat? :slight_smile:

2011.to_s.chars.sum{|d| d.to_i}

=> 4

ยทยทยท

On Thu, Dec 1, 2011 at 4:03 AM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:

Thank you everyone for Helping me out. :slight_smile:

If I am not mistaken, everybody in this thread resorted to using a
string conversion. Why does nobody want to calculate this
numerically?

I posted a similar version to yours which uses an iterator.

Oh, I'm sorry I overlooked that one.

n = 2011
n.size.pred.downto(0).reduce(0) { |sum, a| sum += (n.abs / 10 ** a) % 10 }

The while loop is more straightforward though. Perhaps we could combine the two?

I'm afraid your solution with Fixnum#size works only accidentally
because it returns the byte size of the number which coindicentally is
also the number of digits. It fails for other values:

n = 90000

=> 90000

n.size.pred.downto(0).reduce(0) { |sum, a| sum += (n.abs / 10 ** a) % 10 }

=> 0

n.size

=> 4

Kind regards

robert

ยทยทยท

On Mon, Dec 5, 2011 at 12:17 PM, Sylvester Keil <sylvester.keil@gmail.com> wrote:

On Dec 5, 2011, at 11:38 AM, Robert Klemme wrote:

On Mon, Dec 5, 2011 at 10:03 AM, James Gallagher >> <lollyproductions@mac.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Aren't you supposed to shout it, like "4!"? (And would some smart@$$
then say 24, because that's mathematically equal?)

-Dave

ยทยทยท

On Wed, Nov 30, 2011 at 15:19, Adam Prescott <adam@aprescott.com> wrote:

On Wed, Nov 30, 2011 at 20:03, Dave Aronson > <rubytalk2dave@davearonson.com>wrote:

Can anyone make that even shorter? Let's play Ruby Golf! :wink:

4

--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

I Admin Tensor's solution modified to support digit strings up to 1149
characters long (which is the path my muddled brain was meandering
down in my prior post):

irb(main):001:0> s="2011";s.sum%(48*s.size)
=> 4

And to verify worst-case a string of 1149 nine digits:

irb(main):002:0> s="9"*1149;s.sum%(48*s.size)
=> 10341

For longer strings, pass an argument to String#sum bigger than the default 16.

Of course using non-arabic UTF-8 encoded digits makes things very
intersting (a.k.a. fail), a la Devanagari digit six (the first
non-arabic UTF code point for a digit my search turned up):
0x096c.chr(Encoding::UTF_8) a.k.a. "\u096C"

Aaron out.