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_Recursion - Fatal编程技术网

为什么这个ruby方法不在返回时终止?

为什么这个ruby方法不在返回时终止?,ruby,recursion,Ruby,Recursion,新手红宝石问题 代码挑战:字符串折叠:删除给定字符串中所有相邻的重复项。注意:删除相邻副本会创建新的相邻副本,您也必须删除这些副本 我很确定我用简单的递归解决了这个问题,但当我在调试器中一步一步地处理这个问题时,代码不会在到达返回行时终止,并开始向字符串添加字母,然后删除字母,直到最后终止 比如说 zzzxaaxy => zxaaxy => zxxy => zy => zxxy => zy => zxxy =>

新手红宝石问题

代码挑战:字符串折叠:删除给定字符串中所有相邻的重复项。注意:删除相邻副本会创建新的相邻副本,您也必须删除这些副本

我很确定我用简单的递归解决了这个问题,但当我在调试器中一步一步地处理这个问题时,代码不会在到达返回行时终止,并开始向字符串添加字母,然后删除字母,直到最后终止

比如说

         zzzxaaxy => zxaaxy => zxxy => zy => zxxy => zy => zxxy => 
                                       ^ code hits return line here and should stop but instead continues 

return关键字并不意味着“立即终止并返回我的值”,而是意味着“返回到调用我的方法”

因此,当您调用方法并返回“zxxy”而不是“zy”时,这是因为它只是在第一次调用相同的字符时返回ar.join()的结果

def same_char_collapse(str)
    ar = str.split('')
    ar.map.each_with_index do |char1, idx|
        char2 = ar[idx+1]
        if char1 == char2
            ar.delete_at(idx)
            ar.delete_at(idx)
# same_char_collapse(ar.join('')) doesn't change the value we are returning.
# you could delete this line and the method would still return the same thing 'zxxy'
            same_char_collapse(ar.join(''))
        end
    end
# ar's value is zxxy from the loop since the same_char_collapse(ar.join('')) line
# doesn't change ar. only the ar.delete_at(idx) lines modified ar.
    return ar.join('') 
end

same_char_collapse('zzzxaaxy') # first call to same_char_collapse

本质上,如果您想修复解决方案,您应该确保使用递归调用中生成的值。

return关键字并不意味着“立即终止并返回我的值”,而是意味着“返回到调用我的方法”

因此,当您调用方法并返回“zxxy”而不是“zy”时,这是因为它只是在第一次调用相同的字符时返回ar.join()的结果

def same_char_collapse(str)
    ar = str.split('')
    ar.map.each_with_index do |char1, idx|
        char2 = ar[idx+1]
        if char1 == char2
            ar.delete_at(idx)
            ar.delete_at(idx)
# same_char_collapse(ar.join('')) doesn't change the value we are returning.
# you could delete this line and the method would still return the same thing 'zxxy'
            same_char_collapse(ar.join(''))
        end
    end
# ar's value is zxxy from the loop since the same_char_collapse(ar.join('')) line
# doesn't change ar. only the ar.delete_at(idx) lines modified ar.
    return ar.join('') 
end

same_char_collapse('zzzxaaxy') # first call to same_char_collapse

基本上,如果您想修复您的解决方案,您应该确保使用递归调用中产生的值。

谢谢您的帮助,经过大量的尝试和错误,我终于成功了 想出解决办法

def dupes?(word) 
    word.each_char.with_index do |char, idx|
        if char == word[idx+1]
            return true
        end
    end
    return false
end

def removeDupe(word)
    ar = word.split('')
    ar.map.with_index do |char,idx|
        if char == ar[idx+1]
            ar.delete_at(idx)
            ar.delete_at(idx)
        end
    end
    return ar.join('')
end

def same_char_collapse(str)
    if dupes?(str)
        same_char_collapse(removeDupe(str))
    else
        return str
    end
end

谢谢你的帮助,经过反复尝试,我终于成功了 想出解决办法

def dupes?(word) 
    word.each_char.with_index do |char, idx|
        if char == word[idx+1]
            return true
        end
    end
    return false
end

def removeDupe(word)
    ar = word.split('')
    ar.map.with_index do |char,idx|
        if char == ar[idx+1]
            ar.delete_at(idx)
            ar.delete_at(idx)
        end
    end
    return ar.join('')
end

def same_char_collapse(str)
    if dupes?(str)
        same_char_collapse(removeDupe(str))
    else
        return str
    end
end

您的递归步骤将
ar.join(“”)
(即一个全新的字符串)作为参数,并忽略
same\u char\u collapse
返回的内容,因此您的递归实际上不会执行任何操作。您可能希望使用
select
来过滤大量
delete\u at
调用,而不是使用
select
。递归通常最好通过一个著名的例子(如fib)和一支铅笔和一张纸来理解:找出它。递归步骤需要
ar.join(“”)
(即一个全新的字符串)作为一个参数,忽略
相同的字符\u collapse
返回的内容,因此您的递归实际上不会执行任何操作。您可能希望在
调用中使用
select
进行筛选,而不是大量的
delete\u。简而言之,是的,但只是当前调用。递归通常最好用一个著名的例子(比如fib)和一支铅笔和一张纸来理解:找出它。