小编Sch*_*ang的帖子

如何选择编写紧凑但复杂的代码和易于遵循但更长的Python代码?

在Hackerrank(https://www.hackerrank.com/challenges/merge-the-tools/problem)上做这个挑战时,我遇到了用户写的这个高投票的答案.紧凑很好但我觉得很难遵循.

def merge_the_tools(string, k):
    S, N = input(), int(input()) 
    for part in zip(*[iter(S)] * N):
        d = dict()
        print(''.join([ d.setdefault(c, c) for c in part if c not in d ]))
Run Code Online (Sandbox Code Playgroud)

这是我编码的方式:

def merge_the_tools(string, k):
    # s1. cut string into list of substrings
    t=[]
    start=0
    k=int(k)
    end=k
    while(end<len(string)+1):
        t.append(string[start:end])
        start+=k
        end+=k
    #test: print(t)

    #s2. strip repeating char from t_i by iterating thru
    for ti in t:
        si=""
        for char in ti:
            if char not in si:
                si+=char
        print(si) …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

6
推荐指数
1
解决办法
103
查看次数

标签 统计

python ×1

python-3.x ×1