私はRubyでカラツバ乗算を実装しようとしていたに方法は...ルビー
# takes two integer x and y and partition them to x=a+b and y=c+d
# example if x = 1234 a=12 and b=34
# recursively compute a*c,a*d,b*c and b*d
def mult (x,y)
if len(x) == 1 && len(y) == 1
return x*y
elsif len(x) > 1 && len(y) > 1
ab = partition(x)
cd = partition(y)
return ab.product(cd).each{ |num| mult(num[0],num[1]) }
end
end
#method for partitioning works fine..
def partition(number)
number.divmod(10**(len(number)/2))
end
#method to find size of integer works fine...
def len(value)
value.to_s.split("").compact.size
end
を期待値を返さないので、代わりにreturn x*y
の
p mult(12,34) should be 3,4,6,8
but is [[1, 3], [1, 4], [2, 3], [2, 4]]
の期待リターン、私はprint "#{x*y}"
を使用line no:3
それは3,4,6,8を印刷します。 mult
メソッドがnil
の返信理由を理解できません。x*y
です。
'len(x)'?それはPython – Ursus
@Ursusのように聞こえます。自己実装されたメソッドのように聞こえるので、スニペットの最後の3行をチェックしてください。 – mudasobwa