Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on reverse string exercise #9

Open
ZuZuD opened this issue Jan 31, 2017 · 0 comments
Open

Error on reverse string exercise #9

ZuZuD opened this issue Jan 31, 2017 · 0 comments

Comments

@ZuZuD
Copy link

ZuZuD commented Jan 31, 2017

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant