【问题标题】:How to keep a loop within a loop from repeating an output in Ruby如何在循环中保持循环以防止在 Ruby 中重复输出
【发布时间】:2014-05-04 05:33:13
【问题描述】:

嘿,我刚开始在 codeacademy 学习 Ruby,我遇到了一个练习,其中包含我无法获得的可选挑战。该程序采用用户定义的字符串并“编辑”用户想要“编辑”的单词。如您所见,它将字符串转换为数组,然后循环遍历数组中的每个值(也称为单个单词),并尝试将原始字符串的值与用户想要“编辑”的单词的值进行交叉检查。最初的问题是让它编辑一个变量,挑战是编辑多个单词。我的问题是,当它通过检查循环时,它总是返回错误的值。我知道出了什么问题,循环必须不止一次地交叉检查值,当它无法“==”时,它会继续打印出这个词,但是有没有办法解决这个问题?还是有更好的角度来解决这个问题?

puts "Give me what you've got" 
text = gets.chomp
text.downcase! 
puts "What words do you wish to redact"
redact = gets.chomp 
redact.downcase!
bye_words = redact.split(" ") 
words = text.split(" ")
words.each do |single_word| 
    bye_words.each do |word_in_question| 
        if word_in_question == single_word 
            print "REDACTED "
        else 
            print single_word + " "
        end 
    end 
end

【问题讨论】:

  • 在内循环中,只需跟踪您是否找到匹配项。然后在内部循环之后,如果找到匹配项,则打印 'REDACTED',如果没有,则打印该单词。
  • 你能解释一下我会怎么做吗?我想这涉及到某种布尔值的使用......?
  • Sweet 我想出了如何让它工作,现在它工作得很好。谢谢
  • 太棒了!如果您像下面建议的那样使用include?(这是一个好主意),那么您实际上是在做同样的事情,您只是使用内置的include?(循环遍历寻找匹配项的元素)代替自己编写循环。

标签: ruby loops


【解决方案1】:

也许只使用include? 而不是另一个循环:

puts "Give me what you've got"
text = gets.chomp
text.downcase!
puts "What words do you wish to redact"
redact = gets.chomp
redact.downcase!
bye_words = redact.split(" ")
words = text.split(" ")

words.each do |single_word|
  if bye_words.include? single_word
    print "REDACTED "
  else
    print single_word + " "
  end
end

甚至gsub,但也许这是作弊......?

【讨论】:

    【解决方案2】:

    这样的事情怎么样?您不必遍历两个不同的集合:

    puts "Give me what you've got" 
    text = gets.chomp
    text.downcase! 
    puts "What words do you wish to redact"
    redact = gets.chomp 
    redact.downcase!
    bye_words = redact.split(" ") 
    words = text.split(" ")
    ouput_words = []
    words.each do |single_word| 
        if bye_words.include?(single_word)
            ouput_words << "REDACTED"
        else
            ouput_words << single_word
        end
    end
    
    print ouput_words.join(" ") + "\n"
    

    【讨论】:

      【解决方案3】:

      获得(几乎)所需的最快方法是使用-

      puts "Give me what you've got" 
      text = gets.chomp
      text.downcase! 
      puts "What words do you wish to redact"
      redact = gets.chomp 
      redact.downcase!
      bye_words = redact.split(" ") 
      words = text.split(" ")
      
      puts (words - bye_words).join(' ')
      

      问题在于,编辑后的文字将被简单地从文本中删除(不会添加'REDACTED')。

      要满足该要求,您需要在bye_words 中查找每个单词是否为included?。可以做些什么来改进其他人的建议是将bye_words 的列表变成Set

      require 'set'
      puts "Give me what you've got" 
      text = gets.chomp
      text.downcase! 
      puts "What words do you wish to redact"
      redact = gets.chomp 
      redact.downcase!
      bye_words = Set.new(redact.split(" "))
      words = text.split(" ")
      
      puts words.map { |word| bye_words.include?(word) ? 'REDACTED':word }.join(' ')
      

      【讨论】:

        猜你喜欢
        • 2022-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-17
        • 2021-10-23
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多