- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在测试以下问题的可能解决方案。我提出的前两个解决方案“去实体”和“去实体”运行不正常。我想知道为什么。
离体三号是目前为止我最喜欢的解决方案。但我觉得如果我不明白我的前两个解决方案哪里出了问题,我就没有权利使用非实体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
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
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
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
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
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
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
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
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
Array#each_with_index
。
def disemvowel_1_9(string)
string.chars.map {|char| "aeiouAEIOU".include?(char) ? nil : char }.join
end
delete
替换
delete_if
。
Array#delete
方法只会删除完全匹配的项,因此必须循环元音才能使其在本例中正常工作。但是,
Array#delete_if
允许您在某个条件下删除,该条件为
vowels.include?(element)
。
def disemvowel_2(string)
string_array = string.split('')
string_array.delete_if {|element| "aeiou".include?(element) }
string_array.join('')
end
def disemvowel_2_1(string)
string_array = string.split('')
string_array.delete_if {|element| "aeiouAEIOU".include?(element) }
string_array.join('')
end
split("")
替换为
String#chars
以获取要处理的字符数组。
def disemvowel_2_2(string)
string_array = string.chars
string_array.delete_if {|element| "aeiouAEIOU".include?(element) }
string_array.join('')
end
join('')
更改为仅
join
。
join
方法已经以这种方式连接,因此额外的参数是多余的
def disemvowel_2_3(string)
string_array = string.chars
string_array.delete_if {|element| "aeiouAEIOU".include?(element) }
string_array.join('')
end
def disemvowel_2_4(string)
string.chars.delete_if {|element| "aeiouAEIOU".include?(element) }.join
end
delete
方法。给定元音,这是一个简单的实现:
def disemvowel_4(string)
string.delete("aeiouAEIOU")
end
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/
我试图在 haskell 上创建一个函数,我知道该函数的定义如下: justWithA : [Char] -> Bool justWithA [] = True justWithA (x:xs) |
我试图在 haskell 上创建一个函数,我知道该函数的定义如下: justWithA : [Char] -> Bool justWithA [] = True justWithA (x:xs) |
我正在尝试计算字符串中元音的总数。我正在使用 strlen 来获取字符串的总长度,但是当我尝试按每个字母对字符串进行计数时,它说 C++ 禁止比较。所以我假设我的 if 语句有问题。 #include
Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+,-). Your
我刚刚开始用 C 语言编程,我必须创建一个程序来计算字符串有多少个元音。到目前为止我有这个: int a; int len = strlen(text)-1 for(a=0;a==len;++a){
尝试学习 Python 中的正则表达式以查找具有连续元音-辅音或辅音-元音组合的单词。我将如何在正则表达式中执行此操作?如果无法在 Regex 中完成,是否有一种在 Python 中执行此操作的有效方
我想输入 dagger-alif、dagger-waw 和 dagger-ya(也称为微型 alif、微型哇和微型 ya)作为 alif-wasla 使用 PC 阿拉伯语键盘。这些标记被使用帮助读者发
我正在尝试创建一个将文件上传到 FTP 服务器的批处理文件。除了一个特定文件夹的名称中包含突变/元音外,一切正常(无法更改。也就是文件夹名称中包含 ö。)。 我的问题是:有哪些选择可以实现这一目标?
我是一名优秀的程序员,十分优秀!