&引用;对于给定字符串,返回每个字母后面的下一个字符';r'&引用;。ruby kickstart教程挑战(1:7)
此练习有以下解决方案:&引用;对于给定字符串,返回每个字母后面的下一个字符';r'&引用;。ruby kickstart教程挑战(1:7),ruby,string,loops,Ruby,String,Loops,此练习有以下解决方案: def pirates_say_arrrrrrrrr(string) to_return = "" add_next = false string.size.times do |index| current_char = string[index] to_return << current_char if add_next add_next = (current_char == "r"
def pirates_say_arrrrrrrrr(string)
to_return = ""
add_next = false
string.size.times do |index|
current_char = string[index]
to_return << current_char if add_next
add_next = (current_char == "r" || current_char == "R")
end
to_return
end
def pirates_say_arrrrrr(字符串)
to_return=“”
add_next=false
string.size.times do |索引|
当前字符=字符串[索引]
要返回以了解程序正在执行的操作,您可以从一个具体的示例开始(例如海盗们说
),然后逐行运行代码。您可能需要一些纸来记录每个变量的中间结果。在开始学习编程时,我发现这很有帮助
务必确保在浏览示例时了解每一行正在执行的操作。原因如下:
>>>>>>>>>>>>>> index = 0
current_char = r
to_return = , add_next = false
add_next = true
>>>>>>>>>>>>>> index = 1
current_char = a
to_return = a, add_next = true
add_next = false
>>>>>>>>>>>>>> index = 2
current_char = r
to_return = a, add_next = false
add_next = true
>>>>>>>>>>>>>> index = 3
current_char = b
to_return = ab, add_next = true
add_next = false
>>>>>>>>>>>>>> index = 4
current_char = r
to_return = ab, add_next = false
add_next = true
>>>>>>>>>>>>>> index = 5
current_char = c
to_return = abc, add_next = true
add_next = false
>>>>>>>>>>>>>> index = 6
current_char = r
to_return = abc, add_next = false
add_next = true
>>>>>>>>>>>>>> index = 7
current_char = d
to_return = abcd, add_next = true
add_next = false
abcd
这条线在干什么
它对以下代码有什么影响
此行之后更改了哪些数据
开始的时候会很难,迟早你会发现随着你练习的越来越多,这很简单
您可以打印每个步骤中涉及的每个变量的中间结果,以便验证您脑海中得到的结果
def rates_say_arrrrrrrrr(string)
to_return = ""
add_next = false
string.size.times do |index|
puts ">>>>>>>>>>>>>> index = #{index}"
current_char = string[index]
puts "current_char = #{current_char}"
to_return << current_char if add_next
puts "to_return = #{to_return}, add_next = #{add_next}"
add_next = (current_char == "r" || current_char == "R")
puts "add_next = #{add_next}"
end
to_return
end
result = rates_say_arrrrrrrrr("rarbrcrd")
puts result
为每次迭代做一些标记,以便看到边界(>>>>>>>>>>>>
)
这是一些原始的调试技术。稍后,当您熟悉该语言及其库时,可以使用一些调试工具,如byebug、pry debug等
要回答您的问题:
要返回要获取程序正在执行的操作,您可以从一个具体的示例开始(例如海盗说“arrrrrr”(“rarbrcrd”)
),然后逐行运行代码。您可能需要一些纸来记录每个变量的中间结果。在开始学习编程时,我发现这很有帮助
务必确保在浏览示例时了解每一行正在执行的操作。原因如下:
>>>>>>>>>>>>>> index = 0
current_char = r
to_return = , add_next = false
add_next = true
>>>>>>>>>>>>>> index = 1
current_char = a
to_return = a, add_next = true
add_next = false
>>>>>>>>>>>>>> index = 2
current_char = r
to_return = a, add_next = false
add_next = true
>>>>>>>>>>>>>> index = 3
current_char = b
to_return = ab, add_next = true
add_next = false
>>>>>>>>>>>>>> index = 4
current_char = r
to_return = ab, add_next = false
add_next = true
>>>>>>>>>>>>>> index = 5
current_char = c
to_return = abc, add_next = true
add_next = false
>>>>>>>>>>>>>> index = 6
current_char = r
to_return = abc, add_next = false
add_next = true
>>>>>>>>>>>>>> index = 7
current_char = d
to_return = abcd, add_next = true
add_next = false
abcd
这条线在干什么
它对以下代码有什么影响
此行之后更改了哪些数据
开始的时候会很难,迟早你会发现随着你练习的越来越多,这很简单
您可以打印每个步骤中涉及的每个变量的中间结果,以便验证您脑海中得到的结果
def rates_say_arrrrrrrrr(string)
to_return = ""
add_next = false
string.size.times do |index|
puts ">>>>>>>>>>>>>> index = #{index}"
current_char = string[index]
puts "current_char = #{current_char}"
to_return << current_char if add_next
puts "to_return = #{to_return}, add_next = #{add_next}"
add_next = (current_char == "r" || current_char == "R")
puts "add_next = #{add_next}"
end
to_return
end
result = rates_say_arrrrrrrrr("rarbrcrd")
puts result
为每次迭代做一些标记,以便看到边界(>>>>>>>>>>>>
)
这是一些原始的调试技术。稍后,当您熟悉该语言及其库时,可以使用一些调试工具,如byebug、pry debug等
要回答您的问题:
to_return理解像这样的代码和许多其他代码的方法是假装你是计算机。写下变量名及其值,然后像计算机一样检查每一行代码,做计算机会做的事情。通常铅笔、纸或白板是最容易的,但由于我们不在同一个房间,我们必须把它打印出来
如果我们用
参数“稀有”
:
string=“罕见”
to_return=“”
add_next=false
我们将add_next
设置为false
,因为我们永远不会添加第一个
字符(在本例中为“R”
)到输出(因为第一个
字符从不跟在“r”
-它从不跟在任何东西后面!)
现在我们进入循环。为了理解代码,我们将手动
“展开”循环,也就是说,因为循环对我们的
输入,我们将在循环中写下代码四次。自从
索引的值
随着每次迭代而递增,而不是
string[index]
我已经在每次之后写了string[0]
,string[1]
,等等
我写的这句话,在一篇评论中,又是同一句话,但带有
变量替换为它们的值
current_char=string[0]
#=>当前字符=“R”
to_return to_return(什么也不做;to_return仍然是“”)
添加下一个字符=(当前字符=“r”| |当前字符=“r”)
#=>添加下一步=(“R”==“R”| |“R”==“R”)
#=>添加下一步=(假| |真)
#=>添加_next=true
我们的第一次迭代已经完成。我们没有向添加任何内容以返回,
但是由于这个字符是“R”
,所以我们在
在下一次迭代中,我们将知道向添加一个字符以返回
现在,第二次迭代:
current_char=string[1]
#=>当前字符=“a”
to_return to_return(to_return现在是“a”)
添加下一个字符=(当前字符=“r”| |当前字符=“r”)
#=>添加下一步=(“a”==“r”| |“a”==“r”)
#=>添加下一步=(假| |假)
#=>添加下一步=错误
在第二次迭代中,我们将“a”
添加到以返回
,因为
在第一次迭代中将add_next
设置为true
。然后我们开始
add_next
至false
,因为此字符不是“r”
现在,第三次迭代
current_char=string[2]
#=>当前字符=“r”
to_return to_return(什么都不做;to_return仍然是“a”)
添加下一个字符=(当前字符=“r”| |当前字符=“r”)
#=>添加下一步=(“r”==“r”| |“r”==“r”)
#=>添加下一步=(真| |假)
#=>添加_next=true
在第三次迭代中,我们没有向返回添加任何内容,因为
我们在第二次迭代中将add_next
设置为false
。然后我们开始
add_next
到true
,因为当前的字符是“r”
,所以在下一个
迭代我们将知道在中添加一个字符以返回
现在是第四次也是最后一次迭代
current_char=string[3]
#=>当前_char=“e”
to_return to_return(to_return是否
"eenu"
"rya"
"arrrrrrrr"
"12"
""
""