Skip to content

Instantly share code, notes, and snippets.

@sanemat
Created January 7, 2010 08:37
Show Gist options
  • Save sanemat/271088 to your computer and use it in GitHub Desktop.
Save sanemat/271088 to your computer and use it in GitHub Desktop.
(サンプル問題 問題No:201)
■Q1.桁数の不明な整数xの各位の値を合計したものを返す関数を作りなさい。(例:x=123 の場合、1 + 2 + 3 = 6 となる)
われながらひどいこたえ
irb(main):012:0> def plus_each_integer(int)
irb(main):013:1> answer = 0
irb(main):014:1> int.to_s.split(//).each{|byte| answer += byte.to_i}
irb(main):015:1> answer
irb(main):016:1> end
=> nil
irb(main):017:0> puts plus_each_integer(123)
6
=> nil
irb(main):018:0> puts plus_each_integer(1234)
10
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment