我正在研究某个问题时,我发现扔掉所有内容的是我在while循环之外声明了一个变量。有问题的变量是sub_string = 1.似乎我需要在第一个while循环之后声明它,就在它实际在第二个while循环中发挥作用之前。通过早期声明它,循环以某种方式只能拾取从字符串[0]开始的回文,然而,可以是任何长度。对于我的生活,我不确定它为什么会以这种方式表现,因为我宣称切片的长度(而不是起始位置)。
如果我错过了一些非常基本的东西,请原谅我,因为我对编码很新。
这是有问题的代码(省略了回文的定义?检查方法)。
def longest_palindrome(string)
idx = 0
longest = nil
#substring_string = 1 ; declaring it early seems to cause weird issues to show up
while idx < string.length
substring_length = 1 #I have to declare the substring_length here
while (idx + substring_length) <= string.length
substring = string.slice(idx, substring_length)
if palindrome?(substring) && (longest == nil || substring.length > longest.length)
longest = substring
print substring_length
end
substring_length += 1
end
idx += 1
end
return longest
end
答案 0 :(得分:0)
场景1:while循环中定义的 substring_length
def longest_palindrome(string)
idx = 0
longest = nil
while idx < string.length
substring_length = 1
while (idx + substring_length) <= string.length
# palindrome
substring_length += 1
end
idx += 1
end
longest
end
while
循环。 while idx < string.length
substring_length
至1
while (idx + substring_length) <= string.length
substring_length
递增1 。此代码将一直运行,直到其条件返回false (idx + substring_length) < string.length
。这意味着,一旦这个内部循环结束,substring_length
总是将成为大于1的值 while
循环。现在代码的第一行重新分配 substring_length
到1
。实质上,无论内部循环结束时substring_length
的值是什么,一旦控制流回到外部循环,当您将substring_length
设置为 1 场景2: substring_length在while循环中定义
def longest_palindrome(string)
idx = 0
longest = nil
substring_length = 1
while idx < string.length
while (idx + substring_length) <= string.length
# palindrome
substring_length += 1
end
idx += 1
end
longest
end
substring_length
初始化为1 ,然后执行控制流移至第一个while
循环。 while idx < string.length
inner
循环substring_length
增加1 。此代码将一直运行,直到其条件返回false (idx + substring_length) < string.length
。就像在方案1 中一样,这意味着一旦这个内部循环结束,substring_length
始终将是大于1的值强> while
环。这次没有初始化语句,因此内部循环结束时substring_length
的值是控制返回外部循环时使用的相同值。让我们尝试使用一个例子
情景1
longest_palindrome('him')
idx = 0; substring_length = 1; string.length = 3
while (0 < 3) # the outer loop condition is true,
substring_length = 1
# while (idx + substring_length) < string.length
while (0 + 1) < 3 # inner loop condition is true
# run code block, increment substring_length by 1, substring_length is NOW 2
while (0 + 2) < 3 # inner loop condition is still true
# run code block, increment substring_length by 1, substring_length is NOW 3
while (0 + 3) < 3 # inner loop condition fails, substring_length = 3
# Go to line after end of inner loop
increase idx by 1 # idx = 1
# Go back to outer loop
set substring_length to 1 # substring_length is NO MORE 3
# while (idx + substring_length) < string.length
while (1 + 1) < 3 # run code block, increment substring_length by 1, substring_length is NOW 2
while (1 + 2) < 3 # condition fails
..... and so on
场景2
longest_palindrome('him')
idx = 0; substring_length = 1; string.length = 3
while (0 < 3) # the outer loop condition is true,
# while (idx + substring_length) < string.length
while (0 + 1) < 3 # inner loop condition is true
# run code block, increment substring_length by 1, substring_length is NOW 2
while (0 + 2) < 3 # inner loop condition is still true
# run code block, increment substring_length by 1, substring_length is NOW 3
while (0 + 3) < 3 # inner loop condition fails, substring_length = 3
# Go to line after end of inner loop
increase idx by 1 # idx = 1
# Go back to outer loop. Remember substring_length =3
# while (idx + substring_length) < string.length
while (1 + 3) < 3 # condition is false. Inner loop is NOT EXECUTED
# Go to line after end of inner loop
increase idx by 1 # idx = 1
# Go back to outer loop while (idx < string.length)
while (1 < 3) # outer loop condition is true
# Go to inner loop while (idx + substring_length) < string.length
while (1 + 3) < 3 # Once again condition is false and inner loop is NOT EXECUTED
# Go to line after end of inner loop
increase idx by 1 # idx = 1
正如您在此场景中所看到的,内部循环停止执行,因为substring_length
未被重新分配为1并且条件不断失败。