4 Replies - 2742 Views - Last Post: 17 March 2011 - 06:59 PM Rate Topic: -----

#1 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 911
  • View blog
  • Posts: 3,180
  • Joined: 12-May 09

Ruby Code Golf 2

Post icon  Posted 16 March 2011 - 06:18 AM

I imagine this one will be trivial for most of you, but it will still be fun to do. I'm pulling this one from a problem in Project Euler because the same idea is used in a few problems.

The challenge is to sum the digits of an integer-style number in the fewest characters possible. Your function should take a number(assuming it is a string or an int class is allowed, just assume the argument is of the type you need) and return the sum.

For those of you who know about passing methods/procs/blocks, try allowing the function to perform any accumulative algorithm across the digits - i.e. any algorithm that keeps a running value and uses the next value to alter it. Examples would be multiplying all the digits or raising each digit to a power and then raising that value to the next digit's power and so on.

This post has been edited by xclite: 16 March 2011 - 06:19 AM


Is This A Good Question/Topic? 2
  • +

Replies To: Ruby Code Golf 2

#2 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Ruby Code Golf 2

Posted 16 March 2011 - 10:12 AM

Two entries, one with strings, one with numbas.

Spoiler


Spoiler

This post has been edited by T3hC13h: 16 March 2011 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Ruby Code Golf 2

Posted 17 March 2011 - 02:26 PM

this is in C++, ill post some ruby later
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Ruby Code Golf 2

Posted 17 March 2011 - 05:49 PM

A different approach, slightly shorter

eval v.gsub /./,'+\0'


See what I did there, I add plus signs in between the characters of the string and then ask ruby to evaluate it as ruby code.


and the shortest recursive solution I can think of, because recursion is fun:

f=lambda{|x|x==""?0:f.(x[1..-1])+x[0].to_i}
f.(v)



still sort of bothers me that I can't think of everything shorter than the c example

This post has been edited by Karel-Lodewijk: 17 March 2011 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Ruby Code Golf 2

Posted 17 March 2011 - 06:59 PM

Two solutions from me. One using numbers and recursion and the other using strings and no recursion. Not as short as some of the others, but I managed to do both on one line each, no ';' required.

Spoiler


Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1