Ad
  • Custom User Avatar

    Hi seraph776! Your solution appears to be quite creative, but regrettably, it is SLOWER and INEFFICIENT.

    The time complexity of your function is O(n^2), while mine is O(n). With that being said, yours is SLOWER.

    Let's see if is WAY SLOWER. Don't believe me, just watch, come on!

    len(text) Mine Yours
    100 0.0069141387939453125 ms 0.03218650817871094 ms
    500 0.008821487426757812 ms 0.24199485778808594 ms
    1000 0.013113021850585938 ms RecursionError: maximum recursion depth exceeded while calling a Python object

    Both functions used the same text for the tests, and the text was generated randomly using:

    def generate_random_string():
        return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(1000))
    

    Besides, as you can see, YOUR FUNCTION DOES NOT SUPPORT LONG STRING CHAINS.

    Why?

    Python has a limit on the depth of recursion to prevent a stack overflow. Since the size of the call stack is finite, a program or a function that recurses too deeply can exhaust the stack. In Python, the maximum depth of recursion is about 1000 layers for most systems.

    Happy to help you, nice try! 😁

  • Custom User Avatar

    Hmph, I think my version is way faster than yours! 😁

  • Custom User Avatar

    Thx snoopythekid! Glad you liked it! This is an old debate; I simply used acquired knowledge.

    For a comparatively large list, under time constraints, it seems that the reversed() function performs faster than the slicing method. This is because reversed() just returns an iterator that iterates the original list in reverse order, without copying anything whereas slicing creates an entirely new list, copying every element from the original list. For a list with 10^6 Values, the reversed() performs almost 20,000 better than the slicing method. If there is a need to store the reverse copy of data then slicing can be used but if one only wants to iterate the list in reverse manner, reversed() is definitely the better option.

  • Custom User Avatar

    I tried making a faster version, but yours is faster than everythng I tried. Congrats

  • Custom User Avatar

    import codewars_test as test

    x = lambda a: ''.join(reversed(a))

    @test.describe("Main")
    def main_tests():
    @test.it("Test 1")
    def test1():
    test.assert_equals(x("hello"), "olleh")

    @test.it("Test 2")
    def test2():
        test.assert_equals(x("python"), "nohtyp")
    
    @test.it("Test 3")
    def test3():
        test.assert_equals(x("world"), "dlrow")
    
    @test.it("Test 4")
    def test4():
        test.assert_equals(x("abcdef"), "fedcba")
    
    @test.it("Test 5")
    def test5():
        test.assert_equals(x(""), "")
    

    test.run_tests()

  • Custom User Avatar
  • Custom User Avatar

    It says 'experinece' (mispelling) somewhere in the Python instructions.

    not sure about other languages

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution