reverse first & second halves of string and return them together

Viewed 455

How can I define a function that splits a string into 2 parts, and reverses them both? If the string is odd, it leaves the middle character (remainder) alone.

For example:

revStr(“ “)
revStr(“xy“)
revStr(“wxyz“)
revStr(“vwxyz“)
revStr(“uvwxyz“)
revStr(“tuvwxyz”)

would output:


xy
xwzy
wvxzy
wvuzyx
vutwzyx

I have tried this, but I can't even get the even strings to flip correctly...

def revStr(string):
    output = " "
    start = 0 
    if len (string) % 2 == 0:
        x = string[start:start//2] 
        y = x[::-1]
        string = x+y
    return(string)
    
printrevStr("abcdef")

Needing some guidance or ideas for this, thanks in advance! Let me know if there is anything I can clarify.

5 Answers
def rev_str(string):
    mid_point = len(string) // 2
    if len(string) % 2 == 0:
        return string[0:mid_point][::-1] + string[mid_point:][::-1]
    else:
        return string[0:mid_point][::-1] + string[mid_point] + string[mid_point + 1:][::-1]


if __name__ == '__main__':
    print(rev_str(" "))
    print(rev_str("xy"))
    print(rev_str("wxyz"))
    print(rev_str("vwxyz"))
    print(rev_str("uvwxyz"))
    print(rev_str("tuvwxyz"))

Output:

 
xy
xwzy
wvxzy
wvuzyx
vutwzyx
def reverse_string(string):
    length = len(string)
    
    mid = length//2
    # seperating in 3 parst
    first_half, second_half, middle = None, None, None
    
    # seperating string into first half, second half and mid based on length
    if length%2==1:
        first_half = string[:mid]
        second_half = string[mid+1:]
        middle = string[mid]
    else:
        first_half = string[:mid]
        second_half = string[mid:]
        
    # reversing the first half and second half
    first_half_reverse = first_half[::-1]
    second_half_reverse = second_half[::-1]
    
    # assembling the final result together
    final_result = None
    if middle is not None:
        final_result = first_half_reverse + middle + second_half_reverse
    else:
        final_result = first_half_reverse + second_half_reverse
    
    return final_result
import math

def revStr(string):
    half = len(string) / 2
    string = string[:math.floor(half):][::-1] + string[math.floor(half):math.ceil(half):] + string[math.ceil(half)::][::-1]
    return (string)

print(revStr("abcde"))
print(revStr("abde"))

what you were messing up was the middle index part and getting the two halves. Here what you could have done is take a mid index value=len//2 and take the halves such that one half is till the mid and the other one begins from the mid index. Once you have the two halves you can simply print them in reverse using x[::-1] etc.

def revStr(string):
    mid=len(string)//2
    if len(string)%2 != 0:
        x=string[:mid]
        middle=string[mid]
        y=string[mid+1:]
        print(x[::-1],middle,y[::-1],sep='')
    else:
        x=string[:mid]
        y=string[mid:]
        print(x[::-1],y[::-1],sep='')

revStr("abcdef")

import re

def revStr(keyword):
    if len(keyword) <= 2:
        return keyword
        
    keyword = [val.group()[::-1] for num,val in enumerate(re.finditer(r"(.){2}",keyword), start=1)]
    return ''.join(map(str, keyword))
Related