今天在群里看到一个面试题,据说是阿里的面试题
没事就来做了下下面的2题

准备的测试用例:
s = "where are you going!I am going home,hello world"
t = "1,2,3 4 , , 23 3"
t = "1,2,3 4,,23 3"
t = "1"
t = ""
t = " "
t = "^%*^%&%^&"
两种办法:
第一种:一行代码解决
t = "where are you going!I am going home"
import string
import re
from functools import reduce
def fun2(t):
res = reduce(lambda x, y: x + t[len(x)] + y,
map(lambda x: " ".join(x.split(" ")[::-1]), re.split("[" + string.punctuation + "]", t)))
return res
第一种:勤勤恳恳写代码
start = 0
end = 0
length = len(s)
result = ""
for i in range(len(s)):
if s[i] in string.punctuation:
end=i
result+=" ".join(s[start:end].split()[::-1])
result+=s[i]
start= end+1
if end!=length-1:
end = length
result+=" ".join(s[start:end].split()[::-1])
本文针对阿里面试中出现的字符串操作题目进行了解析,并提供了两种不同的实现方案,包括使用一行代码解决问题的方法以及逐步实现的详细过程。
2167

被折叠的 条评论
为什么被折叠?



