The count smiley face question on Codewars, my codes passed all the tests, but an "Exit Code = 1" error message keeps popping up, what does that mean? What went wrong?
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
EDIT: The error message is as below:
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>'
I realized what the problem is - there is a test count_smileys([";", ")", ";*", ":$", "8-D"])
where x[1] and x[2] would not be valid for the first 2 items in the array, so I need to fix the array inside the select method:
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 was correct in the sense that nils need to be converted, but we should do it in the iteration, and common items x[1] should be set to an empty string with a space to avoid being counted, while x[2] is uncommon enough that an empty string would work.