【问题标题】:Outputs string if string contains vowels and is in alphabetical order如果字符串包含元音并且按字母顺序输出字符串
【发布时间】:2014-01-21 18:55:10
【问题描述】:

我正在做以下练习:

编写一个方法ovd(str),它接受一串小写单词并返回一个字符串,其中仅包含按字母顺序包含所有元音(不包括“y”)的单词。元音可以重复("afoot" 是一个有序的元音词)。如果单词不是按字母顺序,则该方法不会返回该单词。

示例输出为:

ovd("这是对元音排序系统的测试") #output=> "这是对系统的测试"

ovd("复杂") #output=> ""

下面是我编写的可以完成这项工作的代码,但我正在寻找是否有更短更聪明的方法来做到这一点。我的解决方案似乎太长了。在此先感谢您的帮助。

def ovd?(str) 
  u=[] 
  k=str.split("")   
  v=["a","e","i","o","u"]
  w=k.each_index.select{|i| v.include? k[i]}
  r={}
  for i in 0..v.length-1
     r[v[i]]=i+1
  end
  w.each do |s|
     u<<r[k[s]]
  end
   if u.sort==u 
      true
  else 
      false
  end

end

def ovd(phrase) 
  l=[]
  b=phrase.split(" ") 
  b.each do |d|
    if ovd?(d)==true
    l<<d 
    end
  end
  p l.join(" ") 
end

【问题讨论】:

    标签: ruby arrays string sorting


    【解决方案1】:
    def ovd(str)
      str.split.select { |word| "aeiou".match(/#{word.gsub(/[^aeiou]/, "").chars.uniq.join(".*")}/) }.join(" ")
    end
    ovd("this is a test of the vowel ordering system") # => "this is a test of the system"
    ovd("complicated") # => ""
    ovd("alooft") # => "alooft"
    ovd("this is testing words having something in them") # => "this is testing words having in them"
    

    编辑

    应OP的要求,解释

    String#gsub word.gsub(/[^aeiou]/, "") 删除非元音字符,例如 "aloot".gsub(/[^aeiou]/, "") # => "aoo" String#chars 将新单词转换为字符数组

    "afloot".gsub(/[^aeiou]/, "").chars # => ["a", "o", "o"]
    

    Array#uniq converts 仅返回数组中的唯一元素,例如

    "afloot".gsub(/[^aeiou]/, "").chars.uniq # => ["a", "o"]
    

    Array#join 将数组转换为字符串,将其与提供的参数合并,例如

    "afloot".gsub(/[^aeiou]/, "").chars.uniq.join(".*") # => "a.*o"  
    

    #{} 只是 String interpolation 并且 // 将内插字符串转换为正则表达式

    /#{"afloot".gsub(/[^aeiou]/, "").chars.uniq.join(".*")}/ # => /a.*o/
    

    【讨论】:

    • 哇!棒极了!您将我的 20 多行代码减少到 3 行!惊人的!谢谢!
    • @user3145336 注意我更新了我的答案,以捕捉重复的元音
    • 谢谢!我认为 str.split.select { |word| "aeiou".match(word.gsub(/[^aeiou]/, "").chars.to_a.uniq.join(".*"))}.join(" ") 也有效 =)
    • @user3145336 chars 作为返回的数组已经不需要调用 to_a 了
    • 当我做 word.gsub(/[^aeiou]/, "").chars: #=> .仅当我执行 word.gsub(/[^aeiou]/, "").chars.to_a #=> ["a", "o", "o"] 时。也许我做错了什么。
    【解决方案2】:

    非正则表达式解决方案:

    V = %w[a e i o u] # => ["a", "e", "i", "o", "u"]
    
      def ovd(str)
        str.split.select{|w| (x = w.downcase.chars.select \
          {|c| V.include?(c)}) == x.sort}.join(' ')
      end
    
      ovd("this is a test of the vowel ordering system")
                                              # => "this is a test of the system"
      ovd("")                                 # => ""
      ovd("camper")                           # => "camper" 
      ovd("Try singleton")                    # => "Try"
      ovd("I like leisure time")              # => "I" 
      ovd("The one and only answer is '42'")  # => "The and only answer is '42'"
      ovd("Oil and water don't mix")          # => "and water don't mix" 
    

    编辑以添加替代:

    NV = (0..127).map(&:chr) - %w(a e i o u) # All ASCII chars except lc vowels
    
    def ovd(str)
      str.split.select{|w| (x = w.downcase.chars - NV) == x.sort}.join(' ')
    end
    

    注意x = w.downcase.chars &amp; V 不起作用。虽然它从w 中剔除元音并保留它们的顺序,但它会删除重复项。

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 2013-05-27
      • 2022-01-02
      • 2016-04-21
      相关资源
      最近更新 更多