gpt4 book ai didi

Python 问题 |在 https ://www. testdome.com 上开发的代码的测试分数是否存在错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:30:05 24 4
gpt4 key购买 nike

关于 Test Dome 的问题.

实现一个 group_by_owners 函数:

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

返回一个字典,其中包含每个所有者名称的文件名列表,顺序不限。

例如,对于字典 {'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']"}

最佳答案

您可以使用集合中的 defaultdict。 Defaultdict 负责添加新 key 。

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']}

关于Python 问题 |在 https ://www. testdome.com 上开发的代码的测试分数是否存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com