Skip to content

Error on reverse string exercise #9

Description

@ZuZuD

There's an error with your code reversed

def reverse(s):
    new_str = ""
    for i in range(len(s)):
        new_str += s[i*-1]
    return new_str

This return

reverse('choco')
'cocoh'
reverse('cats')
'csta'

And it's wrong because python Index start at 0 and 0*-1 = 0

A workaround could be

def reverse(s):
     new_str = ""
     for i in range(len(s)):
         if i == 0:
             new_str += s[-1]
         else:
             new_str += s[(i+1)*-1]         
     return new_str

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions