反转字符串的前半部分和后半部分并将它们 return 在一起
reverse first & second halves of string and return them together
我如何定义一个将字符串分成两部分并将它们反转的函数?如果字符串是奇数,则保留中间字符(余数)。
例如:
revStr(“ “)
revStr(“xy“)
revStr(“wxyz“)
revStr(“vwxyz“)
revStr(“uvwxyz“)
revStr(“tuvwxyz”)
会输出:
xy
xwzy
wvxzy
wvuzyx
vutwzyx
我已经试过了,但我什至无法正确翻转偶数字符串...
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")
为此需要一些指导或想法,在此先感谢!如果有什么我可以澄清的,请告诉我。
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"))
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"))
输出:
xy
xwzy
wvxzy
wvuzyx
vutwzyx
你搞砸的是中间索引部分和两半。在这里你可以做的是取一个中间索引 value=len//2 并取一半,这样一半到中间,另一半从中间索引开始。一旦你有了两半,你可以简单地使用 x[::-1] 等反向打印它们
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))
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
我如何定义一个将字符串分成两部分并将它们反转的函数?如果字符串是奇数,则保留中间字符(余数)。
例如:
revStr(“ “)
revStr(“xy“)
revStr(“wxyz“)
revStr(“vwxyz“)
revStr(“uvwxyz“)
revStr(“tuvwxyz”)
会输出:
xy
xwzy
wvxzy
wvuzyx
vutwzyx
我已经试过了,但我什至无法正确翻转偶数字符串...
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")
为此需要一些指导或想法,在此先感谢!如果有什么我可以澄清的,请告诉我。
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"))
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"))
输出:
xy
xwzy
wvxzy
wvuzyx
vutwzyx
你搞砸的是中间索引部分和两半。在这里你可以做的是取一个中间索引 value=len//2 并取一半,这样一半到中间,另一半从中间索引开始。一旦你有了两半,你可以简单地使用 x[::-1] 等反向打印它们
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))
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