pythonpython-3.xstringconditional-statementscodewarrior

single word input succesfully ase swaped,but not worked for the sentences in problem weird case codewarrior?


Currently i am doing the weird case problem in cordwarrior. the problem is that 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.

I wrote the function.It successfully swap the case of single word,But don't work for multiple words or a sentence,So i thought about to apply swapcase but it swaped all the words of the sentences which is not desirable.So i can not figure out which condition may suitable for this::

   def to_weird_case(string):
    tot=""
    for i in range(len(string)):
        if i%2==0:
             tot+=string[i].upper()
        if i%2==1:
             tot+=string[i].lower()

    if string[i]==" " or i%2==1:
       to_weird_case(string[i:])
    return tot   

error i got this type:

 toWeirdCase
 should return the correct value for a single word (8 of 8 Assertions)
 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'\

link to the problem:https://www.codewars.com/kata/52b757663a95b11b3d00062d/train/python

I thought about a another approach that to divide the sentence and apply the function on each word.


   def swap(string):
    tot=""
    for i in range(len(string)):
        if i%2==0:
             tot+=string[i].upper()
        if i%2==1:
             tot+=string[i].lower()
    if i%2==0:
        swap(string[i])
    return tot

  def to_weird_case(string):
      new=""
      for j in string:
          print(swap(j))
  print(to_weird_case('ThIs iS A TeSt'))

but it make all word uppercase.


Solution

  • You were really close. You just have to to something special when there is space.

    Since your first function worked well for single words, I kept it to do the real one.

    def to_weird_case(string): # actually your function
        tot=""
        for i in range(len(string)):
            if i%2==0:
                 tot+=string[i].upper()
            if i%2==1:
                 tot+=string[i].lower()
    
        if string[i]==" " or i%2==1:
           to_weird_case(string[i:])
        return tot   
    
    def to_weird_case_words(string): # my contribution, so that It work with strings with spaces
        return ' '.join([to_weird_case(s) for s in string.split(' ')])
    
    print(to_weird_case('this is a test'))
    print(to_weird_case_words('this is a test'))
    

    here is an explanation of what I did : first, there is the string.split(' ') that makes a list with single words. With 'this is a test', the output is

    ['this', 'is', 'a', 'test'].

    Then I loop over it with the list comprehension, and for each word, apply your function. So now we have this list :

    ['ThIs', 'Is', 'A', 'TeSt'].

    And now we need to join that list, putting space character between each words. That is done with ' '.join(list). So we have now our final output :

    'ThIs Is A TeSt'

    There is others ways to do it, but I wanted to use what you had already done.