在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)