gpt4 book ai didi

ruby - 在Ruby中从字符串中删除元音

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:01 24 4
gpt4 key购买 nike

我正在测试以下问题的可能解决方案。我提出的前两个解决方案“去实体”和“去实体”运行不正常。我想知道为什么。
离体三号是目前为止我最喜欢的解决方案。但我觉得如果我不明白我的前两个解决方案哪里出了问题,我就没有权利使用非实体3。
有谁能帮我弄清楚前两个解决方案有什么问题吗?

# Write a function disemvowel(string), which takes in a string,
# and returns that string with all the vowels removed. Treat "y" as a
# consonant.

def disemvowel(string)
string_array = string.split
vowels = %w[aeiou]
i = 0
while i < string.length
if vowels.include? string[i] == true
string_array[i] = " "
end
i +=1
end

new_string = string_array.join
new_string = new_string.sub(/\s+/,"")
return new_string
end


def disemvowel_2(string)
string_array = string.split('')
string_array.delete('a','e','i','o','u')
return string_array.join('')
end

# This is my favorite solution.
def disemvowel_3(string)
result = string.gsub(/[aeiou]/i, '')
return result
end


#tests
puts disemvowel("foobar")
puts disemvowel("ruby")
puts disemvowel("aeiou")

最佳答案

微小的改变会使disempower正常工作。这就是解决问题的原因:
德文维尔
虫子
1)将split改为string.split("")。不带参数的split将按空格拆分,split("")将按字符拆分。通过此更改,string_array变成字符串中每个字符的数组。这也可以用首选方法string.chars更简洁地完成。
见:
String#split
String#chars
2)vowels已更改为字符串。%w[]创建一个单词数组,因此当使用%w[aeiou]时,vowels实际上是一个由1个字符串"aeiou"组成的数组。这意味着String#include?Array#include?都不能与每个字符进行比较。将其更改为常量字符串意味着vowels.include?可以与字符匹配。
见:
%w[]
Array#include?
String.include?
3)vowels.include?没有parens,并与true进行显式比较。ruby的工作方式是,表达式string_array[i] == true的结果被传递给vowels.include?,而这并不是我们想要的。
有两个风格提示可以帮助您做到这一点:
与true的比较应该是隐式的(例如,不要使用== true
调用函数或方法时使用parens。
见:
Omit parens for DSL and keywords; use around all other method invocations部分
4)sub更改为gsub。对sub的调用将只替换字符串中的一个,因此当使用“f b r”调用时,只替换第一个空格,留下字符串“fb r”。gsub执行“全局替换”,这正是您在本例中需要的。
见:
Ruby Style Guide
String#gsub
第一个工作版本
workingdisemvowel函数如下所示:

def disemvowel(string)
string_array = string.split("")
vowels = "aeiou"
i = 0
while i < string.length
if vowels.include?(string[i])
string_array[i] = " "
end
i +=1
end

new_string = string_array.join
new_string = new_string.gsub(/\s+/,"")
return new_string
end

并通过测试生成此输出:
fbr
rby

清理
1)支持大小写混合元音。
def disemvowel_1_1(string)
string_array = string.split("")
vowels = "aeiouAEIOU"
i = 0
while i < string_array.length
if vowels.include?(string_array[i])
string_array[i] = " "
end
i +=1
end

new_string = string_array.join
new_string = new_string.gsub(/\s+/,"")
return new_string
end

2)一致使用 string_array而不是与 string混合使用。当更适合使用 string时, string_array的各种用法就会出现。这个应该换掉。
def disemvowel_1_2(string)
string_array = string.split("")
vowels = "aeiouAEIOU"
i = 0
while i < string_array.length
if vowels.include?(string_array[i])
string_array[i] = " "
end
i +=1
end

new_string = string_array.join
new_string = new_string.gsub(/\s+/,"")
return new_string
end

