Ruby、138132バイト
->x,y{d,*a=->{a.map.with_index{|n,i|n/BigDecimal.new(2)**(i+1)}.inject(:+)||0};[a+=[1],a[-1]=d[]>=y ?0:1]while d[]<x;a}
-rbigdecimal
フラグ用に13バイトが追加されました。入力が2つBigDecimal
のであることを期待しています。
サンプルの実行:
irb(main):029:0> f=->x,y{d,*a=->{a.map.with_index{|n,i|n/BigDecimal.new(2)**(i+1)}.inject(:+)||0};[a+=[1],a[-1]=d[]>=y ?0:1]while d[]<x;a}
=> #<Proc:0x00000001053a10@(irb):29 (lambda)>
irb(main):030:0> f[BigDecimal.new('0.98983459823945792125172638374187268447126843298479182647'),BigDecimal.new('0.98983459823945792125172638374187268447126843298479182648')]
=> [1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1]
説明:
->x,y{
d,*a= # sets d to the following, and a to [] with fancy splat operator
->{ # lambda...
a.map.with_index{|n,i| # map over a with indices
n/BigDecimal.new(2)**(i+1) # this will return:
# - 0 [ /2**(i+1) ] if n is 0
# - 1 /2**(i+1) if n is 1
}.inject(:+) # sum
||0 # inject returns nil on empty array; replace with 0
}; # this lambda turns `[0, 1, 1]' into `BigDecimal(0b0.011)'
[ # `[...]while x' is a fancy/golfy way to say `while x;...;end'
a+=[1], # add a digit 1 to the array
a[-1]=d[]>=y ?0:1 # replace it with 0 if we exceeded upper bound
]while d[]<x; # do this while(below the lower bound)
a # return the array of digits
}
(0.98983459823945792125172638374187268447126843298479182647, 0.98983459823945792125172638374187268447126843298479182648)
ですか、それとも、の入力をサポートする必要がありますか?また、テストケースも役立ちます。