donnemartin / interactive-coding-challenges

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
Other
29.44k stars 4.45k forks source link

Issue with Pythonic Solution for reverse_string #256

Closed anthonyattard closed 5 months ago

anthonyattard commented 5 years ago

The following solution does not work:

class ReverseStringAlt(object):

    def reverse_string_alt(string):
        if string:
            return string[::-1]
        return string

    def reverse_string_alt2(string):
        if string:
            return ''.join(reversed(string))
        return string
ghost commented 5 years ago

class ReverseStringAlt(object): def reverse_string_alt(self,string): if string: return string[::-1] return string

a=ReverseStringAlt() s=input() k=a.reverse_string_alt(s) print(k)

anthonyattard commented 5 years ago

@Harshavika is my solution correct?

rizveeredwan commented 5 years ago

@anthonyattard , this is your code which I just changed a little bit to run which gives correct answer. https://ideone.com/Tm5EE0.