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....
-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)
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
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!
-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 ).
Yes, you are right that with slightly longer code, we can deal with
larger number.
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:
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!
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:
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"