LoginSignup
3
3

More than 5 years have passed since last update.

1万字書けるツイッターに数字を順に並べる

Last updated at Posted at 2016-01-06

irb
# 1万文字書けるツイッターに連続した数字はいくつまで並ぶか
(2..Float::INFINITY).lazy.inject(1){ |l, n| nx = l + n.to_s.size + 1; break(n - 1) if nx > 10000; nx}

ツイッターに書き込もうとしてがんばって短くしたので変な感じになってしまいました。^^;

無限配列を2から始めて、初期値に1を与えるようにしたのががんばった所です。先頭の数字だったら空白はいらないけど2つ目の数字からは空白の分1を足して、、、っていう処理がいらなくなりました。

追記

あまりに分かりづらかったので書き直しました。メソッド名が長いのは私の英語力が無いせいです。^^;;;;;

twitter10000.rb
# coding: utf-8

def how_many_consecutive_numbers_can_write_within_byte_of(limit)
  (2..Float::INFINITY).lazy.inject(1) do |len, num|
    next_len = len + num.to_s.size + 1
    break(num - 1) if next_len > limit
    next_len
  end
end

# 1万文字書けるツイッターに連続した数字はいくつまで並ぶか
puts how_many_consecutive_numbers_can_write_within_byte_of(10000)
3
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3