Ruby 计数笑脸 - 退出代码 = 1
Ruby Count Smiley Face - Exit Code = 1
Codewars 上的计数笑脸问题,我的代码通过了所有测试,但是 "Exit Code = 1" 错误消息一直弹出,这是什么意思?什么地方出了错?
countSmileys([':)', ';(', ';}', ':-D']); // should return 2;
countSmileys([';D', ':-(', ':-)', ';~)']); // should return 3;
countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;
def count_smileys(arr)
first = ";:"
second = "-~"
third = ")D"
arr.select{|x|
third.include?(x[1]) or (second.include?(x[1]) && third.include?(x[2].to_s))
}.count
end
编辑:
错误信息如下:
main.rb:8:in `include?': no implicit conversion of nil into String (TypeError)
from main.rb:8:in `block in count_smileys'
from main.rb:7:in `select'
from main.rb:7:in `count_smileys'
from main.rb:16:in `block in <main>'
from /runner/frameworks/ruby/cw-2.rb:55:in `block in describe'
from /runner/frameworks/ruby/cw-2.rb:46:in `measure'
from /runner/frameworks/ruby/cw-2.rb:51:in `describe'
from main.rb:11:in `<main>'
如消息所述,没有从 nil 到字符串的隐式转换。虽然明确存在:
2.3.1 :001 > nil.to_s
=> ""
您可以先为 nil
解析您的数组,然后通过 select
方法将其放入。
def count_smileys(arr)
first = ";:"
second = "-~"
third = ")D"
# parse your array for nil values here
arr.map {|x| x.nil? ? "" : x }
arr.select{|x|
third.include?(x[1]) or (second.include?(x[1]) && third.include?(x[2].to_s))
}.count
end
我意识到问题所在 - 有一个测试 count_smileys([";", ")", ";*", ":$", "8-D"])
,其中 x[1] 和 x[2] 对数组中的前 2 项无效,所以我需要修复select 方法中的数组:
def count_smileys(arr)
first = ";:"
second = "-~"
third = ")D"
arr.select{|x|
x[1] = " " if x[1] == nil
x[2] = "" if x[2] == nil
(first.include?(x[0]) && third.include?(x[1])) || (first.include?(x[0]) && second.include?(x[1]) && third.include?(x[2]))
}.count
end
Joseph Cho 在需要转换 nils 的意义上是正确的,但我们应该在迭代中进行转换,并且公共项目 x[1] 应该设置为带有 space 的空字符串避免被计算在内,而 x[2] 并不常见,空字符串也可以。
Codewars 上的计数笑脸问题,我的代码通过了所有测试,但是 "Exit Code = 1" 错误消息一直弹出,这是什么意思?什么地方出了错?
countSmileys([':)', ';(', ';}', ':-D']); // should return 2;
countSmileys([';D', ':-(', ':-)', ';~)']); // should return 3;
countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;
def count_smileys(arr)
first = ";:"
second = "-~"
third = ")D"
arr.select{|x|
third.include?(x[1]) or (second.include?(x[1]) && third.include?(x[2].to_s))
}.count
end
编辑: 错误信息如下:
main.rb:8:in `include?': no implicit conversion of nil into String (TypeError)
from main.rb:8:in `block in count_smileys'
from main.rb:7:in `select'
from main.rb:7:in `count_smileys'
from main.rb:16:in `block in <main>'
from /runner/frameworks/ruby/cw-2.rb:55:in `block in describe'
from /runner/frameworks/ruby/cw-2.rb:46:in `measure'
from /runner/frameworks/ruby/cw-2.rb:51:in `describe'
from main.rb:11:in `<main>'
如消息所述,没有从 nil 到字符串的隐式转换。虽然明确存在:
2.3.1 :001 > nil.to_s
=> ""
您可以先为 nil
解析您的数组,然后通过 select
方法将其放入。
def count_smileys(arr)
first = ";:"
second = "-~"
third = ")D"
# parse your array for nil values here
arr.map {|x| x.nil? ? "" : x }
arr.select{|x|
third.include?(x[1]) or (second.include?(x[1]) && third.include?(x[2].to_s))
}.count
end
我意识到问题所在 - 有一个测试 count_smileys([";", ")", ";*", ":$", "8-D"])
,其中 x[1] 和 x[2] 对数组中的前 2 项无效,所以我需要修复select 方法中的数组:
def count_smileys(arr)
first = ";:"
second = "-~"
third = ")D"
arr.select{|x|
x[1] = " " if x[1] == nil
x[2] = "" if x[2] == nil
(first.include?(x[0]) && third.include?(x[1])) || (first.include?(x[0]) && second.include?(x[1]) && third.include?(x[2]))
}.count
end
Joseph Cho 在需要转换 nils 的意义上是正确的,但我们应该在迭代中进行转换,并且公共项目 x[1] 应该设置为带有 space 的空字符串避免被计算在内,而 x[2] 并不常见,空字符串也可以。