【每日一题20220722】按要求移动字符串

:mage:‍我们有一个字符串 s

我们有一个数字n

下面是一个函数,它采用字符串,将偶数索引字符连接到前面,将奇数索引字符连接到后面。

例子

s = "Wow Example!"
result = "WwEapeo xml!"
s = "I'm JomoPipi"
result = "ImJm ii' ooPp"

任务:

在将函数应用到字符串 n 次后,返回字符串的结果。

例如,s = “qwertyuio” 和 n = 2:

after 1 iteration  s = "qetuowryi"
after 2 iterations s = "qtorieuwy"
return "qtorieuwy"

注意

有很多测试,大字符串,n大于十亿

所以准备好优化。

题目难度:一般
题目来源:String → N iterations → String | Codewars

def jumbled_string(s: str, n: int) -> str:
    # you've made it this far, now finish it!
    pass

assert jumbled_string("Such Wow!",1) == "Sc o!uhWw"
assert jumbled_string("better example",2) == "bexltept merae"
assert jumbled_string("qwertyuio",2) == "qtorieuwy"
assert jumbled_string("Greetings",8) == "Gtsegenri"

def jumbled_string(s: str, n: int) -> str:
    # you've made it this far, now finish it!
    start_num = 0
    while start_num < n:
        single_index_str = []
        pair_index_str = []
        for i in range(len(s)):
            if i % 2 == 0:
                pair_index_str.append(s[i])
            else:
                single_index_str.append(s[i])
        s = "".join((pair_index_str+single_index_str))
        start_num += 1
    return s


assert jumbled_string("Such Wow!", 1) == "Sc o!uhWw"
assert jumbled_string("better example", 2) == "bexltept merae"
assert jumbled_string("qwertyuio", 2) == "qtorieuwy"
assert jumbled_string("Greetings", 8) == "Gtsegenri"

时间复杂度不够哦,还有优化的方案,循环多了之后计算量有点大

了解

def jumbled_string(s: str, n: int):
    # you've made it this far, now finish it!
    for i in range(n):
        s_l = [item for index, item in enumerate(s) if index % 2 == 0]
        s_r = [item for index, item in enumerate(s) if index % 2 != 0]
        s = s_l + s_r
    return "".join(s)

assert jumbled_string("Such Wow!",1) == "Sc o!uhWw"
assert jumbled_string("better example",2) == "bexltept merae"
assert jumbled_string("qwertyuio",2) == "qtorieuwy"
assert jumbled_string("Greetings",8) == "Gtsegenri"

加个楼 -----------------

有个题是这样的::point_down:

完成Python 的装饰器 提交参数检查器,

若是整形 数字在0-10之间,

若是字符串 长度在8-16之间

若是数组list 则要求类型都是字符串

其他类型默认参数不能为空。

如果校验不通过,则抛异常,通过则继续执行

求助好心人help

可以直接发帖呀

很明显n没有做优化,n特别大的时候会超时

傻了 我干嘛要自己去计算呢 :sob::sob::sob:

好逗 哈哈哈哈

:rage::rage:

def jumbled_string(s, n):
    count = 0
    s_list=list(s)
    old_s=s_list
    while count < n:
        s_list=s_list[::2]+s_list[1::2]
        #找到交换多少次后还原的次数,然后重新计算需要交换的次数
        if s_list==old_s:
            n = n % (count+1)
            count = 0
            s=old_s
            continue
        count += 1
    return ''.join(s_list)
def jumbled_string(s: str, n: int) -> str:
    while n!=0:
        res=s[0::2]+s[1::2]
        n=n-1
        if n!=0:
            return jumbled_string(res,n)
        else:
            return res
def changestr(tar, num):    
    for i in range(num):
        tar = tar[::2] + tar[1::2]
    return tar

大佬 可以解释下代码嘛

ef jumbled_string(s: str, n: int) -> str:
    t=0
    while t<n:
        s=s[::2]+s[1::2]
        t+=1
    return s

assert jumbled_string("Such Wow!",1) == "Sc o!uhWw"
assert jumbled_string("better example",2) == "bexltept merae"
assert jumbled_string("qwertyuio",2) == "qtorieuwy"
assert jumbled_string("Greetings",8) == "Gtsegenri"