I don't understand what's wrong with this function I coded for a kata in Codewars.
These 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 the function I coded:
def to_weird_case(string): return "".join(char.upper() if string.find(char) % 2 == 0 else char.lower() for char in string) Here's the result I get:
Time: 695ms Passed: 5 Failed: 8 Exit Code: 1 Test Results: toWeirdCase should return the correct value for a single word Test Passed Test Passed Test Passed 'TeST' should equal 'TeSt' 'LookS' should equal 'LoOkS' Test Passed Test Passed 'PaSSEd' should equal 'PaSsEd' should return the correct value for multiple words 'ThIs Is A TesT' should equal 'ThIs Is A TeSt' 'LookS Like you pASSeD' should equal 'LoOkS LiKe YoU PaSsEd' 'ThIs Is ThE FINaL TEsT casE' should equal 'ThIs Is ThE FiNaL TeSt CaSe' 'JuSt kIddINg' should equal 'JuSt KiDdInG' 'Ok fInE YoU ArE DonE now' should equal 'Ok FiNe YoU ArE DoNe NoW' It looks like it passes some of the tests, while it struggles with a couple of single words and every sentence. Does any of you know what I am doing wrong? Thanks.
'Ok fInEyou have lowerfbut it has to be upperF. You have to convert every word separately but it seems you convert all words as single string.string.find(char)is not good idea - for secondTin worktestit gives you position of firstT. better useenumerate(word)