I ran across an odd thing working on a Codewars problem. Here are the instructions:
Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the zero-ith index is even, therefore that character should be upper cased.
The passed in string will only consist of alphabetical characters and spaces(' '). Spaces will only be present if there are multiple words. Words will be separated by a single space(' '). Examples:
weirdcase( "String" )#=> returns "StRiNg"
weirdcase( "Weird string case" );#=> returns "WeIrD StRiNg CaSe"
Here's my code:
def weirdcase string
@case = []
# Ternary:
string.each_char { |c|
# string.index(c).even? ? @case.push(c.upcase) : @case.push(c.downcase)
c =~ /[[:alpha:]]/ ? (string.index(c).even? ? (@case.push(c.upcase)) : (@case.push(c.downcase))) : (string.index(c) + 1)
}
# If/Then:
# string.each_char { |c|
#if string.index(c).even? && c != " "
# if c =~ /[[:alpha:]]/ && string.index(c).even?
# then @case.push(c.upcase)
# else @case.push(c.downcase)
# end }
@case.join
end
p "TEST"
p weirdcase "this is a test"
p weirdcase "thisisatest"
p weirdcase " th is is a t es t"
The results:
"TEST"
"ThIsIsATesT"
"ThIsIsATEsT"
"tHIsIsAtest"
weirdcase
should return the correct value for multiple words
Expected: "ThIs Is A TeSt", instead got: "ThIsIsATesT"
0 Passed
1 Failed
0 Errors
Process took 171ms to complete
And here are the tests:
describe 'weirdcase' do
#it 'should return the correct value for a single word' do
# Test.assert_equals(weirdcase('This'), 'ThIs');
# Test.assert_equals(weirdcase('is'), 'Is');
#end
it 'should return the correct value for multiple words' do
Test.assert_equals(weirdcase('This is a test'), 'ThIs Is A TeSt');
end
end
I've tried writing this code a few different ways, and I keep getting the same result: instead of "ThIs Is A TeSt" for a result, I instead get "ThIsIsATesT" or some other variation where the alternating capitalization seems to fail once it reaches the word "test", which makes no sense to me - it seems to go against what the methods are supposed to do. I also tried it in irb, and saw the same result so I don't think its a Codewars bug.
I'm still relatively new at Ruby. Can anyone help me find the thing(s) I must be overlooking and/or explain how this code came to this result ?
Aucun commentaire:
Enregistrer un commentaire