Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法未在Ruby中返回预期值_Ruby_Algorithm - Fatal编程技术网

方法未在Ruby中返回预期值

方法未在Ruby中返回预期值,ruby,algorithm,Ruby,Algorithm,我试图用Ruby实现Karatsuba乘法 # 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

我试图用Ruby实现Karatsuba乘法

# 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
那么

 p mult(12,34) should be 3,4,6,8
 but is [[1, 3], [1, 4], [2, 3], [2, 4]]

当我在
行号3
中使用
打印“#{x*y}”时,它会打印3,4,6,8,而不是
返回x*y
。我无法理解为什么
mult
方法为
x*y
返回
nil

问题在于迭代器错误:

#              ⇓⇓⇓⇓    
ab.product(cd).each{ |num| mult(num[0],num[1]) }
您想要的是:

旁注:您也不需要显式调用
return

def mult (x,y)
  if len(x) == 1 && len(y) == 1
    x*y
  elsif len(x) > 1 && len(y) > 1
    ab = partition(x)
    cd = partition(y)
    ab.product(cd).map { |num| mult(num[0], num[1]) }
  else
    raise "We got a problem"
  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.size
end

p mult 12, 34
#⇒ [3,4,6,8]

问题是迭代器错误:

#              ⇓⇓⇓⇓    
ab.product(cd).each{ |num| mult(num[0],num[1]) }
您想要的是:

旁注:您也不需要显式调用
return

def mult (x,y)
  if len(x) == 1 && len(y) == 1
    x*y
  elsif len(x) > 1 && len(y) > 1
    ab = partition(x)
    cd = partition(y)
    ab.product(cd).map { |num| mult(num[0], num[1]) }
  else
    raise "We got a problem"
  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.size
end

p mult 12, 34
#⇒ [3,4,6,8]

len(x)
?听起来像Python@Ursus这听起来像是自我实现的方法,检查代码片段的最后3行。
len(x)
?听起来像Python@Ursus这听起来像是自我实现的方法,检查代码段的最后3行。感谢它的工作,我应该知道它何时打印出
[[1,3],[1,4],[2,3],[2,4]
并停止,但是当我必须迭代每个值并将其传递给
mult(x,y)时,
为什么每个
都不工作呢
方法。因为
每个
都返回iterable本身,而
映射
实际上将iterable转换为从
mult
方法返回的新值。感谢它的工作,我应该知道它何时打印
[[1,3],[1,4],[2,3],[2,4]]
和stopped但是当我必须迭代每个值并将其传递给
mult(x,y)
方法时,
每个
都会返回iterable本身,而
映射
实际上会将iterable转换为从
mult
方法返回的新值。