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:

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.

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:

On Wed, Nov 30, 2011 at 09:10, James Gallagher
[email protected] wrote:

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

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 19:23, Dave A.
[email protected]wrote:

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

You can do it with #chars, too.

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

On Wed, Nov 30, 2011 at 14:26, Adam P. [email protected] wrote:

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 1:03 PM, Dave A.
[email protected] wrote:

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.

How about this one?

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

On Wed, Nov 30, 2011 at 15:19, Adam P. [email protected] wrote:

On Wed, Nov 30, 2011 at 20:03, Dave A.
[email protected]wrote:

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

4

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

-Dave

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

"2011".sum(48)%48

Regards,

Bill

On Wed, Nov 30, 2011 at 20:03, Dave A.
[email protected]wrote:

“2011”.chars.map(&:to_i).inject(&:+)

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

4

:slight_smile:

On Wed, Nov 30, 2011 at 1:42 PM, Admin T.
[email protected] wrote:

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

“2011”.sum(48)%48

Regards,

Bill

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 2:03 PM, Dave A.
<[email protected]

wrote:

On Wed, Nov 30, 2011 at 14:26, Adam P. [email protected] wrote:

You can do it with #chars, too.

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

More importantly, it’s much clearer.

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

I Admin T.'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%(48s.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.

On Thu, Dec 1, 2011 at 4:03 AM, Dave A.
[email protected] wrote:

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

Thank you everyone for Helping me out. :slight_smile:

On Mon, Dec 5, 2011 at 10:03 AM, James Gallagher
[email protected] 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?

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 Dec 5, 2011, at 11:38 AM, Robert K. wrote:

On Mon, Dec 5, 2011 at 10:03 AM, James Gallagher
[email protected] 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.

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?

Aaron D. Gifford wrote in post #1034519:

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%(48s.size)
=> 10341

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

Very good, Aaron. Now, is there any way to replace that “s.size” with
something to make it really a one-liner (without the semicolon):
“some number”.sum%(48 times something)
?

Regards,

Bill