RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 876341
Accepted
Ole Lukøje
Ole Lukøje
Asked:2020-09-02 09:59:03 +0000 UTC2020-09-02 09:59:03 +0000 UTC 2020-09-02 09:59:03 +0000 UTC

python 程序如何对相同的数据产生随机结果?

  • 772

有一个名为“Pedigree: Ancestors and Descendants”的pythontutor作业。

条件:
给定树中的两个元素。确定其中一个是否是另一个的孩子。
输入数据包含一棵与上一个问题中格式相同的树接下来是请求数 K。接下来的 K 行中的每一行都包含树的两个元素的名称。
对于每个这样的查询,打印三个数字之一:如果第一个元素是第二个元素的祖先,则打印 1,如果第二个元素是第一个元素的祖先,则打印 2,或者如果它们都不是另一个元素的祖先,则打印 0。

它是介绍性的(src_data.txt):

9
Alexei Peter_I
Anna Peter_I
Elizabeth Peter_I
Peter_II Alexei
Peter_III Anna
Paul_I Peter_III
Alexander_I Paul_I
Nicholaus_I Paul_I
3
Anna Nicholaus_I
Peter_II Peter_I
Alexei Paul_I

有一个代码(嗯,就是一个解决方案的尝试):

def input():
    return rows.pop(0)

rows = []

file = open('src_data.txt', 'r')
for line in file:
    rows.append(line[:-1])
# ======== Досюда это имитация ввода на PythonTutor ===
names = set()
tree = []
for _ in range(int(input()), 1, -1):
    child, parrent = input().split()
    tree.append([child, parrent])
    names.update([child, parrent])
for name in names:
    for item in tree:
        if name == item[0]:
            for i in range(len(tree)):
                if name == tree[i][-1]:
                    tree[i].append(item[1])
# === Собственно проблема вот этом блоке (который выше) ==
# === Он то формирует списки как нужно, то недовставляет элементы ==

res_row = []
for _ in range(int(input())):
    pers_1, pers_2 = input().split()
    res = '0'
    for branch in tree:
        if pers_1 in branch and pers_2 in branch:
            if branch.index(pers_1) > branch.index(pers_2):
                res = '1'
            else:
                res = '2'
    res_row.append(res)
print(' '.join(res_row))

如果不是输出中的随机值,一切都会好起来的:

for i in {1..40}; do python3 first.py; sleep 1; done
0 2 0
0 2 0
0 2 0
1 2 0
0 2 0
1 2 0
1 2 0
0 2 0
1 2 0
1 2 0
1 2 0
1 2 0
1 2 0
0 2 0

请解释这是怎么回事,有什么问题?

python
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Ole Lukøje
    2020-09-02T10:18:41Z2020-09-02T10:18:41Z

    其实想通了。我注意到本身有问题的块是一条曲线,结果的随机性是由于该块中涉及的集合没有按定义排序,并且元素每次分别排列不同,据我了解,循环不会达到所有值,并且输出取决于元素所在的集合中的位置。

    所以我完成了它(我自己没有看开发人员):-))。

    def family(p: list, main_dict: dict):
        if p[-1] not in main_dict:
            return p
        return family(p + main_dict[p[-1]], main_dict)
    
    
    ancestors = {x: [y] for x, y in [input().split() for _ in range(int(input()) - 1)]}
    
    
    for child, parent in ancestors.items():
        ancestors[child] = family(ancestors[child], ancestors)
    
    for child, parent in ancestors.copy().items():
        if parent[0] not in ancestors:
            ancestors[parent[0]] = ancestors.get(parent[0], list())
    
    relatives = []
    for _ in range(int(input())):
        kinsman_1, kinsman_2 = input().split()
        if kinsman_1 in ancestors[kinsman_2]:
            relatives.append(1)
        elif kinsman_2 in ancestors[kinsman_1]:
            relatives.append(2)
        else:
            relatives.append(0)
    
    print(*relatives)
    
    • 2
  2. fedor.chernolutsky
    2020-09-02T23:27:11Z2020-09-02T23:27:11Z

    不要忘记在 file=open(***) 之后执行 file.close() 顺便说一句,您需要在此任务中使用递归。我自己也尝试在 pythontutor 上解决这个问题很长时间,我提出了一个非常漫长而可怕的解决方案(当然没有任何递归)))并且它适用于 pythontutor 的所有变体,然后我想出了 pythontutor 提供的解决方案我)对我来说是一项艰巨的任务。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5