为什么代码不能满足Hackerrank中的所有测试用例?

问题描述 投票:0回答:1

我正在尝试解决 python Hackerrank 中的“合并工具”问题,我的代码满足示例测试用例,但不满足其他测试用例。有人可以帮我找出我哪里出错了吗?

这是问题的链接:https://www.hackerrank.com/challenges/merge-the-tools/problem?isFullScreen=true

这就是我在函数中编写的内容:

import textwrap
from collections import OrderedDict as od

def merge_the_tools(string, k):
    # your code goes here
    length = int(len(string)/k)
    words = textwrap.wrap(string,length)

    for i in words:
        new_dict = od.fromkeys(i)
        print("".join(new_dict))
python string ordereddictionary
1个回答
0
投票

一本简单的字典就足够了。您不需要导入任何东西

def merge_the_tools(string, k):
    for i in range(0, len(string), k):
        d = {}
        for c in string[i:i+k]:
            d[c] = None
        print(*d, sep="")
© www.soinside.com 2019 - 2024. All rights reserved.