Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Tuesday, May 7, 2013

Reverse the given string.


Problem: Reverse the given string.

Solution in Python:


def reverse(s):

    """
    Author: Mayur P Srivastava
    """


    arr = list(s)    
    n   = len(arr)

    for i in range(n // 2):
        arr[i], arr[n-i-1] = arr[n-i-1], arr[i]

    return "".join(arr)


Concepts Learned: Single loop.

No comments:

Post a Comment