3)不要为“aeiou”使用变量。这是一个常量表达式,应该以字符串文字或常量的形式写入。在这种情况下,将选择一个文本字符串,因为没有限制在全局命名空间中使用常量的封闭作用域(以防此代码插入到另一个上下文中)。
def disemvowel_1_3(string)
string_array = string.split("")
i = 0
while i < string_array.length
if "aeiouAEIOU".include?(string_array[i])
string_array[i] = " "
end
i +=1
end

new_string = string_array.join
new_string = new_string.gsub(/\s+/,"")
return new_string
end

4)用 nil代替 " "替换元音字符,以消除 gsub替换。
def disemvowel_1_4(string)
string_array = string.split("")
i = 0
while i < string_array.length
if "aeiouAEIOU".include?(string_array[i])
string_array[i] = nil
end
i +=1
end

new_string = string_array.join
return new_string
end

5)将 while循环转换为 Array#each_with_index以处理数组元素
def disemvowel_1_5(string)
string_array = string.split("")
string_array.each_with_index do |char, i|
if "aeiouAEIOU".include?(char)
string_array[i] = nil
end
end

new_string = string_array.join
return new_string
end

6)将 split("")替换为 String#chars以获取要处理的字符数组。
def disemvowel_1_6(string)
string_array = string.chars
string_array.each_with_index do |char, i|
if "aeiouAEIOU".include?(char)
string_array[i] = nil
end
end

new_string = string_array.join
return new_string
end

7)通过链接结果减少临时变量的数量。这可以最小化ruby必须跟踪的单个变量的数量,并减少每次引用变量名时发生的变量查找。
def disemvowel_1_7(string)
string_array = string.chars
string_array.each_with_index do |char, i|
if "aeiouAEIOU".include?(char)
string_array[i] = nil
end
end

new_string = string_array.join
return new_string
end

8)删除显式返回以使用Ruby基于表达式的返回值。
def disemvowel_1_8(string)
string_array = string.chars
string_array.each_with_index do |char, i|
if "aeiouAEIOU".include?(char)
string_array[i] = nil
end
end.join
end

9)使用array map来处理字符,而不是 Array#each_with_index
def disemvowel_1_9(string)
string.chars.map {|char| "aeiouAEIOU".include?(char) ? nil : char }.join
end

DeDeVoWEL 2
虫子
1)用 delete替换 delete_ifArray#delete方法只会删除完全匹配的项,因此必须循环元音才能使其在本例中正常工作。但是, Array#delete_if允许您在某个条件下删除,该条件为 vowels.include?(element)
见:
String#sub
Array#delete
第一个工作版本
def disemvowel_2(string)
string_array = string.split('')
string_array.delete_if {|element| "aeiou".include?(element) }
string_array.join('')
end

清理
1)支持大小写混合元音。
def disemvowel_2_1(string)
string_array = string.split('')
string_array.delete_if {|element| "aeiouAEIOU".include?(element) }
string_array.join('')
end

2)将 split("")替换为 String#chars以获取要处理的字符数组。
def disemvowel_2_2(string)
string_array = string.chars
string_array.delete_if {|element| "aeiouAEIOU".include?(element) }
string_array.join('')
end

3)将 join('')更改为仅 joinjoin方法已经以这种方式连接,因此额外的参数是多余的
def disemvowel_2_3(string)
string_array = string.chars
string_array.delete_if {|element| "aeiouAEIOU".include?(element) }
string_array.join('')
end

4)通过链接结果减少临时变量的数量。这可以最小化ruby必须跟踪的单个变量的数量,并减少每次引用变量名时发生的变量查找。
def disemvowel_2_4(string)
string.chars.delete_if {|element| "aeiouAEIOU".include?(element) }.join
end

DeDeVoWEL 4
string有一个将删除所有匹配字符的 delete方法。给定元音,这是一个简单的实现:
def disemvowel_4(string)
string.delete("aeiouAEIOU")
end

见:
Array#delete_if
测试
我创建了一个类似单元测试的程序来进行程序化的自我测试,而不仅仅是向控制台显示不受支持的字符串。这将测试函数的每个版本,并报告它是否通过测试:
data = [
["foobar", "fbr"],
["ruby", "rby"],
["aeiou", ""],
["AeIoU", ""],
]

data.each do |test|
puts "disemvowel_1 #{disemvowel_1(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_1 #{disemvowel_1_1(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_2 #{disemvowel_1_2(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_3 #{disemvowel_1_3(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_4 #{disemvowel_1_4(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_5 #{disemvowel_1_5(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_6 #{disemvowel_1_6(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_7 #{disemvowel_1_7(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_8 #{disemvowel_1_8(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_1_9 #{disemvowel_1_9(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_2 #{disemvowel_2(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_2_1 #{disemvowel_2_1(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_2_2 #{disemvowel_2_2(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_2_3 #{disemvowel_2_3(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_2_4 #{disemvowel_2_4(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_3 #{disemvowel_3(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
puts "disemvowel_4 #{disemvowel_4(test[0]) == test[1] ? 'Pass' : 'Fail'}: '#{test[0]}'"
end

这将产生以下输出:
>$ ruby disemvowel.rb
disemvowel_1 Pass: 'foobar'
disemvowel_1_1 Pass: 'foobar'
disemvowel_1_2 Pass: 'foobar'
disemvowel_1_3 Pass: 'foobar'
disemvowel_1_4 Pass: 'foobar'
disemvowel_1_5 Pass: 'foobar'
disemvowel_1_6 Pass: 'foobar'
disemvowel_1_7 Pass: 'foobar'
disemvowel_1_8 Pass: 'foobar'
disemvowel_1_9 Pass: 'foobar'
disemvowel_2 Pass: 'foobar'
disemvowel_2_1 Pass: 'foobar'
disemvowel_2_2 Pass: 'foobar'
disemvowel_2_3 Pass: 'foobar'
disemvowel_2_4 Pass: 'foobar'
disemvowel_3 Pass: 'foobar'
disemvowel_4 Pass: 'foobar'
disemvowel_1 Pass: 'ruby'
disemvowel_1_1 Pass: 'ruby'
disemvowel_1_2 Pass: 'ruby'
disemvowel_1_3 Pass: 'ruby'
disemvowel_1_4 Pass: 'ruby'
disemvowel_1_5 Pass: 'ruby'
disemvowel_1_6 Pass: 'ruby'
disemvowel_1_7 Pass: 'ruby'
disemvowel_1_8 Pass: 'ruby'
disemvowel_1_9 Pass: 'ruby'
disemvowel_2 Pass: 'ruby'
disemvowel_2_1 Pass: 'ruby'
disemvowel_2_2 Pass: 'ruby'
disemvowel_2_3 Pass: 'ruby'
disemvowel_2_4 Pass: 'ruby'
disemvowel_3 Pass: 'ruby'
disemvowel_4 Pass: 'ruby'
disemvowel_1 Pass: 'aeiou'
disemvowel_1_1 Pass: 'aeiou'
disemvowel_1_2 Pass: 'aeiou'
disemvowel_1_3 Pass: 'aeiou'
disemvowel_1_4 Pass: 'aeiou'
disemvowel_1_5 Pass: 'aeiou'
disemvowel_1_6 Pass: 'aeiou'
disemvowel_1_7 Pass: 'aeiou'
disemvowel_1_8 Pass: 'aeiou'
disemvowel_1_9 Pass: 'aeiou'
disemvowel_2 Pass: 'aeiou'
disemvowel_2_1 Pass: 'aeiou'
disemvowel_2_2 Pass: 'aeiou'
disemvowel_2_3 Pass: 'aeiou'
disemvowel_2_4 Pass: 'aeiou'
disemvowel_3 Pass: 'aeiou'
disemvowel_4 Pass: 'aeiou'
disemvowel_1 Fail: 'AeIoU'
disemvowel_1_1 Pass: 'AeIoU'
disemvowel_1_2 Pass: 'AeIoU'
disemvowel_1_3 Pass: 'AeIoU'
disemvowel_1_4 Pass: 'AeIoU'
disemvowel_1_5 Pass: 'AeIoU'
disemvowel_1_6 Pass: 'AeIoU'
disemvowel_1_7 Pass: 'AeIoU'
disemvowel_1_8 Pass: 'AeIoU'
disemvowel_1_9 Pass: 'AeIoU'
disemvowel_2 Pass: 'AeIoU'
disemvowel_2_1 Pass: 'AeIoU'
disemvowel_2_2 Pass: 'AeIoU'
disemvowel_2_3 Pass: 'AeIoU'
disemvowel_2_4 Pass: 'AeIoU'
disemvowel_3 Pass: 'AeIoU'
disemvowel_4 Pass: 'AeIoU'

