dimanche 1 mars 2015

How to alternately upcase/downcase a passed-in string by each characters index?

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 ?


Update:


Everyone, thank you for your good advice. I updated the title to something more descriptive, and re-wrote this code from scratch to index-iterate over each word individually, instead of treating it like one long string. I'm still having some trouble with it though. Here's my new code:



def weirdcase string
char = Array.new
puts "string: #{string}"
string.split(' ').each do |word|
word.each_char.with_index do |c, index|
if index.even?
char.push(c.upcase)
else
char.push(c.downcase)
end
end
end
puts "char: #{char}"
puts "char.join(''): #{char.join('')}"
puts "char.join('space'): #{char.join(' ')}"

end


And here are the results:



weirdcase
should return the correct value for multiple words
string: This is a test
char: ["T", "h", "I", "s", "I", "s", "A", "T", "e", "S", "t"]
char.join(''): ThIsIsATeSt
char.join('space'): T h I s I s A T e S t
Expected: "ThIs Is A TeSt", instead got: nil


So the trouble I'm having now is how to properly concatenate the words with spaces in-between, instead of what you see above. I'm looking up ways to access the array entries properly after they are split initially. But of course I would welcome any more tips anyone might have :)


Aucun commentaire:

Enregistrer un commentaire