Python问题 | 在https://www.testdome.com开发的代码测试分数有bug吗

2024-06-24 13:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

关于Test Dome的问题。你知道吗

实施“按所有者分组”功能:

接受包含每个文件名的文件所有者名称的字典。你知道吗

返回一个字典,其中包含每个所有者名称的文件名列表,按任意顺序排列。你知道吗

例如,对于dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt': 'Randy'}group_by_owners函数应该返回{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}。你知道吗

当我运行我在做示例测试时开发的下面的代码时,他们的在线应用程序给了我0%的分数,如果你在IDE中运行下面的代码,它会输出正确的转换字典。你知道吗

为什么他们的网站没有为开发的测试打分呢?这是他们的算法的一个缺陷,可以检测他们网站上的代码是否正确?还是我做的不对?你知道吗

我对Python的理解大约有2年的经验。你知道吗

'''Find all unique names given a dict'''
def getUniqueNames(file):
    file_owner_names = []
    for file_type, file_o in file.items():
        if file_o not in file_owner_names:
             file_owner_names.append(file_o)

     return file_owner_names

'''Get a list of files for an owner given a dict and owner'''
def getFileArray(file, file_owner):
    file_type_names = []
    for file_t, file_o in file.items():
        if file_o == file_owner:
            if file_t not in file_type_names:
                file_type_names.append(file_t)

    return file_type_names

'''Learned to used the dict'''
def group_by_owners(files_dict):
     new_file = {}
     i = 0
     file_owner_names = getUniqueNames(files_dict)
     for file_owner_name in file_owner_names:
         if i != len(file_owner_names):
              if (i < len(file_owner_names)):
                 new_file[file_owner_name] = str(getFileArray(files_dict, 
                 file_owner_name))
                 i = i + 1

return new_file


files = {
    'Input.txt': 'Randy',
    'Code.py': 'Stan',
    'HomeController.py': 'Randy',
    'Output.txt': 'Jeff',
    'SearchController.py': 'Rafeena',
    'ABTest.py': 'Nicholas',
    'SQL.py':'Nicholas'
 }

 print(group_by_owners(files))

上述python脚本的输出:

{'Randy': "['Input.txt', 'HomeController.py']", 'Stan': "['Code.py']", 'Jeff': "['Output.txt']", 'Rafeena': "['SearchController.py']", 'Nicholas': "['ABTest.py', 'SQL.py']"}

Tags: inpytxtinputoutputifnamestype
3条回答

我还没测试过,但我认为你的问题是你应该改变

new_file[file_owner_name] = str(getFileArray(files_dict, file_owner_name))

new_file[file_owner_name] = getFileArray(files_dict, file_owner_name)

通过删除对str的调用。你知道吗

输出应为:

{'Randy': ['Input.txt', 'HomeController.py'], 'Stan': ['Code.py'], 'Jeff': ['Output.txt'], 'Rafeena': ['SearchController.py'], 'Nicholas': ['ABTest.py', 'SQL.py']}

注意列表周围缺少引号。你知道吗

您可以从集合中使用defaultdict。Defaultdict负责添加新密钥。你知道吗

from collections import defaultdict
d = {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt': 'Randy'}
o = defaultdict(list)
for k, v in d.items():
    o[v] += [k]
print(dict(o))
# prints {'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}

不完全回答你的问题,但是这个解决方案使用标准字典。你知道吗

def group_by_owners(files):
    grouped_files = {}

    for key, value in sorted(files.items()):
        grouped_files.setdefault(value, []).append(key)

    return grouped_files

#prints {'Stan': ['Code.py'], 'Randy': ['Input.txt', 'Output.txt']}

我的答案基于一个例子:StackOverflow

相关问题 更多 >