标杆管理
我编写了一个基准程序来测试每个实现的性能。下面是基准程序:
Times = 5_000
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*(),./<>?;':\"[]{}\\|-=_+`~".chars
array = Times.times.map { |n| "#{chars.sample(n)}" }

puts "============================================================="
puts RUBY_DESCRIPTION

Benchmark.bm(15) do |x|
dismevowel_1_report = x.report("disemvowel_1:") { array.each {|s| disemvowel_1(s) } }
dismevowel_1_1_report = x.report("disemvowel_1_1:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_2_report = x.report("disemvowel_1_2:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_3_report = x.report("disemvowel_1_3:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_4_report = x.report("disemvowel_1_4:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_5_report = x.report("disemvowel_1_5:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_6_report = x.report("disemvowel_1_6:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_7_report = x.report("disemvowel_1_7:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_8_report = x.report("disemvowel_1_8:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_1_9_report = x.report("disemvowel_1_9:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_2_report = x.report("disemvowel_2:") { array.each {|s| disemvowel_2(s) } }
dismevowel_2_1_report = x.report("disemvowel_2_1:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_2_2_report = x.report("disemvowel_2_2:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_2_3_report = x.report("disemvowel_2_3:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_2_4_report = x.report("disemvowel_2_4:") { array.each {|s| disemvowel_1_1(s) } }
dismevowel_3_report = x.report("disemvowel_3:") { array.each {|s| disemvowel_3(s) } }
dismevowel_4_report = x.report("disemvowel_4:") { array.each {|s| disemvowel_4(s) } }
end

这是基准测试的结果:
=============================================================
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
user system total real
disemvowel_1: 2.630000 0.010000 2.640000 ( 3.487851)
disemvowel_1_1: 2.300000 0.010000 2.310000 ( 2.536056)
disemvowel_1_2: 2.360000 0.010000 2.370000 ( 2.651750)
disemvowel_1_3: 2.290000 0.010000 2.300000 ( 2.449730)
disemvowel_1_4: 2.320000 0.020000 2.340000 ( 2.599105)
disemvowel_1_5: 2.360000 0.010000 2.370000 ( 2.473005)
disemvowel_1_6: 2.340000 0.010000 2.350000 ( 2.813744)
disemvowel_1_7: 2.380000 0.030000 2.410000 ( 3.663057)
disemvowel_1_8: 2.330000 0.010000 2.340000 ( 2.525702)
disemvowel_1_9: 2.290000 0.010000 2.300000 ( 2.494189)
disemvowel_2: 2.490000 0.000000 2.490000 ( 2.591459)
disemvowel_2_1: 2.310000 0.010000 2.320000 ( 2.503748)
disemvowel_2_2: 2.340000 0.010000 2.350000 ( 2.608350)
disemvowel_2_3: 2.320000 0.010000 2.330000 ( 2.820086)
disemvowel_2_4: 2.330000 0.010000 2.340000 ( 2.735653)
disemvowel_3: 0.070000 0.000000 0.070000 ( 0.070498)
disemvowel_4: 0.020000 0.000000 0.020000 ( 0.018580)

结论
String#delete方法的性能比所有手工轧制的解决方案都高出100倍以上,比 String#gsub快2.5倍。它很容易使用,而且比其他任何东西都好;这很容易就是最好的解决方案。

关于ruby - 在Ruby中从字符串中删除元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com